mirror of
https://github.com/Instadapp/smart-contract.git
synced 2024-07-29 22:08:07 +00:00
added cToken import
This commit is contained in:
parent
3c46226357
commit
f5d345ec66
|
@ -8,6 +8,11 @@ interface ERC20Interface {
|
||||||
function transferFrom(address, address, uint) external returns (bool);
|
function transferFrom(address, address, uint) external returns (bool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ComptrollerInterface {
|
||||||
|
function enterMarkets(address[] calldata cTokens) external returns (uint[] memory);
|
||||||
|
function getAssetsIn(address account) external view returns (address[] memory);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
contract DSMath {
|
contract DSMath {
|
||||||
function add(uint x, uint y) internal pure returns (uint z) {
|
function add(uint x, uint y) internal pure returns (uint z) {
|
||||||
|
@ -26,8 +31,41 @@ contract DSMath {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract ImportResolver is DSMath {
|
contract Helper is DSMath {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev get Compound Comptroller Address
|
||||||
|
*/
|
||||||
|
function getComptrollerAddress() public pure returns (address troller) {
|
||||||
|
troller = 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B;
|
||||||
|
}
|
||||||
|
|
||||||
|
function enterMarket(address[] memory cErc20) internal {
|
||||||
|
ComptrollerInterface troller = ComptrollerInterface(getComptrollerAddress());
|
||||||
|
address[] memory markets = troller.getAssetsIn(address(this));
|
||||||
|
address[] memory toEnter = new address[](cErc20.length);
|
||||||
|
uint count = 0;
|
||||||
|
for (uint j = 0; j < cErc20.length; j++) {
|
||||||
|
bool isEntered = false;
|
||||||
|
for (uint i = 0; i < markets.length; i++) {
|
||||||
|
if (markets[i] == cErc20[j]) {
|
||||||
|
isEntered = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isEntered) {
|
||||||
|
toEnter[count] = cErc20[j];
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
troller.enterMarkets(toEnter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
contract ImportResolver is Helper {
|
||||||
event LogTokensImport(address owner, uint percentage, address[] tokenAddr, uint[] tokenBalArr);
|
event LogTokensImport(address owner, uint percentage, address[] tokenAddr, uint[] tokenBalArr);
|
||||||
|
event LogCTokensImport(address owner, uint percentage, address[] tokenAddr, uint[] tokenBalArr);
|
||||||
|
|
||||||
function importTokens(uint toConvert, address[] memory tokenAddrArr) public {
|
function importTokens(uint toConvert, address[] memory tokenAddrArr) public {
|
||||||
uint[] memory tokenBalArr = new uint[](tokenAddrArr.length);
|
uint[] memory tokenBalArr = new uint[](tokenAddrArr.length);
|
||||||
|
@ -51,6 +89,30 @@ contract ImportResolver is DSMath {
|
||||||
tokenBalArr
|
tokenBalArr
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function importCTokens(uint toConvert, address[] memory ctokenAddrArr) public {
|
||||||
|
uint[] memory tokenBalArr = new uint[](ctokenAddrArr.length);
|
||||||
|
|
||||||
|
// transfer tokens to InstaDApp smart wallet from user wallet
|
||||||
|
enterMarket(ctokenAddrArr);
|
||||||
|
for (uint i = 0; i < ctokenAddrArr.length; i++) {
|
||||||
|
address erc20 = ctokenAddrArr[i];
|
||||||
|
ERC20Interface tknContract = ERC20Interface(erc20);
|
||||||
|
uint tokenBal = tknContract.balanceOf(msg.sender);
|
||||||
|
tokenBal = toConvert < 10**18 ? wmul(tokenBal, toConvert) : tokenBal;
|
||||||
|
if (tokenBal > 0) {
|
||||||
|
require(tknContract.transferFrom(msg.sender, address(this), tokenBal), "Allowance?");
|
||||||
|
}
|
||||||
|
tokenBalArr[i] = tokenBal;
|
||||||
|
}
|
||||||
|
|
||||||
|
emit LogCTokensImport(
|
||||||
|
msg.sender,
|
||||||
|
toConvert,
|
||||||
|
ctokenAddrArr,
|
||||||
|
tokenBalArr
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user