Skip to content

Commit

Permalink
update: totalBorrows
Browse files Browse the repository at this point in the history
  • Loading branch information
0xyolo committed Apr 8, 2021
1 parent 88d22f0 commit 0d23116
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
12 changes: 9 additions & 3 deletions contracts/CCapableErc20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ contract CCapableErc20 is CToken, CCapableErc20Interface, CCapableDelegateInterf
}

function flashLoan(address receiver, uint amount, bytes calldata params) external nonReentrant {
require(accrueInterest() == uint(Error.NO_ERROR), "accrue interest failed");

uint cashOnChainBefore = getCashOnChain();
uint cashBefore = getCashPrior();
require(cashBefore >= amount, "INSUFFICIENT_LIQUIDITY");
Expand All @@ -239,17 +241,21 @@ contract CCapableErc20 is CToken, CCapableErc20Interface, CCapableDelegateInterf
// 2. transfer fund to receiver
doTransferOut(address(uint160(receiver)), amount);

// 3. execute receiver's callback function
// 3. update totalBorrows
totalBorrows = add_(totalBorrows, amount);

// 4. execute receiver's callback function
IFlashloanReceiver(receiver).executeOperation(msg.sender, underlying, amount, totalFee, params);

// 4. check balance
// 5. check balance
uint cashOnChainAfter = getCashOnChain();
require(cashOnChainAfter == add_(cashOnChainBefore, totalFee), "BALANCE_INCONSISTENT");

// 5. update reserves and internal cash
// 6. update reserves and internal cash and totalBorrows
uint reservesFee = mul_ScalarTruncate(Exp({mantissa: reserveFactorMantissa}), totalFee);
totalReserves = add_(totalReserves, reservesFee);
internalCash = add_(cashBefore, totalFee);
totalBorrows = sub_(totalBorrows, amount);

emit Flashloan(receiver, amount, totalFee, reservesFee);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Contracts/FlashloanReceiver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import "../../contracts/SafeMath.sol";
contract FlashloanReceiver is IFlashloanReceiver {
using SafeMath for uint256;

uint totalBorrows;

function doFlashloan(address cToken, uint borrowAmount, uint repayAmount) external {
uint balanceBefore = ERC20(CCapableErc20(cToken).underlying()).balanceOf(address(this));
bytes memory data = abi.encode(cToken, borrowAmount, repayAmount);
totalBorrows = CCapableErc20(cToken).totalBorrows();
CCapableErc20(cToken).flashLoan(address(this), borrowAmount, data);
uint balanceAfter = ERC20(CCapableErc20(cToken).underlying()).balanceOf(address(this));
require(balanceAfter == balanceBefore.add(borrowAmount).sub(repayAmount), "Balance inconsistent");
Expand All @@ -20,6 +23,8 @@ contract FlashloanReceiver is IFlashloanReceiver {
(address cToken, uint borrowAmount, uint repayAmount) = abi.decode(params, (address, uint, uint));
require(underlying == CCapableErc20(cToken).underlying(), "Params not match");
require(amount == borrowAmount, "Params not match");
uint totalBorrowsAfter = CCapableErc20(cToken).totalBorrows();
require(totalBorrows.add(borrowAmount) == totalBorrowsAfter, "totalBorrow mismatch");
require(ERC20(underlying).transfer(cToken, repayAmount), "Transfer fund back failed");
}
}
Expand Down

0 comments on commit 0d23116

Please sign in to comment.