Skip to content

Commit

Permalink
add flash loan test
Browse files Browse the repository at this point in the history
edit

add sol

add upgrade flashloan
  • Loading branch information
stitetsati committed Aug 18, 2021
1 parent 070ce12 commit 27f5e91
Show file tree
Hide file tree
Showing 57 changed files with 13,179 additions and 4,320 deletions.
5 changes: 5 additions & 0 deletions abi/cCollateralCapDelegator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = [
'function _setImplementation(address implementation_, bool allowResign, bytes memory becomeImplementationData) external',
'function implementation() view returns (address) '

];
6 changes: 6 additions & 0 deletions abi/cTokenAdmin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


module.exports = [
'function _setImplementation(address cToken, address implementation, bool allowResign, bytes calldata becomeImplementationData) external',
];

2 changes: 2 additions & 0 deletions abi/comptroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ module.exports = [
'function _setAllowlist(address protocol, bool allow) external',
'function _setCreditLimit(address protocol, uint creditLimit) external',
'function _dropInvalidMarket() external',
'function _setFlashloanPaused(address cToken, bool state) external returns (bool)',
'function flashloanGuardianPaused(address addr) external view returns (bool)',
'function getAllMarkets() external view returns (address[] memory)'
];
2 changes: 2 additions & 0 deletions abi/erc20.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module.exports = [
'function approve(address spender, uint256 amount) external returns (bool success)',
'function transfer(address to, uint value) external',
'function balanceOf(address account) external view returns (uint)',

];
2 changes: 1 addition & 1 deletion abi/unitroller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = [
'function _setPendingImplementation(address newPendingImplementation) external returns (uint)',
'function comptrollerImplementation() external view returns (address)'
'function comptrollerImplementation() external view returns (address)',
];
65 changes: 44 additions & 21 deletions contracts/CCTokenDelegate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import "./EIP20Interface.sol";
*/
interface IComptroller {
function getCompAddress() external view returns (address);
function claimComp(address[] calldata holders, CToken[] calldata cTokens, bool borrowers, bool suppliers) external;

function claimComp(
address[] calldata holders,
CToken[] calldata cTokens,
bool borrowers,
bool suppliers
) external;
}

/**
Expand All @@ -33,8 +39,8 @@ contract CCTokenDelegate is CCapableErc20Delegate {
* @member index The last updated index
*/
struct RewardState {
uint balance;
uint index;
uint256 balance;
uint256 index;
}

/**
Expand All @@ -45,12 +51,12 @@ contract CCTokenDelegate is CCapableErc20Delegate {
/**
* @notice The index of every Compound's CToken supplier
*/
mapping(address => uint) public supplierState;
mapping(address => uint256) public supplierState;

/**
* @notice The comp amount of every user
*/
mapping(address => uint) public compUserAccrued;
mapping(address => uint256) public compUserAccrued;

/**
* @notice Delegate interface to become the implementation
Expand All @@ -67,20 +73,20 @@ contract CCTokenDelegate is CCapableErc20Delegate {
* @notice Manually claim comp rewards by user
* @return The amount of comp rewards user claims
*/
function claimComp() public returns (uint) {
function claimComp(address account) public returns (uint256) {
harvestComp();

updateSupplyIndex();
updateSupplierIndex(msg.sender);
updateSupplierIndex(account);

uint compBalance = compUserAccrued[msg.sender];
uint256 compBalance = compUserAccrued[account];
if (compBalance > 0) {
// Transfer user comp and subtract the balance in supplyState
EIP20Interface(comp).transfer(msg.sender, compBalance);
EIP20Interface(comp).transfer(account, compBalance);
supplyState.balance = sub_(supplyState.balance, compBalance);

// Clear user's comp accrued.
compUserAccrued[msg.sender] = 0;
compUserAccrued[account] = 0;

return compBalance;
}
Expand All @@ -97,7 +103,12 @@ contract CCTokenDelegate is CCapableErc20Delegate {
* @param tokens The number of tokens to transfer
* @return Whether or not the transfer succeeded
*/
function transferTokens(address spender, address src, address dst, uint tokens) internal returns (uint) {
function transferTokens(
address spender,
address src,
address dst,
uint256 tokens
) internal returns (uint256) {
harvestComp();

updateSupplyIndex();
Expand All @@ -113,11 +124,17 @@ contract CCTokenDelegate is CCapableErc20Delegate {
* @notice Transfer the underlying to this contract
* @param from Address to transfer funds from
* @param amount Amount of underlying to transfer
* @param isNative The amount is in native or not
* @return The actual amount that is transferred
*/
function doTransferIn(address from, uint amount) internal returns (uint) {
uint transferredIn = super.doTransferIn(from, amount);
function doTransferIn(
address from,
uint256 amount,
bool isNative
) internal returns (uint256) {
uint256 transferredIn = super.doTransferIn(from, amount, isNative);

harvestComp();
updateSupplyIndex();
updateSupplierIndex(from);

Expand All @@ -128,19 +145,25 @@ contract CCTokenDelegate is CCapableErc20Delegate {
* @notice Transfer the underlying from this contract
* @param to Address to transfer funds to
* @param amount Amount of underlying to transfer
* @param isNative The amount is in native or not
*/
function doTransferOut(address payable to, uint amount) internal {
function doTransferOut(
address payable to,
uint256 amount,
bool isNative
) internal {
harvestComp();
updateSupplyIndex();
updateSupplierIndex(to);

super.doTransferOut(to, amount);
super.doTransferOut(to, amount, isNative);
}

/*** Internal functions ***/

function harvestComp() internal {
address[] memory holders = new address[](1);
holders[0] = msg.sender;
holders[0] = address(this);
CToken[] memory cTokens = new CToken[](1);
cTokens[0] = CToken(underlying);

Expand All @@ -149,8 +172,8 @@ contract CCTokenDelegate is CCapableErc20Delegate {
}

function updateSupplyIndex() internal {
uint compAccrued = sub_(compBalance(), supplyState.balance);
uint supplyTokens = CToken(address(this)).totalSupply();
uint256 compAccrued = sub_(compBalance(), supplyState.balance);
uint256 supplyTokens = CToken(address(this)).totalSupply();
Double memory ratio = supplyTokens > 0 ? fraction(compAccrued, supplyTokens) : Double({mantissa: 0});
Double memory index = add_(Double({mantissa: supplyState.index}), ratio);

Expand All @@ -164,14 +187,14 @@ contract CCTokenDelegate is CCapableErc20Delegate {
Double memory supplierIndex = Double({mantissa: supplierState[supplier]});
Double memory deltaIndex = sub_(supplyIndex, supplierIndex);
if (deltaIndex.mantissa > 0) {
uint supplierTokens = CToken(address(this)).balanceOf(supplier);
uint supplierDelta = mul_(supplierTokens, deltaIndex);
uint256 supplierTokens = CToken(address(this)).balanceOf(supplier);
uint256 supplierDelta = mul_(supplierTokens, deltaIndex);
compUserAccrued[supplier] = add_(compUserAccrued[supplier], supplierDelta);
supplierState[supplier] = supplyIndex.mantissa;
}
}

function compBalance() internal view returns (uint) {
function compBalance() internal view returns (uint256) {
return EIP20Interface(comp).balanceOf(address(this));
}
}
Loading

0 comments on commit 27f5e91

Please sign in to comment.