Borrow&Liquidate
Borrow
comptroller.borrowAllowed(cToken, borrower, borrowAmount);
CToken use these code to adjust user can borrow borrowAmount of underlying .
Then write accountBorrows[borrower] to save changes.
struct BorrowSnapshot {
uint principal;
uint interestIndex;
}
mapping(address => BorrowSnapshot) accountBorrows;
accountBorrows record borrow action of an account,
is used by borrowBalance.
const m = accountBorrows[user];
borrowBalance = user => m.principal * borrowIndex / m.interestIndex;
borroeRate
Using code below to get borrowRate from interestRateModel
interestRateModel.getBorrowRate(getCashPrior(), totalBorrows, totalReserves);
Input:
- getCashPrior: underlying balance of CToken.
- totalBorrows: total underlying borrowed.
- totalReserves: total reserves.
accrueInterest
main logic:
simpleInterestFactor = borrowRate * blockDelta
interestAccumulated = simpleInterestFactor * totalBorrows
totalBorrowsNew = interestAccumulated + totalBorrows
totalReservesNew = interestAccumulated * reserveFactor + totalReserves
borrowIndexNew = simpleInterestFactor * borrowIndex + borrowIndex
accrueInterest write:
- borrowIndex
- totalBorrows
- totalReserves
It seems that when borrow and repay called in the same block, there will be no interst.
repayBorrow
...
liquidateBorrow
Use code below to calc seize Amount
comptroller.liquidateCalculateSeizeTokens(address(this), address(cTokenCollateral), repayAmount);

浙公网安备 33010602011771号