2019-10-15 13:30:27 +00:00
|
|
|
pragma solidity ^0.5.7;
|
2019-10-10 21:46:09 +00:00
|
|
|
|
2019-10-07 15:15:12 +00:00
|
|
|
interface CTokenInterface {
|
|
|
|
function borrow(uint borrowAmount) external returns (uint);
|
|
|
|
function repayBorrowBehalf(address borrower, uint repayAmount) external returns (uint); // For ERC20
|
|
|
|
function borrowBalanceCurrent(address account) external returns (uint);
|
2019-10-15 13:30:27 +00:00
|
|
|
function exchangeRateCurrent() external returns (uint);
|
2019-10-07 15:15:12 +00:00
|
|
|
function balanceOf(address owner) external view returns (uint256 balance);
|
|
|
|
function transferFrom(address, address, uint) external returns (bool);
|
|
|
|
function underlying() external view returns (address);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CETHInterface {
|
|
|
|
function repayBorrowBehalf(address borrower) external payable; // For ETH
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ERC20Interface {
|
|
|
|
function allowance(address, address) external view returns (uint);
|
|
|
|
function balanceOf(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ComptrollerInterface {
|
|
|
|
function enterMarkets(address[] calldata cTokens) external returns (uint[] memory);
|
|
|
|
function getAssetsIn(address account) external view returns (address[] memory);
|
|
|
|
}
|
|
|
|
|
2019-10-11 09:32:38 +00:00
|
|
|
interface PoolInterface {
|
2019-10-12 10:07:39 +00:00
|
|
|
function accessToken(address[] calldata ctknAddr, uint[] calldata tknAmt, bool isCompound) external;
|
|
|
|
function paybackToken(address[] calldata ctknAddr, bool isCompound) external payable;
|
2019-10-07 15:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract DSMath {
|
|
|
|
|
2019-10-11 09:32:38 +00:00
|
|
|
function sub(uint x, uint y) internal pure returns (uint z) {
|
|
|
|
z = x - y <= x ? x - y : 0;
|
|
|
|
}
|
|
|
|
|
2019-10-07 15:15:12 +00:00
|
|
|
function add(uint x, uint y) internal pure returns (uint z) {
|
|
|
|
require((z = x + y) >= x, "math-not-safe");
|
|
|
|
}
|
|
|
|
|
|
|
|
function mul(uint x, uint y) internal pure returns (uint z) {
|
|
|
|
require(y == 0 || (z = x * y) / y == x, "math-not-safe");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint constant WAD = 10 ** 18;
|
|
|
|
|
|
|
|
function wmul(uint x, uint y) internal pure returns (uint z) {
|
|
|
|
z = add(mul(x, y), WAD / 2) / WAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wdiv(uint x, uint y) internal pure returns (uint z) {
|
|
|
|
z = add(mul(x, WAD), y / 2) / y;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract Helpers is DSMath {
|
2019-10-15 13:30:27 +00:00
|
|
|
/**
|
|
|
|
* @dev get Compound ETH Address
|
|
|
|
*/
|
|
|
|
function getCETHAddress() public pure returns (address cEthAddr) {
|
|
|
|
cEthAddr = 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5; // main
|
|
|
|
}
|
2019-10-07 15:15:12 +00:00
|
|
|
|
2019-10-15 13:30:27 +00:00
|
|
|
/**
|
|
|
|
* @dev get Compound Comptroller Address
|
|
|
|
*/
|
|
|
|
function getComptrollerAddress() public pure returns (address troller) {
|
|
|
|
troller = 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B;
|
|
|
|
}
|
2019-10-07 15:15:12 +00:00
|
|
|
|
2019-10-15 13:30:27 +00:00
|
|
|
/**
|
|
|
|
* @dev get InstaDApp Liquidity Address
|
|
|
|
*/
|
|
|
|
function getPoolAddress() public pure returns (address payable liqAddr) {
|
2019-10-16 14:19:25 +00:00
|
|
|
liqAddr = 0x1564D040EC290C743F67F5cB11f3C1958B39872A;
|
2019-10-15 13:30:27 +00:00
|
|
|
}
|
2019-10-07 15:15:12 +00:00
|
|
|
|
2019-10-11 08:57:30 +00:00
|
|
|
/**
|
|
|
|
* @dev check if the user has entered the market and enter if not
|
|
|
|
*/
|
2019-10-11 09:32:38 +00:00
|
|
|
function enterMarket(address[] memory cErc20) internal {
|
2019-10-15 13:30:27 +00:00
|
|
|
ComptrollerInterface troller = ComptrollerInterface(getComptrollerAddress());
|
2019-10-07 15:15:12 +00:00
|
|
|
address[] memory markets = troller.getAssetsIn(address(this));
|
2019-10-15 20:09:18 +00:00
|
|
|
address[] memory toEnter = new address[](cErc20.length);
|
2019-10-15 13:30:27 +00:00
|
|
|
uint count = 0;
|
2019-10-11 09:32:38 +00:00
|
|
|
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) {
|
2019-10-15 13:30:27 +00:00
|
|
|
toEnter[count] = cErc20[j];
|
|
|
|
count += 1;
|
2019-10-07 15:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-11 09:32:38 +00:00
|
|
|
troller.enterMarkets(toEnter);
|
2019-10-07 15:15:12 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 08:57:30 +00:00
|
|
|
/**
|
2019-10-11 09:32:38 +00:00
|
|
|
* @dev get markets address owner has entered
|
2019-10-11 08:57:30 +00:00
|
|
|
*/
|
2019-10-15 13:30:27 +00:00
|
|
|
function enteredMarkets(address owner) internal view returns (address[] memory) {
|
|
|
|
address[] memory markets = ComptrollerInterface(getComptrollerAddress()).getAssetsIn(owner);
|
2019-10-07 15:15:12 +00:00
|
|
|
return markets;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-11 08:57:30 +00:00
|
|
|
* @dev setting allowance for the "user proxy" if required
|
2019-10-07 15:15:12 +00:00
|
|
|
*/
|
|
|
|
function setApproval(address erc20, uint srcAmt, address to) internal {
|
|
|
|
ERC20Interface erc20Contract = ERC20Interface(erc20);
|
|
|
|
uint tokenAllowance = erc20Contract.allowance(address(this), to);
|
|
|
|
if (srcAmt > tokenAllowance) {
|
2019-10-10 21:46:09 +00:00
|
|
|
erc20Contract.approve(to, uint(-1));
|
2019-10-07 15:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract ImportResolver is Helpers {
|
2019-10-12 10:07:39 +00:00
|
|
|
event LogCompoundImport(address owner, uint percentage, bool isCompound, address[] markets, address[] borrowAddr, uint[] borrowAmt);
|
2019-10-07 15:15:12 +00:00
|
|
|
|
2019-10-16 12:20:42 +00:00
|
|
|
function importAssets(uint toConvert, bool isCompound) external {
|
|
|
|
// subtracting 0.00000001 ETH from initialPoolBal to solve Compound 8 decimal CETH error.
|
2019-10-15 20:09:18 +00:00
|
|
|
uint initialPoolBal = sub(getPoolAddress().balance, 10000000000);
|
2019-10-15 13:30:27 +00:00
|
|
|
address[] memory markets = enteredMarkets(msg.sender);
|
2019-10-16 14:19:25 +00:00
|
|
|
address[] memory borrowAddr = new address[](markets.length);
|
|
|
|
uint[] memory borrowAmt = new uint[](markets.length);
|
2019-10-15 13:30:27 +00:00
|
|
|
uint borrowCount = 0;
|
2019-10-10 21:46:09 +00:00
|
|
|
|
2019-10-11 08:57:30 +00:00
|
|
|
// create an array of borrowed address and amount
|
2019-10-07 15:15:12 +00:00
|
|
|
for (uint i = 0; i < markets.length; i++) {
|
|
|
|
address cErc20 = markets[i];
|
2019-10-10 21:46:09 +00:00
|
|
|
uint toPayback = CTokenInterface(cErc20).borrowBalanceCurrent(msg.sender);
|
2019-10-07 15:15:12 +00:00
|
|
|
toPayback = wmul(toPayback, toConvert);
|
|
|
|
if (toPayback > 0) {
|
2019-10-15 13:30:27 +00:00
|
|
|
borrowAddr[borrowCount] = cErc20;
|
|
|
|
borrowAmt[borrowCount] = toPayback;
|
|
|
|
borrowCount += 1;
|
2019-10-07 15:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 08:57:30 +00:00
|
|
|
// Get liquidity assets to payback user wallet borrowed assets
|
2019-10-15 13:30:27 +00:00
|
|
|
PoolInterface(getPoolAddress()).accessToken(borrowAddr, borrowAmt, isCompound);
|
2019-10-10 21:46:09 +00:00
|
|
|
|
2019-10-15 13:30:27 +00:00
|
|
|
// // payback user wallet borrowed assets
|
2019-10-16 14:19:25 +00:00
|
|
|
for (uint i = 0; i < borrowCount; i++) {
|
2019-10-11 09:32:38 +00:00
|
|
|
address cErc20 = borrowAddr[i];
|
|
|
|
uint toPayback = borrowAmt[i];
|
2019-10-15 13:30:27 +00:00
|
|
|
if (cErc20 == getCETHAddress()) {
|
2019-10-10 21:46:09 +00:00
|
|
|
CETHInterface(cErc20).repayBorrowBehalf.value(toPayback)(msg.sender);
|
|
|
|
} else {
|
|
|
|
CTokenInterface ctknContract = CTokenInterface(cErc20);
|
|
|
|
address erc20 = ctknContract.underlying();
|
|
|
|
setApproval(erc20, toPayback, cErc20);
|
|
|
|
require(ctknContract.repayBorrowBehalf(msg.sender, toPayback) == 0, "transfer approved?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 08:57:30 +00:00
|
|
|
// transfer minted ctokens to InstaDApp smart wallet from user wallet
|
2019-10-07 15:15:12 +00:00
|
|
|
for (uint i = 0; i < markets.length; i++) {
|
|
|
|
address cErc20 = markets[i];
|
|
|
|
CTokenInterface ctknContract = CTokenInterface(cErc20);
|
|
|
|
uint supplyAmt = ctknContract.balanceOf(msg.sender);
|
|
|
|
supplyAmt = wmul(supplyAmt, toConvert);
|
|
|
|
if (supplyAmt > 0) {
|
|
|
|
require(ctknContract.transferFrom(msg.sender, address(this), supplyAmt), "Allowance?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 13:30:27 +00:00
|
|
|
// // borrow and transfer assets to payback liquidity
|
2019-10-11 09:32:38 +00:00
|
|
|
enterMarket(markets);
|
2019-10-16 14:19:25 +00:00
|
|
|
for (uint i = 0; i < borrowCount; i++) {
|
2019-10-11 09:32:38 +00:00
|
|
|
address cErc20 = borrowAddr[i];
|
|
|
|
uint toBorrow = borrowAmt[i];
|
2019-10-07 15:15:12 +00:00
|
|
|
CTokenInterface ctknContract = CTokenInterface(cErc20);
|
2019-10-11 09:32:38 +00:00
|
|
|
require(ctknContract.borrow(toBorrow) == 0, "got collateral?");
|
2019-10-15 13:30:27 +00:00
|
|
|
if (cErc20 == getCETHAddress()) {
|
|
|
|
getPoolAddress().transfer(toBorrow);
|
2019-10-07 15:15:12 +00:00
|
|
|
} else {
|
2019-10-15 13:30:27 +00:00
|
|
|
address erc20 = ctknContract.underlying();
|
|
|
|
require(ERC20Interface(erc20).transfer(getPoolAddress(), toBorrow), "Not-enough-amt");
|
2019-10-07 15:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 21:46:09 +00:00
|
|
|
//payback InstaDApp liquidity
|
2019-10-15 13:30:27 +00:00
|
|
|
PoolInterface(getPoolAddress()).paybackToken(borrowAddr, isCompound);
|
2019-10-15 20:09:18 +00:00
|
|
|
|
|
|
|
uint finalPoolBal = getPoolAddress().balance;
|
|
|
|
assert(finalPoolBal >= initialPoolBal);
|
2019-10-11 09:32:38 +00:00
|
|
|
|
|
|
|
emit LogCompoundImport(
|
|
|
|
msg.sender,
|
|
|
|
toConvert,
|
2019-10-12 10:07:39 +00:00
|
|
|
isCompound,
|
2019-10-11 09:32:38 +00:00
|
|
|
markets,
|
|
|
|
borrowAddr,
|
|
|
|
borrowAmt
|
|
|
|
);
|
2019-10-07 15:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-15 13:30:27 +00:00
|
|
|
contract CompImport is ImportResolver {
|
2019-10-07 15:15:12 +00:00
|
|
|
function() external payable {}
|
|
|
|
}
|