smart-contract/contracts/ProxyLogics/InstaCompound.sol

334 lines
12 KiB
Solidity
Raw Normal View History

2019-05-27 23:06:05 +00:00
pragma solidity ^0.5.7;
2019-05-25 19:57:44 +00:00
2019-05-28 16:54:09 +00:00
interface CTokenInterface {
2019-05-25 19:57:44 +00:00
function redeem(uint redeemTokens) external returns (uint);
function redeemUnderlying(uint redeemAmount) external returns (uint);
function borrow(uint borrowAmount) external returns (uint);
function liquidateBorrow(address borrower, uint repayAmount, address cTokenCollateral) external returns (uint);
function liquidateBorrow(address borrower, address cTokenCollateral) external payable;
2019-05-25 20:35:56 +00:00
function exchangeRateCurrent() external returns (uint);
function getCash() external view returns (uint);
function totalBorrowsCurrent() external returns (uint);
function borrowRatePerBlock() external view returns (uint);
function supplyRatePerBlock() external view returns (uint);
function totalReserves() external view returns (uint);
function reserveFactorMantissa() external view returns (uint);
2019-05-27 23:06:05 +00:00
function totalSupply() external view returns (uint256);
function balanceOf(address owner) external view returns (uint256 balance);
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);
}
2019-05-28 16:54:09 +00:00
interface CERC20Interface {
function mint(uint mintAmount) external returns (uint); // For ERC20
function repayBorrow(uint repayAmount) external returns (uint); // For ERC20
function repayBorrowBehalf(address borrower, uint repayAmount) external returns (uint); // For ERC20
2019-06-01 10:43:16 +00:00
function borrowBalanceCurrent(address account) external returns (uint);
2019-05-28 16:54:09 +00:00
}
interface CETHInterface {
function mint() external payable; // For ETH
function repayBorrow() external payable; // For ETH
function repayBorrowBehalf(address borrower) external payable; // For ETH
2019-06-01 10:43:16 +00:00
function borrowBalanceCurrent(address account) external returns (uint);
2019-05-28 16:54:09 +00:00
}
2019-05-27 23:06:05 +00:00
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 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);
}
2019-05-28 16:54:09 +00:00
contract DSMath {
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-05-31 11:01:28 +00:00
/**
* @dev get ethereum address for trade
*/
function getAddressETH() public pure returns (address eth) {
eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
}
2019-05-27 23:06:05 +00:00
/**
2019-06-01 10:18:39 +00:00
* @dev get Compound Comptroller Address
2019-05-27 23:06:05 +00:00
*/
function getComptrollerAddress() public pure returns (address troller) {
troller = 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B;
2019-06-01 10:18:39 +00:00
// troller = 0x2EAa9D77AE4D8f9cdD9FAAcd44016E746485bddb; // Rinkeby
2019-06-03 19:59:11 +00:00
// troller = 0x3CA5a0E85aD80305c2d2c4982B2f2756f1e747a5; // Kovan
2019-05-27 23:06:05 +00:00
}
2019-05-28 16:54:09 +00:00
/**
2019-06-01 05:07:45 +00:00
* @dev Transfer ETH/ERC20 to user
2019-05-28 16:54:09 +00:00
*/
2019-05-31 13:43:52 +00:00
function transferToken(address erc20) internal {
if (erc20 == getAddressETH()) {
msg.sender.transfer(address(this).balance);
} else {
ERC20Interface erc20Contract = ERC20Interface(erc20);
uint srcBal = erc20Contract.balanceOf(address(this));
if (srcBal > 0) {
erc20Contract.transfer(msg.sender, srcBal);
}
}
}
2019-06-03 19:59:11 +00:00
function enterMarket(address cErc20) internal {
ComptrollerInterface troller = ComptrollerInterface(getComptrollerAddress());
address[] memory markets = troller.getAssetsIn(address(this));
bool isEntered = false;
for (uint i = 0; i < markets.length; i++) {
if (markets[i] == cErc20) {
isEntered = true;
}
}
if (!isEntered) {
address[] memory toEnter = new address[](1);
toEnter[0] = cErc20;
troller.enterMarkets(toEnter);
}
}
2019-06-20 13:41:02 +00:00
/**
* @dev setting allowance to compound for the "user proxy" if required
*/
function setApproval(address erc20, uint srcAmt, address to) internal {
ERC20Interface erc20Contract = ERC20Interface(erc20);
uint tokenAllowance = erc20Contract.allowance(address(this), to);
if (srcAmt > tokenAllowance) {
erc20Contract.approve(to, 2**255);
}
}
2019-05-27 23:06:05 +00:00
}
2019-05-28 16:54:09 +00:00
contract CompoundResolver is Helpers {
2019-05-31 13:43:52 +00:00
event LogMint(address erc20, address cErc20, uint tokenAmt, address owner);
event LogRedeem(address erc20, address cErc20, uint tokenAmt, address owner);
event LogBorrow(address erc20, address cErc20, uint tokenAmt, address owner);
event LogRepay(address erc20, address cErc20, uint tokenAmt, address owner);
2019-10-12 11:53:17 +00:00
event LogRepayBehalf(address borrower, address erc20, address cErc20, uint tokenAmt, address owner);
2019-05-31 13:43:52 +00:00
2019-06-01 10:18:39 +00:00
/**
* @dev Deposit ETH/ERC20 and mint Compound Tokens
*/
2019-05-31 11:01:28 +00:00
function mintCToken(address erc20, address cErc20, uint tokenAmt) external payable {
2019-06-03 19:59:11 +00:00
enterMarket(cErc20);
2019-05-31 11:01:28 +00:00
if (erc20 == getAddressETH()) {
CETHInterface cToken = CETHInterface(cErc20);
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);
2019-06-01 10:18:39 +00:00
setApproval(erc20, toDeposit, cErc20);
2019-05-31 11:01:28 +00:00
assert(cToken.mint(toDeposit) == 0);
2019-05-28 16:54:09 +00:00
}
2019-05-31 13:43:52 +00:00
emit LogMint(
erc20,
cErc20,
tokenAmt,
2019-10-05 08:22:34 +00:00
address(this)
2019-05-31 13:43:52 +00:00
);
2019-05-28 16:54:09 +00:00
}
2019-06-01 10:18:39 +00:00
/**
* @dev Redeem ETH/ERC20 and burn Compound Tokens
* @param cTokenAmt Amount of CToken To burn
*/
2019-05-31 11:01:28 +00:00
function redeemCToken(address erc20, address cErc20, uint cTokenAmt) external {
2019-05-28 16:54:09 +00:00
CTokenInterface cToken = CTokenInterface(cErc20);
uint toBurn = cToken.balanceOf(address(this));
if (toBurn > cTokenAmt) {
toBurn = cTokenAmt;
}
2019-06-01 10:18:39 +00:00
setApproval(cErc20, toBurn, cErc20);
2019-05-28 16:54:09 +00:00
require(cToken.redeem(toBurn) == 0, "something went wrong");
2019-05-31 11:01:28 +00:00
transferToken(erc20);
2019-06-03 11:55:51 +00:00
uint tokenReturned = wmul(toBurn, cToken.exchangeRateCurrent());
2019-05-31 13:43:52 +00:00
emit LogRedeem(
erc20,
cErc20,
tokenReturned,
address(this)
);
2019-05-28 16:54:09 +00:00
}
2019-06-01 10:18:39 +00:00
/**
* @dev Redeem ETH/ERC20 and mint Compound Tokens
* @param tokenAmt Amount of token To Redeem
*/
2019-05-31 11:01:28 +00:00
function redeemUnderlying(address erc20, address cErc20, uint tokenAmt) external {
2019-05-28 16:54:09 +00:00
CTokenInterface cToken = CTokenInterface(cErc20);
2019-06-01 10:18:39 +00:00
setApproval(cErc20, 10**50, cErc20);
2019-06-03 12:12:16 +00:00
uint toBurn = cToken.balanceOf(address(this));
uint tokenToReturn = wmul(toBurn, cToken.exchangeRateCurrent());
if (tokenToReturn > tokenAmt) {
tokenToReturn = tokenAmt;
}
require(cToken.redeemUnderlying(tokenToReturn) == 0, "something went wrong");
2019-05-31 11:01:28 +00:00
transferToken(erc20);
2019-05-31 13:43:52 +00:00
emit LogRedeem(
erc20,
cErc20,
2019-06-03 12:59:24 +00:00
tokenToReturn,
2019-05-31 13:43:52 +00:00
address(this)
);
2019-05-28 16:54:09 +00:00
}
2019-06-01 10:18:39 +00:00
/**
* @dev borrow ETH/ERC20
*/
2019-05-28 16:54:09 +00:00
function borrow(address erc20, address cErc20, uint tokenAmt) external {
2019-06-03 19:59:11 +00:00
enterMarket(cErc20);
2019-05-28 16:54:09 +00:00
require(CTokenInterface(cErc20).borrow(tokenAmt) == 0, "got collateral?");
2019-05-31 11:01:28 +00:00
transferToken(erc20);
2019-05-31 13:43:52 +00:00
emit LogBorrow(
erc20,
cErc20,
tokenAmt,
address(this)
);
2019-05-31 11:01:28 +00:00
}
2019-06-01 10:18:39 +00:00
/**
* @dev Pay Debt ETH/ERC20
*/
2019-05-31 11:01:28 +00:00
function repayToken(address erc20, address cErc20, uint tokenAmt) external payable {
if (erc20 == getAddressETH()) {
CETHInterface cToken = CETHInterface(cErc20);
2019-06-01 10:43:16 +00:00
uint toRepay = msg.value;
uint borrows = cToken.borrowBalanceCurrent(address(this));
if (toRepay > borrows) {
toRepay = borrows;
msg.sender.transfer(msg.value - toRepay);
}
2019-06-03 12:12:16 +00:00
cToken.repayBorrow.value(toRepay)();
emit LogRepay(
erc20,
cErc20,
toRepay,
address(this)
);
2019-05-31 11:01:28 +00:00
} else {
CERC20Interface cToken = CERC20Interface(cErc20);
ERC20Interface token = ERC20Interface(erc20);
uint toRepay = token.balanceOf(msg.sender);
2019-06-01 10:43:16 +00:00
uint borrows = cToken.borrowBalanceCurrent(address(this));
2019-05-31 11:01:28 +00:00
if (toRepay > tokenAmt) {
toRepay = tokenAmt;
}
2019-06-01 10:43:16 +00:00
if (toRepay > borrows) {
toRepay = borrows;
}
2019-06-01 10:18:39 +00:00
setApproval(erc20, toRepay, cErc20);
2019-05-31 11:01:28 +00:00
token.transferFrom(msg.sender, address(this), toRepay);
require(cToken.repayBorrow(toRepay) == 0, "transfer approved?");
2019-06-03 12:12:16 +00:00
emit LogRepay(
erc20,
cErc20,
toRepay,
address(this)
);
2019-05-28 16:54:09 +00:00
}
}
2019-06-01 10:18:39 +00:00
/**
* @dev Pay Debt for someone else
*/
2019-05-28 16:54:09 +00:00
function repaytokenBehalf(
address borrower,
address erc20,
address cErc20,
uint tokenAmt
) external payable
{
2019-05-31 11:01:28 +00:00
if (erc20 == getAddressETH()) {
2019-05-31 13:47:56 +00:00
CETHInterface cToken = CETHInterface(cErc20);
2019-06-01 10:43:16 +00:00
uint toRepay = msg.value;
2019-10-12 11:53:17 +00:00
uint borrows = cToken.borrowBalanceCurrent(borrower);
2019-06-01 10:43:16 +00:00
if (toRepay > borrows) {
toRepay = borrows;
msg.sender.transfer(msg.value - toRepay);
}
2019-06-03 12:12:16 +00:00
cToken.repayBorrowBehalf.value(toRepay)(borrower);
2019-10-12 11:53:17 +00:00
emit LogRepayBehalf(
borrower,
2019-06-03 12:12:16 +00:00
erc20,
cErc20,
toRepay,
address(this)
);
2019-05-31 11:01:28 +00:00
} else {
CERC20Interface cToken = CERC20Interface(cErc20);
ERC20Interface token = ERC20Interface(erc20);
uint toRepay = token.balanceOf(msg.sender);
2019-10-12 11:53:17 +00:00
uint borrows = cToken.borrowBalanceCurrent(borrower);
2019-05-31 11:01:28 +00:00
if (toRepay > tokenAmt) {
toRepay = tokenAmt;
}
2019-06-01 10:43:16 +00:00
if (toRepay > borrows) {
toRepay = borrows;
2019-05-31 11:01:28 +00:00
}
2019-06-01 10:43:16 +00:00
setApproval(erc20, toRepay, cErc20);
2019-05-31 11:01:28 +00:00
token.transferFrom(msg.sender, address(this), toRepay);
2019-10-12 16:20:54 +00:00
require(cToken.repayBorrowBehalf(borrower, toRepay) == 0, "transfer approved?");
2019-10-12 11:53:17 +00:00
emit LogRepayBehalf(
borrower,
2019-06-03 12:12:16 +00:00
erc20,
cErc20,
toRepay,
address(this)
);
2019-05-28 16:54:09 +00:00
}
}
2019-05-31 11:01:28 +00:00
}
contract InstaCompound is CompoundResolver {
2019-06-01 10:18:39 +00:00
function() external payable {}
2019-05-28 16:54:09 +00:00
}