removed bugs and fixed lint

This commit is contained in:
pradyuman-verma 2022-03-23 13:41:24 +05:30
parent 98d9c158fc
commit 5ff4637b91
No known key found for this signature in database
GPG Key ID: E36FD6BC8923221F
4 changed files with 367 additions and 268 deletions

View File

@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
pragma experimental ABIEncoderV2;

View File

@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
import { DSMath } from "../../common/math.sol";
@ -6,26 +7,28 @@ import { TokenInterface, AccountInterface } from "../../common/interfaces.sol";
import { ComptrollerInterface, CompoundMappingInterface, CETHInterface, CTokenInterface } from "./interface.sol";
abstract contract Helpers is DSMath, Basic {
/**
* @dev Compound CEth
*/
CETHInterface internal constant cEth = CETHInterface(0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5);
CETHInterface internal constant cEth =
CETHInterface(0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5);
/**
* @dev Compound Comptroller
*/
ComptrollerInterface internal constant troller = ComptrollerInterface(0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B);
ComptrollerInterface internal constant troller =
ComptrollerInterface(0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B);
/**
* @dev Compound Mapping
*/
CompoundMappingInterface internal constant compMapping = CompoundMappingInterface(0xe7a85d0adDB972A4f0A4e57B698B37f171519e88);
CompoundMappingInterface internal constant compMapping =
CompoundMappingInterface(0xe7a85d0adDB972A4f0A4e57B698B37f171519e88);
struct ImportData {
address[] cTokens; // is the list of all tokens the user has interacted with (supply/borrow) -> used to enter markets
uint[] borrowAmts;
uint[] supplyAmts;
uint256[] borrowAmts;
uint256[] supplyAmts;
address[] borrowTokens;
address[] supplyTokens;
CTokenInterface[] borrowCtokens;
@ -50,7 +53,6 @@ abstract contract Helpers is DSMath, Basic {
}
contract CompoundHelper is Helpers {
/**
* @notice fetch the borrow details of the user
* @dev approve the cToken to spend (borrowed amount of) tokens to allow for repaying later
@ -64,23 +66,31 @@ contract CompoundHelper is Helpers {
) internal returns (ImportData memory) {
if (_importInputData.borrowIds.length > 0) {
// initialize arrays for borrow data
data.borrowTokens = new address[](_importInputData.borrowIds.length);
data.borrowCtokens = new CTokenInterface[](_importInputData.borrowIds.length);
data.borrowCtokensAddr = new address[](_importInputData.borrowIds.length);
data.borrowAmts = new uint[](_importInputData.borrowIds.length);
uint256 _length = _importInputData.borrowIds.length;
data.borrowTokens = new address[](_length);
data.borrowCtokens = new CTokenInterface[](_length);
data.borrowCtokensAddr = new address[](_length);
data.borrowAmts = new uint256[](_length);
// populate the arrays with borrow tokens, cToken addresses and instances, and borrow amounts
for (uint i = 0; i < _importInputData.borrowIds.length; i++) {
(address _token, address _cToken) = compMapping.getMapping(_importInputData.borrowIds[i]);
for (uint256 i; i < _length; i++) {
(address _token, address _cToken) = compMapping.getMapping(
_importInputData.borrowIds[i]
);
require(_token != address(0) && _cToken != address(0), "ctoken mapping not found");
require(
_token != address(0) && _cToken != address(0),
"ctoken mapping not found"
);
data.cTokens[i] = _cToken;
data.borrowTokens[i] = _token;
data.borrowCtokens[i] = CTokenInterface(_cToken);
data.borrowCtokensAddr[i] = _cToken;
data.borrowAmts[i] = data.borrowCtokens[i].borrowBalanceCurrent(_importInputData.userAccount);
data.borrowAmts[i] = data.borrowCtokens[i].borrowBalanceCurrent(
_importInputData.userAccount
);
// give the resp. cToken address approval to spend tokens
if (_token != ethAddr && data.borrowAmts[i] > 0) {
@ -104,24 +114,32 @@ contract CompoundHelper is Helpers {
ImportData memory data
) internal view returns (ImportData memory) {
// initialize arrays for supply data
data.supplyTokens = new address[](_importInputData.supplyIds.length);
data.supplyCtokens = new CTokenInterface[](_importInputData.supplyIds.length);
data.supplyCtokensAddr = new address[](_importInputData.supplyIds.length);
data.supplyAmts = new uint[](_importInputData.supplyIds.length);
uint256 _length = _importInputData.supplyIds.length;
data.supplyTokens = new address[](_length);
data.supplyCtokens = new CTokenInterface[](_length);
data.supplyCtokensAddr = new address[](_length);
data.supplyAmts = new uint256[](_length);
// populate arrays with supply data (supply tokens address, cToken addresses, cToken instances and supply amounts)
for (uint i = 0; i < _importInputData.supplyIds.length; i++) {
(address _token, address _cToken) = compMapping.getMapping(_importInputData.supplyIds[i]);
for (uint256 i; i < _length; i++) {
(address _token, address _cToken) = compMapping.getMapping(
_importInputData.supplyIds[i]
);
require(_token != address(0) && _cToken != address(0), "ctoken mapping not found");
require(
_token != address(0) && _cToken != address(0),
"ctoken mapping not found"
);
uint _supplyIndex = add(i, _importInputData.borrowIds.length);
uint256 _supplyIndex = add(i, _importInputData.borrowIds.length);
data.cTokens[_supplyIndex] = _cToken;
data.supplyTokens[i] = _token;
data.supplyCtokens[i] = CTokenInterface(_cToken);
data.supplyCtokensAddr[i] = (_cToken);
data.supplyAmts[i] = data.supplyCtokens[i].balanceOf(_importInputData.userAccount);
data.supplyAmts[i] = data.supplyCtokens[i].balanceOf(
_importInputData.userAccount
);
}
return data;
}
@ -136,19 +154,22 @@ contract CompoundHelper is Helpers {
function _repayUserDebt(
address _userAccount,
CTokenInterface[] memory _cTokenContracts,
uint[] memory _borrowAmts
uint256[] memory _borrowAmts
) internal {
for(uint i = 0; i < _cTokenContracts.length; i++){
for (uint256 i; i < _cTokenContracts.length; i++) {
if (_borrowAmts[i] > 0) {
if(address(_cTokenContracts[i]) == address(cEth)){
cEth.repayBorrowBehalf{value: _borrowAmts[i]}(_userAccount);
}
else{
require(_cTokenContracts[i].repayBorrowBehalf(
if (address(_cTokenContracts[i]) == address(cEth))
cEth.repayBorrowBehalf{ value: _borrowAmts[i] }(
_userAccount
);
else
require(
_cTokenContracts[i].repayBorrowBehalf(
_userAccount,
_borrowAmts[i]
) == 0, "repayOnBehalf-failed");
}
) == 0,
"repayOnBehalf-failed"
);
}
}
}
@ -163,17 +184,18 @@ contract CompoundHelper is Helpers {
function _transferTokensToDsa(
address _userAccount,
CTokenInterface[] memory _cTokenContracts,
uint[] memory _amts
uint256[] memory _amts
) internal {
for(uint i = 0; i < _cTokenContracts.length; i++) {
if(_amts[i] > 0) {
require(_cTokenContracts[i].transferFrom(
for (uint256 i; i < _cTokenContracts.length; i++)
if (_amts[i] > 0)
require(
_cTokenContracts[i].transferFrom(
_userAccount,
address(this),
_amts[i]
), "ctoken-transfer-failed-allowance?");
}
}
),
"ctoken-transfer-failed-allowance?"
);
}
/**
@ -188,14 +210,13 @@ contract CompoundHelper is Helpers {
uint256[] memory _amts,
uint256[] memory _flashLoanFees
) internal {
for (uint i = 0; i < _cTokenContracts.length; i++) {
if (_amts[i] > 0) {
require(_cTokenContracts[i].borrow(
add(
_amts[i],
_flashLoanFees[i]
)) == 0, "borrow-failed-collateral?");
}
}
for (uint256 i; i < _cTokenContracts.length; i++)
if (_amts[i] > 0)
require(
_cTokenContracts[i].borrow(
add(_amts[i], _flashLoanFees[i])
) == 0,
"borrow-failed-collateral?"
);
}
}

View File

@ -1,46 +1,100 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
interface TokenInterface {
function balanceOf(address) external view returns (uint);
function allowance(address, address) external view returns (uint);
function approve(address, uint) external;
function transfer(address, uint) external returns (bool);
function transferFrom(address, address, uint) external returns (bool);
function balanceOf(address) external view returns (uint256);
function allowance(address, address) external view returns (uint256);
function approve(address, uint256) external;
function transfer(address, uint256) external returns (bool);
function transferFrom(
address,
address,
uint256
) external returns (bool);
}
interface CTokenInterface {
function mint(uint mintAmount) external returns (uint);
function redeem(uint redeemTokens) external returns (uint);
function borrow(uint borrowAmount) external returns (uint);
function repayBorrow(uint repayAmount) external returns (uint);
function repayBorrowBehalf(address borrower, uint repayAmount) external returns (uint); // For ERC20
function liquidateBorrow(address borrower, uint repayAmount, address cTokenCollateral) external returns (uint);
function mint(uint256 mintAmount) external returns (uint256);
function borrowBalanceCurrent(address account) external returns (uint);
function redeemUnderlying(uint redeemAmount) external returns (uint);
function exchangeRateCurrent() external returns (uint);
function redeem(uint256 redeemTokens) external returns (uint256);
function borrow(uint256 borrowAmount) external returns (uint256);
function repayBorrow(uint256 repayAmount) external returns (uint256);
function repayBorrowBehalf(address borrower, uint256 repayAmount)
external
returns (uint256); // For ERC20
function liquidateBorrow(
address borrower,
uint256 repayAmount,
address cTokenCollateral
) external returns (uint256);
function borrowBalanceCurrent(address account) external returns (uint256);
function redeemUnderlying(uint256 redeemAmount) external returns (uint256);
function exchangeRateCurrent() external returns (uint256);
function balanceOf(address owner) external view returns (uint256 balance);
function transferFrom(address, address, uint) external returns (bool);
function allowance(address, address) external view returns (uint);
function transferFrom(
address,
address,
uint256
) external returns (bool);
function allowance(address, address) external view returns (uint256);
}
interface CETHInterface {
function mint() external payable;
function repayBorrow() external payable;
function repayBorrowBehalf(address borrower) external payable;
function liquidateBorrow(address borrower, address cTokenCollateral) external payable;
function liquidateBorrow(address borrower, address cTokenCollateral)
external
payable;
}
interface ComptrollerInterface {
function enterMarkets(address[] calldata cTokens) external returns (uint[] memory);
function exitMarket(address cTokenAddress) external returns (uint);
function getAssetsIn(address account) external view returns (address[] memory);
function getAccountLiquidity(address account) external view returns (uint, uint, uint);
function enterMarkets(address[] calldata cTokens)
external
returns (uint256[] memory);
function exitMarket(address cTokenAddress) external returns (uint256);
function getAssetsIn(address account)
external
view
returns (address[] memory);
function getAccountLiquidity(address account)
external
view
returns (
uint256,
uint256,
uint256
);
}
interface CompoundMappingInterface {
function cTokenMapping(string calldata tokenId) external view returns (address);
function getMapping(string calldata tokenId) external view returns (address, address);
function cTokenMapping(string calldata tokenId)
external
view
returns (address);
function getMapping(string calldata tokenId)
external
view
returns (address, address);
}

View File

@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.6;
pragma experimental ABIEncoderV2;
@ -11,7 +12,6 @@ import { Events } from "./events.sol";
// 4. Then borrow debt of same tokens but include flash loan fee in it.
contract CompoundImportResolver is CompoundHelper {
/**
* @notice this function performs the import of user's Compound positions into its DSA
* @dev called internally by the importCompound and migrateCompound functions
@ -22,29 +22,49 @@ contract CompoundImportResolver is CompoundHelper {
ImportInputData memory _importInputData,
uint256[] memory _flashLoanFees
) internal returns (string memory _eventName, bytes memory _eventParam) {
require(AccountInterface(address(this)).isAuth(_importInputData.userAccount), "user-account-not-auth");
require(
AccountInterface(address(this)).isAuth(
_importInputData.userAccount
),
"user-account-not-auth"
);
require(_importInputData.supplyIds.length > 0, "0-length-not-allowed");
ImportData memory data;
uint _length = add(_importInputData.supplyIds.length, _importInputData.borrowIds.length);
uint256 _length = add(
_importInputData.supplyIds.length,
_importInputData.borrowIds.length
);
data.cTokens = new address[](_length);
// get info about all borrowings and lendings by the user on Compound
data = getBorrowAmounts(_importInputData, data);
data = getSupplyAmounts(_importInputData, data);
_enterMarkets(_importInputData.cTokens);
_enterMarkets(data.cTokens);
// pay back user's debt using flash loan funds
_repayUserDebt(_importInputData.userAccount, data.borrowCtokens, data.borrowAmts);
_repayUserDebt(
_importInputData.userAccount,
data.borrowCtokens,
data.borrowAmts
);
// transfer user's tokens to DSA
_transferTokensToDsa(_importInputData.userAccount, data.supplyCtokens, data.supplyAmts);
_transferTokensToDsa(
_importInputData.userAccount,
data.supplyCtokens,
data.supplyAmts
);
// borrow the earlier position from Compound with flash loan fee added
_borrowDebtPosition(data.borrowCtokens, data.borrowAmts, _flashLoanFees);
_borrowDebtPosition(
data.borrowCtokens,
data.borrowAmts,
_flashLoanFees
);
_eventName = "LogCompoundImport(address,address[],string[],string[],uint256[],uint256[])";
_eventParam = abi.encode(
@ -70,7 +90,11 @@ contract CompoundImportResolver is CompoundHelper {
string[] memory _supplyIds,
string[] memory _borrowIds,
uint256[] memory _flashLoanFees
) external payable returns (string memory _eventName, bytes memory _eventParam) {
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
ImportInputData memory inputData = ImportInputData({
userAccount: _userAccount,
supplyIds: _supplyIds,
@ -79,7 +103,6 @@ contract CompoundImportResolver is CompoundHelper {
(_eventName, _eventParam) = _importCompound(inputData, _flashLoanFees);
}
}
contract ConnectV2CompoundImport is CompoundImportResolver {