Skip to content
Prev Previous commit
Add edge cases claim expired
  • Loading branch information
Agusx1211 committed Nov 12, 2019
commit 2f645fe7d0c5c4124f02f39e5d9ae81b55eba07f
172 changes: 172 additions & 0 deletions test/diaspore/TestCollateralAlt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2368,4 +2368,176 @@ contract('Test Collateral cosigner Diaspore', function ([_, stub, owner, user, a
expect(entry.amount).to.eq.BN(b(2250));
});
});
describe('Should handle edge cases on claim expired debt', () => {
it('Should ignore claim if debt is not expired', async () => {
// Create oracle and alt token
const dai = await TestToken.new();
const oracle = await TestRateOracle.new();
await oracle.setToken(dai.address);
await oracle.setEquivalent(b('500000000000000000'));

// Create loan oracle
const loanOracle = await TestRateOracle.new();
await loanOracle.setEquivalent(b('2000000000000000000'));

// configure converter
await rcn.setBalance(converter.address, b(2).pow(b(64)));
await dai.setBalance(converter.address, b(2).pow(b(64)));
await converter.setRate(dai.address, rcn.address, b('2000000000000000000'));
await converter.setRate(rcn.address, dai.address, b('500000000000000000'));

// Request a loan
const modelData = await model.encodeData(
b(1000),
MAX_UINT64
);

// Request loan
const requestReceipt = await loanManager.requestLoan(
b(1000), // Requested amount
model.address, // Debt model
loanOracle.address, // Oracle
user, // Borrower
address0x, // Callback
b(0), // Salt
MAX_UINT64, // Expiration
modelData, // Model data
{
from: user,
}
);

const debtId = requestReceipt.receipt.logs.find((e) => e.event === 'Requested').args._id;

// Create collateral entry
await dai.setBalance(user, b(2500));
await dai.approve(collateral.address, b(2500), { from: user });
await collateral.create(
debtId, // Debt ID
oracle.address, // Oracle address
b(2500), // Token Amount
b(12000), // Liquidation Ratio
b(15000), // Balance ratio
b(9), // Burn fee
b(1), // Reward fee
{
from: user,
}
);

const entryId = b(1);

// Lend loan
await rcn.setBalance(anotherUser, b(500));
await rcn.approve(loanManager.address, b(500), { from: anotherUser });
await loanManager.lend(
debtId, // Debt ID
[], // Oracle data
collateral.address, // Collateral cosigner
b(0), // Cosigner limit
toBytes32(entryId), // Cosigner data
[], // Callback data
{
from: anotherUser,
}
);

// Claim loan payment (due time)
await collateral.claim(address0x, debtId, [], { from: stub });
expect(await dai.balanceOf(collateral.address)).to.eq.BN(b(2500));
expect(await rcn.balanceOf(debtEngine.address)).to.eq.BN(b(0));
expect(await model.getPaid(debtId)).to.eq.BN(b(0));
const entry = await collateral.entries(entryId);
expect(entry.amount).to.eq.BN(b(2500));
});
it('Should only use collateral in entry', async () => {
// Create oracle and alt token
const dai = await TestToken.new();
const oracle = await TestRateOracle.new();
await oracle.setToken(dai.address);
await oracle.setEquivalent(b('500000000000000000'));

// Create loan oracle
const loanOracle = await TestRateOracle.new();
await loanOracle.setEquivalent(b('2000000000000000000'));

// configure converter
await rcn.setBalance(converter.address, b(2).pow(b(64)));
await dai.setBalance(converter.address, b(2).pow(b(64)));
await converter.setRate(dai.address, rcn.address, b('2000000000000000000'));
await converter.setRate(rcn.address, dai.address, b('500000000000000000'));

// Request a loan
const modelData = await model.encodeData(
b(1000),
MAX_UINT64
);

// Request loan
const requestReceipt = await loanManager.requestLoan(
b(1000), // Requested amount
model.address, // Debt model
loanOracle.address, // Oracle
user, // Borrower
address0x, // Callback
b(0), // Salt
MAX_UINT64, // Expiration
modelData, // Model data
{
from: user,
}
);

const debtId = requestReceipt.receipt.logs.find((e) => e.event === 'Requested').args._id;

// Create collateral entry
await dai.setBalance(user, b(2500));
await dai.approve(collateral.address, b(2500), { from: user });
await collateral.create(
debtId, // Debt ID
oracle.address, // Oracle address
b(2500), // Token Amount
b(12000), // Liquidation Ratio
b(15000), // Balance ratio
b(0), // Burn fee
b(0), // Reward fee
{
from: user,
}
);

const entryId = b(1);

// Lend loan
await rcn.setBalance(anotherUser, b(500));
await rcn.approve(loanManager.address, b(500), { from: anotherUser });
await loanManager.lend(
debtId, // Debt ID
[], // Oracle data
collateral.address, // Collateral cosigner
b(0), // Cosigner limit
toBytes32(entryId), // Cosigner data
[], // Callback data
{
from: anotherUser,
}
);

// Move due time
await model.setDueTime(debtId, b(500), { from: owner });
await model.setTotal(debtId, b(900000), { from: owner });

// Increase balance of collateral contract
await dai.setBalance(collateral.address, b(5000000));

// Claim loan payment (due time)
// and should use all collateral
await collateral.claim(address0x, debtId, [], { from: stub });
expect(await dai.balanceOf(collateral.address)).to.eq.BN(b(5000000).sub(b(2500)));
expect(await rcn.balanceOf(debtEngine.address)).to.eq.BN(b(5000));
expect(await model.getPaid(debtId)).to.eq.BN(b(10000));
const entry = await collateral.entries(entryId);
expect(entry.amount).to.eq.BN(b(0));
});
});
});