mirror of
https://github.com/Instadapp/smart-contract.git
synced 2024-07-29 22:08:07 +00:00
compound save almost complete
This commit is contained in:
parent
57b3ed1a37
commit
c6725a9ae6
|
@ -13,6 +13,7 @@ interface CTokenInterface {
|
||||||
function supplyRatePerBlock() external view returns (uint);
|
function supplyRatePerBlock() external view returns (uint);
|
||||||
function totalReserves() external view returns (uint);
|
function totalReserves() external view returns (uint);
|
||||||
function reserveFactorMantissa() external view returns (uint);
|
function reserveFactorMantissa() external view returns (uint);
|
||||||
|
function borrowBalanceCurrent(address account) external returns (uint);
|
||||||
|
|
||||||
function totalSupply() external view returns (uint256);
|
function totalSupply() external view returns (uint256);
|
||||||
function balanceOf(address owner) external view returns (uint256 balance);
|
function balanceOf(address owner) external view returns (uint256 balance);
|
||||||
|
@ -51,6 +52,16 @@ interface ComptrollerInterface {
|
||||||
function getAccountLiquidity(address account) external view returns (uint, uint, uint);
|
function getAccountLiquidity(address account) external view returns (uint, uint, uint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface CompOracleInterface {
|
||||||
|
function getUnderlyingPrice(address) external view returns (uint);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SplitSwapInterface {
|
||||||
|
function getBest(address src, address dest, uint srcAmt) external view returns (uint bestExchange, uint destAmt);
|
||||||
|
function ethToDaiSwap(uint splitAmt, uint slippageAmt) external payable returns (uint destAmt);
|
||||||
|
function daiToEthSwap(uint srcAmt, uint splitAmt, uint slippageAmt) external returns (uint destAmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
contract DSMath {
|
contract DSMath {
|
||||||
|
|
||||||
|
@ -58,6 +69,10 @@ contract DSMath {
|
||||||
require((z = x + y) >= x, "math-not-safe");
|
require((z = x + y) >= x, "math-not-safe");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sub(uint x, uint y) internal pure returns (uint z) {
|
||||||
|
z = x - y <= x ? x - y : 0;
|
||||||
|
}
|
||||||
|
|
||||||
function mul(uint x, uint y) internal pure returns (uint z) {
|
function mul(uint x, uint y) internal pure returns (uint z) {
|
||||||
require(y == 0 || (z = x * y) / y == x, "math-not-safe");
|
require(y == 0 || (z = x * y) / y == x, "math-not-safe");
|
||||||
}
|
}
|
||||||
|
@ -84,6 +99,13 @@ contract Helpers is DSMath {
|
||||||
eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev get ethereum address for trade
|
||||||
|
*/
|
||||||
|
function getAddressDAI() public pure returns (address dai) {
|
||||||
|
dai = 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev get Compound Comptroller Address
|
* @dev get Compound Comptroller Address
|
||||||
*/
|
*/
|
||||||
|
@ -92,18 +114,31 @@ contract Helpers is DSMath {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Transfer ETH/ERC20 to user
|
* @dev get Compound Comptroller Address
|
||||||
*/
|
*/
|
||||||
function transferToken(address erc20) internal {
|
function getCompOracleAddress() public pure returns (address troller) {
|
||||||
if (erc20 == getAddressETH()) {
|
troller = 0xe7664229833AE4Abf4E269b8F23a86B657E2338D;
|
||||||
msg.sender.transfer(address(this).balance);
|
}
|
||||||
} else {
|
|
||||||
ERC20Interface erc20Contract = ERC20Interface(erc20);
|
/**
|
||||||
uint srcBal = erc20Contract.balanceOf(address(this));
|
* @dev get Compound Comptroller Address
|
||||||
if (srcBal > 0) {
|
*/
|
||||||
erc20Contract.transfer(msg.sender, srcBal);
|
function getCETHAddress() public pure returns (address cEth) {
|
||||||
}
|
cEth = 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev get Compound Comptroller Address
|
||||||
|
*/
|
||||||
|
function getCDAIAddress() public pure returns (address cDai) {
|
||||||
|
cDai = 0xF5DCe57282A584D2746FaF1593d3121Fcac444dC;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev get admin address
|
||||||
|
*/
|
||||||
|
function getAddressSplitSwap() public pure returns (address payable splitSwap) {
|
||||||
|
splitSwap = 0xa4BCA645f9cB9e6F9ad8C56D90a65b07C2f4e1Dd;
|
||||||
}
|
}
|
||||||
|
|
||||||
function enterMarket(address cErc20) internal {
|
function enterMarket(address cErc20) internal {
|
||||||
|
@ -136,7 +171,62 @@ contract Helpers is DSMath {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract CompoundResolver is Helpers {
|
contract CompoundHelper is Helpers {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev get user's token supply and borrow in ETH
|
||||||
|
*/
|
||||||
|
function compSupplyBorrow(address cTokenAdd, address user) public returns(uint supplyInEth, uint borrowInEth) {
|
||||||
|
CTokenInterface cTokenContract = CTokenInterface(cTokenAdd);
|
||||||
|
uint tokenPriceInEth = CompOracleInterface(getCompOracleAddress()).getUnderlyingPrice(cTokenAdd);
|
||||||
|
uint cTokenBal = sub(cTokenContract.balanceOf(user), 1);
|
||||||
|
uint cTokenExchangeRate = cTokenContract.exchangeRateCurrent();
|
||||||
|
uint tokenSupply = sub(wmul(cTokenBal, cTokenExchangeRate), 1);
|
||||||
|
supplyInEth = sub(wmul(tokenSupply, tokenPriceInEth), 10);
|
||||||
|
uint tokenBorrowed = cTokenContract.borrowBalanceCurrent(user);
|
||||||
|
borrowInEth = add(wmul(tokenBorrowed, tokenPriceInEth), 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev get users overall details for Compound
|
||||||
|
*/
|
||||||
|
function getCompStats(
|
||||||
|
address user,
|
||||||
|
address[] memory cTokenAddr,
|
||||||
|
uint[] memory cTokenFactor
|
||||||
|
) public returns (uint totalSupply, uint totalBorrow, uint maxBorrow, uint borrowRemain, uint maxWithdraw, uint ratio)
|
||||||
|
{
|
||||||
|
for (uint i = 0; i < cTokenAddr.length; i++) {
|
||||||
|
address cTokenAdd = cTokenAddr[i];
|
||||||
|
uint factor = cTokenFactor[i];
|
||||||
|
(uint supplyInEth, uint borrowInEth) = compSupplyBorrow(cTokenAdd, user);
|
||||||
|
totalSupply += supplyInEth;
|
||||||
|
totalBorrow += borrowInEth;
|
||||||
|
maxBorrow += wmul(supplyInEth, factor);
|
||||||
|
}
|
||||||
|
borrowRemain = sub(maxBorrow, totalBorrow);
|
||||||
|
maxWithdraw = wdiv(borrowRemain, 750000000000000000); // divide it by 0.75 (ETH Factor)
|
||||||
|
uint userEthSupply = getEthSupply(user);
|
||||||
|
maxWithdraw = userEthSupply > maxWithdraw ? maxWithdraw : userEthSupply;
|
||||||
|
ratio = wdiv(totalBorrow, totalSupply);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getEthSupply(address user) internal returns (uint ethSupply) {
|
||||||
|
CTokenInterface cTokenContract = CTokenInterface(getCETHAddress());
|
||||||
|
uint cTokenBal = sub(cTokenContract.balanceOf(user), 1);
|
||||||
|
uint cTokenExchangeRate = cTokenContract.exchangeRateCurrent();
|
||||||
|
ethSupply = wmul(cTokenBal, cTokenExchangeRate);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDaiRemainBorrow(uint daiInEth) internal view returns (uint daiAmt) {
|
||||||
|
uint tokenPriceInEth = CompOracleInterface(getCompOracleAddress()).getUnderlyingPrice(getCDAIAddress());
|
||||||
|
daiAmt = sub(wdiv(daiInEth, tokenPriceInEth), 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
contract CompoundResolver is CompoundHelper {
|
||||||
|
|
||||||
event LogMint(address erc20, address cErc20, uint tokenAmt, address owner);
|
event LogMint(address erc20, address cErc20, uint tokenAmt, address owner);
|
||||||
event LogRedeem(address erc20, address cErc20, uint tokenAmt, address owner);
|
event LogRedeem(address erc20, address cErc20, uint tokenAmt, address owner);
|
||||||
|
@ -146,70 +236,30 @@ contract CompoundResolver is Helpers {
|
||||||
/**
|
/**
|
||||||
* @dev Deposit ETH/ERC20 and mint Compound Tokens
|
* @dev Deposit ETH/ERC20 and mint Compound Tokens
|
||||||
*/
|
*/
|
||||||
function mintCToken(address erc20, address cErc20, uint tokenAmt) external payable {
|
function mintCEth(uint tokenAmt) internal {
|
||||||
enterMarket(cErc20);
|
enterMarket(getAddressETH());
|
||||||
if (erc20 == getAddressETH()) {
|
CETHInterface cToken = CETHInterface(getAddressETH());
|
||||||
CETHInterface cToken = CETHInterface(cErc20);
|
cToken.mint.value(tokenAmt)();
|
||||||
cToken.mint.value(msg.value)();
|
|
||||||
} else {
|
|
||||||
ERC20Interface token = ERC20Interface(erc20);
|
|
||||||
uint toDeposit = token.balanceOf(msg.sender);
|
|
||||||
if (toDeposit > tokenAmt) {
|
|
||||||
toDeposit = tokenAmt;
|
|
||||||
}
|
|
||||||
token.transferFrom(msg.sender, address(this), toDeposit);
|
|
||||||
CERC20Interface cToken = CERC20Interface(cErc20);
|
|
||||||
setApproval(erc20, toDeposit, cErc20);
|
|
||||||
assert(cToken.mint(toDeposit) == 0);
|
|
||||||
}
|
|
||||||
emit LogMint(
|
emit LogMint(
|
||||||
erc20,
|
getAddressETH(),
|
||||||
cErc20,
|
getAddressETH(),
|
||||||
tokenAmt,
|
tokenAmt,
|
||||||
msg.sender
|
msg.sender
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Redeem ETH/ERC20 and burn Compound Tokens
|
|
||||||
* @param cTokenAmt Amount of CToken To burn
|
|
||||||
*/
|
|
||||||
function redeemCToken(address erc20, address cErc20, uint cTokenAmt) external {
|
|
||||||
CTokenInterface cToken = CTokenInterface(cErc20);
|
|
||||||
uint toBurn = cToken.balanceOf(address(this));
|
|
||||||
if (toBurn > cTokenAmt) {
|
|
||||||
toBurn = cTokenAmt;
|
|
||||||
}
|
|
||||||
setApproval(cErc20, toBurn, cErc20);
|
|
||||||
require(cToken.redeem(toBurn) == 0, "something went wrong");
|
|
||||||
transferToken(erc20);
|
|
||||||
uint tokenReturned = wmul(toBurn, cToken.exchangeRateCurrent());
|
|
||||||
emit LogRedeem(
|
|
||||||
erc20,
|
|
||||||
cErc20,
|
|
||||||
tokenReturned,
|
|
||||||
address(this)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Redeem ETH/ERC20 and mint Compound Tokens
|
* @dev Redeem ETH/ERC20 and mint Compound Tokens
|
||||||
* @param tokenAmt Amount of token To Redeem
|
* @param tokenAmt Amount of token To Redeem
|
||||||
*/
|
*/
|
||||||
function redeemUnderlying(address erc20, address cErc20, uint tokenAmt) external {
|
function redeemEth(uint tokenAmt) internal {
|
||||||
CTokenInterface cToken = CTokenInterface(cErc20);
|
CTokenInterface cToken = CTokenInterface(getCETHAddress());
|
||||||
setApproval(cErc20, 10**50, cErc20);
|
setApproval(getCETHAddress(), 10**30, getCETHAddress());
|
||||||
uint toBurn = cToken.balanceOf(address(this));
|
require(cToken.redeemUnderlying(tokenAmt) == 0, "something went wrong");
|
||||||
uint tokenToReturn = wmul(toBurn, cToken.exchangeRateCurrent());
|
|
||||||
if (tokenToReturn > tokenAmt) {
|
|
||||||
tokenToReturn = tokenAmt;
|
|
||||||
}
|
|
||||||
require(cToken.redeemUnderlying(tokenToReturn) == 0, "something went wrong");
|
|
||||||
transferToken(erc20);
|
|
||||||
emit LogRedeem(
|
emit LogRedeem(
|
||||||
erc20,
|
getAddressETH(),
|
||||||
cErc20,
|
getCETHAddress(),
|
||||||
tokenToReturn,
|
tokenAmt,
|
||||||
address(this)
|
address(this)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -217,13 +267,12 @@ contract CompoundResolver is Helpers {
|
||||||
/**
|
/**
|
||||||
* @dev borrow ETH/ERC20
|
* @dev borrow ETH/ERC20
|
||||||
*/
|
*/
|
||||||
function borrow(address erc20, address cErc20, uint tokenAmt) external {
|
function borrow(uint tokenAmt) internal {
|
||||||
enterMarket(cErc20);
|
enterMarket(getCDAIAddress());
|
||||||
require(CTokenInterface(cErc20).borrow(tokenAmt) == 0, "got collateral?");
|
require(CTokenInterface(getCDAIAddress()).borrow(tokenAmt) == 0, "got collateral?");
|
||||||
transferToken(erc20);
|
|
||||||
emit LogBorrow(
|
emit LogBorrow(
|
||||||
erc20,
|
getAddressDAI(),
|
||||||
cErc20,
|
getCDAIAddress(),
|
||||||
tokenAmt,
|
tokenAmt,
|
||||||
address(this)
|
address(this)
|
||||||
);
|
);
|
||||||
|
@ -232,43 +281,57 @@ contract CompoundResolver is Helpers {
|
||||||
/**
|
/**
|
||||||
* @dev Pay Debt ETH/ERC20
|
* @dev Pay Debt ETH/ERC20
|
||||||
*/
|
*/
|
||||||
function repayToken(address erc20, address cErc20, uint tokenAmt) external payable {
|
function repayDai(uint tokenAmt) internal {
|
||||||
if (erc20 == getAddressETH()) {
|
CERC20Interface cToken = CERC20Interface(getCDAIAddress());
|
||||||
CETHInterface cToken = CETHInterface(cErc20);
|
setApproval(getAddressDAI(), tokenAmt, getCDAIAddress());
|
||||||
uint toRepay = msg.value;
|
require(cToken.repayBorrow(tokenAmt) == 0, "transfer approved?");
|
||||||
uint borrows = cToken.borrowBalanceCurrent(address(this));
|
emit LogRepay(
|
||||||
if (toRepay > borrows) {
|
getAddressDAI(),
|
||||||
toRepay = borrows;
|
getCDAIAddress(),
|
||||||
msg.sender.transfer(msg.value - toRepay);
|
tokenAmt,
|
||||||
}
|
address(this)
|
||||||
cToken.repayBorrow.value(toRepay)();
|
);
|
||||||
emit LogRepay(
|
}
|
||||||
erc20,
|
|
||||||
cErc20,
|
}
|
||||||
toRepay,
|
|
||||||
address(this)
|
|
||||||
);
|
contract CompoundSave is CompoundResolver {
|
||||||
} else {
|
|
||||||
CERC20Interface cToken = CERC20Interface(cErc20);
|
event LogSaveCompound(uint srcETH, uint destDAI);
|
||||||
ERC20Interface token = ERC20Interface(erc20);
|
|
||||||
uint toRepay = token.balanceOf(msg.sender);
|
event LogLeverageCompound(uint srcDAI,uint destETH);
|
||||||
uint borrows = cToken.borrowBalanceCurrent(address(this));
|
|
||||||
if (toRepay > tokenAmt) {
|
function save(
|
||||||
toRepay = tokenAmt;
|
uint ethToFree,
|
||||||
}
|
address[] memory cTokenAddr,
|
||||||
if (toRepay > borrows) {
|
uint[] memory ctokenFactor,
|
||||||
toRepay = borrows;
|
uint splitAmt,
|
||||||
}
|
uint slippageAmt
|
||||||
setApproval(erc20, toRepay, cErc20);
|
) public
|
||||||
token.transferFrom(msg.sender, address(this), toRepay);
|
{
|
||||||
require(cToken.repayBorrow(toRepay) == 0, "transfer approved?");
|
(,,,,uint maxWithdraw,) = getCompStats(address(this), cTokenAddr, ctokenFactor);
|
||||||
emit LogRepay(
|
uint ethToSwap = ethToFree < maxWithdraw ? ethToFree : maxWithdraw;
|
||||||
erc20,
|
redeemEth(ethToSwap);
|
||||||
cErc20,
|
uint destAmt = SplitSwapInterface(getAddressSplitSwap()).ethToDaiSwap.value(ethToSwap)(splitAmt, slippageAmt);
|
||||||
toRepay,
|
repayDai(destAmt);
|
||||||
address(this)
|
emit LogSaveCompound(ethToSwap, destAmt);
|
||||||
);
|
}
|
||||||
}
|
|
||||||
|
function leverage(
|
||||||
|
uint daiToBorrow,
|
||||||
|
address[] memory cTokenAddr,
|
||||||
|
uint[] memory ctokenFactor,
|
||||||
|
uint splitAmt,
|
||||||
|
uint slippageAmt
|
||||||
|
) public
|
||||||
|
{
|
||||||
|
(,,,uint borrowRemain,,) = getCompStats(address(this), cTokenAddr, ctokenFactor);
|
||||||
|
uint daiToSwap = getDaiRemainBorrow(borrowRemain);
|
||||||
|
daiToSwap = daiToSwap < daiToBorrow ? daiToSwap : daiToBorrow;
|
||||||
|
borrow(daiToSwap);
|
||||||
|
uint destAmt = SplitSwapInterface(getAddressSplitSwap()).daiToEthSwap(daiToSwap, splitAmt, slippageAmt);
|
||||||
|
mintCEth(destAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -153,7 +153,7 @@ contract Helpers is DSMath {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev get ethereum address for trade
|
* @dev get dai address for trade
|
||||||
*/
|
*/
|
||||||
function getAddressDAI() public pure returns (address dai) {
|
function getAddressDAI() public pure returns (address dai) {
|
||||||
dai = 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359;
|
dai = 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user