smart-contract/contracts/ProxyLogics/InstaMakerCompBridge.sol
2019-09-16 12:47:56 -07:00

610 lines
21 KiB
Solidity

pragma solidity ^0.5.7;
interface TubInterface {
function open() external returns (bytes32);
function join(uint) external;
function exit(uint) external;
function lock(bytes32, uint) external;
function free(bytes32, uint) external;
function draw(bytes32, uint) external;
function wipe(bytes32, uint) external;
function give(bytes32, address) external;
function shut(bytes32) external;
function cups(bytes32) external view returns (address, uint, uint, uint);
function gem() external view returns (TokenInterface);
function gov() external view returns (TokenInterface);
function skr() external view returns (TokenInterface);
function sai() external view returns (TokenInterface);
function ink(bytes32) external view returns (uint);
function tab(bytes32) external returns (uint);
function rap(bytes32) external returns (uint);
function per() external view returns (uint);
function pep() external view returns (PepInterface);
}
interface TokenInterface {
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);
function deposit() external payable;
function withdraw(uint) external;
}
interface PepInterface {
function peek() external returns (bytes32, bool);
}
interface MakerOracleInterface {
function read() external view returns (bytes32);
}
interface UniswapExchange {
function getEthToTokenOutputPrice(uint256 tokensBought) external view returns (uint256 ethSold);
function getTokenToEthOutputPrice(uint256 ethBought) external view returns (uint256 tokensSold);
function tokenToTokenSwapOutput(
uint256 tokensBought,
uint256 maxTokensSold,
uint256 maxEthSold,
uint256 deadline,
address tokenAddr
) external returns (uint256 tokensSold);
}
interface LiquidityInterface {
function redeemTknAndTransfer(address tknAddr, address ctknAddr, uint tknAmt) external;
function mintTknBack(address tknAddr, address ctknAddr, uint tknAmt) external;
function borrowTknAndTransfer(address tknAddr, address ctknAddr, uint tknAmt) external;
function payBorrowBack(address tknAddr, address ctknAddr, uint tknAmt) external;
}
interface CTokenInterface {
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;
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);
function borrowBalanceCurrent(address account) external returns (uint);
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);
}
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
function borrowBalanceCurrent(address account) external returns (uint);
}
interface CETHInterface {
function mint() external payable; // For ETH
function repayBorrow() external payable; // For ETH
function repayBorrowBehalf(address borrower) external payable; // For ETH
function borrowBalanceCurrent(address account) external returns (uint);
}
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);
}
interface CompOracleInterface {
function getUnderlyingPrice(address) external view returns (uint);
}
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;
uint constant RAY = 10 ** 27;
function rmul(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, y), RAY / 2) / RAY;
}
function rdiv(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, RAY), y / 2) / y;
}
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 Helper is DSMath {
/**
* @dev get ethereum address for trade
*/
function getAddressETH() public pure returns (address eth) {
eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
}
/**
* @dev get MakerDAO CDP engine
*/
function getSaiTubAddress() public pure returns (address sai) {
sai = 0x448a5065aeBB8E423F0896E6c5D525C040f59af3;
}
/**
* @dev get MakerDAO Oracle for ETH price
*/
function getOracleAddress() public pure returns (address oracle) {
oracle = 0x729D19f657BD0614b4985Cf1D82531c67569197B;
}
/**
* @dev get uniswap MKR exchange
*/
function getUniswapMKRExchange() public pure returns (address ume) {
ume = 0x2C4Bd064b998838076fa341A83d007FC2FA50957;
}
/**
* @dev get uniswap DAI exchange
*/
function getUniswapDAIExchange() public pure returns (address ude) {
ude = 0x09cabEC1eAd1c0Ba254B09efb3EE13841712bE14;
}
/**
* @dev get InstaDApp Liquidity contract
*/
function getLiquidityAddr() public pure returns (address liquidity) {
liquidity = 0x22BE7F22E7ca2D4949d2B369d02bC9283CE7d285;
}
/**
* @dev get Compound Comptroller Address
*/
function getComptrollerAddress() public pure returns (address troller) {
troller = 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B;
}
/**
* @dev get Compound Oracle Address
*/
function getCompOracleAddress() public pure returns (address troller) {
troller = 0xe7664229833AE4Abf4E269b8F23a86B657E2338D;
}
/**
* @dev get CETH Address
*/
function getCETHAddress() public pure returns (address cEth) {
cEth = 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5;
}
/**
* @dev get DAI Address
*/
function getDAIAddress() public pure returns (address dai) {
dai = 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359;
}
/**
* @dev get CDAI Address
*/
function getCDAIAddress() public pure returns (address cDai) {
cDai = 0xF5DCe57282A584D2746FaF1593d3121Fcac444dC;
}
/**
* @dev setting allowance to compound contracts for the "user proxy" if required
*/
function setApproval(address erc20, uint srcAmt, address to) internal {
TokenInterface erc20Contract = TokenInterface(erc20);
uint tokenAllowance = erc20Contract.allowance(address(this), to);
if (srcAmt > tokenAllowance) {
erc20Contract.approve(to, uint(-1));
}
}
}
contract MakerHelper is Helper {
event LogOpen(uint cdpNum, address owner);
event LogLock(uint cdpNum, uint amtETH, uint amtPETH, address owner);
event LogFree(uint cdpNum, uint amtETH, uint amtPETH, address owner);
event LogDraw(uint cdpNum, uint amtDAI, address owner);
event LogWipe(uint cdpNum, uint daiAmt, uint mkrFee, uint daiFee, address owner);
event LogShut(uint cdpNum);
/**
* @dev Allowance to Maker's contract
*/
function setMakerAllowance(TokenInterface _token, address _spender) internal {
if (_token.allowance(address(this), _spender) != uint(-1)) {
_token.approve(_spender, uint(-1));
}
}
/**
* @dev CDP stats by Bytes
*/
function getCDPStats(bytes32 cup) internal view returns (uint ethCol, uint daiDebt) {
TubInterface tub = TubInterface(getSaiTubAddress());
(, uint pethCol, uint debt,) = tub.cups(cup);
ethCol = rmul(pethCol, tub.per()); // get ETH col from PETH col
daiDebt = debt;
}
}
contract CompoundHelper is MakerHelper {
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);
/**
* @dev Compound Enter Market which allows borrowing
*/
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);
}
}
}
contract MakerResolver is CompoundHelper {
/**
* @dev Open new CDP
*/
function open() internal returns (uint) {
bytes32 cup = TubInterface(getSaiTubAddress()).open();
emit LogOpen(uint(cup), address(this));
return uint(cup);
}
/**
* @dev transfer CDP ownership
*/
function give(uint cdpNum, address nextOwner) internal {
TubInterface(getSaiTubAddress()).give(bytes32(cdpNum), nextOwner);
}
/**
* @dev Pay CDP debt
*/
function wipe(uint cdpNum, uint _wad) internal returns (uint daiAmt) {
if (_wad > 0) {
TubInterface tub = TubInterface(getSaiTubAddress());
UniswapExchange daiEx = UniswapExchange(getUniswapDAIExchange());
UniswapExchange mkrEx = UniswapExchange(getUniswapMKRExchange());
TokenInterface dai = tub.sai();
TokenInterface mkr = tub.gov();
bytes32 cup = bytes32(cdpNum);
(address lad,,,) = tub.cups(cup);
require(lad == address(this), "cup-not-owned");
setMakerAllowance(dai, getSaiTubAddress());
setMakerAllowance(mkr, getSaiTubAddress());
setMakerAllowance(dai, getUniswapDAIExchange());
(bytes32 val, bool ok) = tub.pep().peek();
// MKR required for wipe = Stability fees accrued in Dai / MKRUSD value
uint mkrFee = wdiv(rmul(_wad, rdiv(tub.rap(cup), tub.tab(cup))), uint(val));
uint daiFeeAmt = daiEx.getTokenToEthOutputPrice(mkrEx.getEthToTokenOutputPrice(mkrFee));
daiAmt = add(_wad, daiFeeAmt);
// Getting Liquidity from Liquidity Contract
LiquidityInterface(getLiquidityAddr()).borrowTknAndTransfer(getDAIAddress(), getCDAIAddress(), daiAmt);
if (ok && val != 0) {
daiEx.tokenToTokenSwapOutput(
mkrFee,
daiAmt,
uint(999000000000000000000),
uint(1899063809), // 6th March 2030 GMT // no logic
address(mkr)
);
}
tub.wipe(cup, _wad);
emit LogWipe(
cdpNum,
daiAmt,
mkrFee,
daiFeeAmt,
address(this)
);
}
}
/**
* @dev Withdraw CDP
*/
function free(uint cdpNum, uint jam) internal {
if (jam > 0) {
bytes32 cup = bytes32(cdpNum);
address tubAddr = getSaiTubAddress();
TubInterface tub = TubInterface(tubAddr);
TokenInterface peth = tub.skr();
TokenInterface weth = tub.gem();
uint ink = rdiv(jam, tub.per());
ink = rmul(ink, tub.per()) <= jam ? ink : ink - 1;
tub.free(cup, ink);
setMakerAllowance(peth, tubAddr);
tub.exit(ink);
uint freeJam = weth.balanceOf(address(this)); // withdraw possible previous stuck WETH as well
weth.withdraw(freeJam);
}
}
/**
* @dev Deposit Collateral
*/
function lock(uint cdpNum, uint ethAmt) internal {
if (ethAmt > 0) {
bytes32 cup = bytes32(cdpNum);
address tubAddr = getSaiTubAddress();
TubInterface tub = TubInterface(tubAddr);
TokenInterface weth = tub.gem();
TokenInterface peth = tub.skr();
(address lad,,,) = tub.cups(cup);
require(lad == address(this), "cup-not-owned");
weth.deposit.value(ethAmt)();
uint ink = rdiv(ethAmt, tub.per());
ink = rmul(ink, tub.per()) <= ethAmt ? ink : ink - 1;
setMakerAllowance(weth, tubAddr);
tub.join(ink);
setMakerAllowance(peth, tubAddr);
tub.lock(cup, ink);
}
}
/**
* @dev Borrow DAI Debt
*/
function draw(uint cdpNum, uint _wad) internal {
bytes32 cup = bytes32(cdpNum);
if (_wad > 0) {
TubInterface tub = TubInterface(getSaiTubAddress());
tub.draw(cup, _wad);
// Returning Liquidity To Liquidity Contract
require(TokenInterface(getDAIAddress()).transfer(getLiquidityAddr(), _wad), "Not-enough-DAI");
LiquidityInterface(getLiquidityAddr()).payBorrowBack(getDAIAddress(), getCDAIAddress(), _wad);
}
}
/**
* @dev Check if entered amt is valid or not (Used in makerToCompound)
*/
function checkCDP(bytes32 cup, uint ethAmt, uint daiAmt) internal returns (uint ethCol, uint daiDebt) {
TubInterface tub = TubInterface(getSaiTubAddress());
ethCol = rmul(tub.ink(cup), tub.per()) - 1; // get ETH col from PETH col
daiDebt = tub.tab(cup);
daiDebt = daiAmt < daiDebt ? daiAmt : daiDebt; // if DAI amount > max debt. Set max debt
ethCol = ethAmt < ethCol ? ethAmt : ethCol; // if ETH amount > max Col. Set max col
}
/**
* @dev Run wipe & Free function together
*/
function wipeAndFreeMaker(uint cdpNum, uint jam, uint _wad) internal returns (uint daiAmt) {
daiAmt = wipe(cdpNum, _wad);
free(cdpNum, jam);
}
/**
* @dev Run Lock & Draw function together
*/
function lockAndDrawMaker(uint cdpNum, uint jam, uint _wad) internal {
lock(cdpNum, jam);
draw(cdpNum, _wad);
}
}
contract CompoundResolver is MakerResolver {
/**
* @dev Deposit ETH and mint CETH
*/
function mintCEth(uint tokenAmt) internal {
enterMarket(getCETHAddress());
CETHInterface cToken = CETHInterface(getCETHAddress());
cToken.mint.value(tokenAmt)();
emit LogMint(
getAddressETH(),
getCETHAddress(),
tokenAmt,
msg.sender
);
}
/**
* @dev borrow DAI
*/
function borrowDAIComp(uint daiAmt) internal {
enterMarket(getCDAIAddress());
require(CTokenInterface(getCDAIAddress()).borrow(daiAmt) == 0, "got collateral?");
// Returning Liquidity to Liquidity Contract
require(TokenInterface(getDAIAddress()).transfer(getLiquidityAddr(), daiAmt), "Not-enough-DAI");
LiquidityInterface(getLiquidityAddr()).payBorrowBack(getDAIAddress(), getCDAIAddress(), daiAmt);
emit LogBorrow(
getDAIAddress(),
getCDAIAddress(),
daiAmt,
address(this)
);
}
/**
* @dev Pay DAI Debt
*/
function repayDaiComp(uint tokenAmt) internal returns (uint wipeAmt) {
CERC20Interface cToken = CERC20Interface(getCDAIAddress());
uint daiBorrowed = cToken.borrowBalanceCurrent(address(this));
wipeAmt = tokenAmt < daiBorrowed ? tokenAmt : daiBorrowed;
// Getting Liquidity from Liquidity Contract
LiquidityInterface(getLiquidityAddr()).borrowTknAndTransfer(getDAIAddress(), getCDAIAddress(), wipeAmt);
setApproval(getDAIAddress(), wipeAmt, getCDAIAddress());
require(cToken.repayBorrow(wipeAmt) == 0, "transfer approved?");
emit LogRepay(
getDAIAddress(),
getCDAIAddress(),
wipeAmt,
address(this)
);
}
/**
* @dev Redeem CETH
* @param tokenAmt Amount of token To Redeem
*/
function redeemCETH(uint tokenAmt) internal returns(uint ethAmtReddemed) {
CTokenInterface cToken = CTokenInterface(getCETHAddress());
uint cethBal = cToken.balanceOf(address(this));
uint exchangeRate = cToken.exchangeRateCurrent();
uint cethInEth = wmul(cethBal, exchangeRate);
setApproval(getCETHAddress(), 2**128, getCETHAddress());
ethAmtReddemed = tokenAmt;
if (tokenAmt > cethInEth) {
require(cToken.redeem(cethBal) == 0, "something went wrong");
ethAmtReddemed = cethInEth;
} else {
require(cToken.redeemUnderlying(tokenAmt) == 0, "something went wrong");
}
emit LogRedeem(
getAddressETH(),
getCETHAddress(),
ethAmtReddemed,
address(this)
);
}
/**
* @dev run mint & borrow together
*/
function mintAndBorrowComp(uint ethAmt, uint daiAmt) internal {
mintCEth(ethAmt);
borrowDAIComp(daiAmt);
}
/**
* @dev run payback & redeem together
*/
function paybackAndRedeemComp(uint ethCol, uint daiDebt) internal returns (uint ethAmt, uint daiAmt) {
daiAmt = repayDaiComp(daiDebt);
ethAmt = redeemCETH(ethCol);
}
/**
* @dev Check if entered amt is valid or not (Used in makerToCompound)
*/
function checkCompound(uint ethAmt, uint daiAmt) internal returns (uint ethCol, uint daiDebt) {
CTokenInterface cEthContract = CTokenInterface(getCETHAddress());
uint cEthBal = cEthContract.balanceOf(msg.sender);
uint ethExchangeRate = cEthContract.exchangeRateCurrent();
ethCol = wmul(cEthBal, ethExchangeRate);
ethCol = wdiv(ethCol, ethExchangeRate) <= cEthBal ? ethCol : ethCol - 1;
ethCol = ethCol <= ethAmt ? ethCol : ethAmt; // Set Max if amount is greater than the Col user have
daiDebt = CERC20Interface(getCDAIAddress()).borrowBalanceCurrent(msg.sender);
daiDebt = daiDebt <= daiAmt ? daiDebt : daiAmt; // Set Max if amount is greater than the Debt user have
}
}
contract InstaMakerCompBridge is CompoundResolver {
event LogMakerToCompound(uint ethAmt, uint daiAmt);
event LogCompoundToMaker(uint ethAmt, uint daiAmt);
/**
* @dev convert Maker CDP into Compound Collateral
*/
function makerToCompound(uint cdpId, uint ethQty, uint daiQty) external {
(uint ethAmt, uint daiDebt) = checkCDP(bytes32(cdpId), ethQty, daiQty);
uint daiAmt = wipeAndFreeMaker(cdpId, ethAmt, daiDebt); // Getting Liquidity inside Wipe function
enterMarket(getCETHAddress());
enterMarket(getCDAIAddress());
mintAndBorrowComp(ethAmt, daiAmt); // Returning Liquidity inside Borrow function
emit LogMakerToCompound(ethAmt, daiAmt);
}
/**
* @dev convert Compound Collateral into Maker CDP
* @param cdpId = 0, if user don't have any CDP
*/
function compoundToMaker(uint cdpId, uint ethQty, uint daiQty) external {
uint cdpNum = cdpId > 0 ? cdpId : open();
(uint ethCol, uint daiDebt) = checkCompound(ethQty, daiQty);
(uint ethAmt, uint daiAmt) = paybackAndRedeemComp(ethCol, daiDebt); // Getting Liquidity inside Wipe function
ethAmt = ethAmt < address(this).balance ? ethAmt : address(this).balance;
lockAndDrawMaker(cdpNum, ethAmt, daiAmt); // Returning Liquidity inside Borrow function
emit LogCompoundToMaker(ethAmt, daiAmt);
}
function() external payable {}
}