smart-contract/contracts/ProxyLogics/InstaBridge.sol

171 lines
4.6 KiB
Solidity
Raw Normal View History

2019-06-19 14:52:24 +00:00
pragma solidity ^0.5.7;
interface ERC20Interface {
function allowance(address, address) external view returns (uint);
function approve(address, uint) external;
}
interface TubInterface {
function give(bytes32, address) external;
}
interface PepInterface {
function peek() external returns (bytes32, bool);
}
interface ComptrollerInterface {
function enterMarkets(address[] calldata cTokens) external returns (uint[] memory);
function getAssetsIn(address account) external view returns (address[] memory);
}
interface CTokenInterface {
function borrow(uint borrowAmount) external returns (uint);
}
2019-06-19 15:03:25 +00:00
interface BridgeInterface {
function makerToCompound(uint, uint, uint) external returns (uint);
function compoundToMaker(uint, uint, uint) external;
2019-06-20 13:41:02 +00:00
function refillFunds(uint) external;
2019-06-19 15:03:25 +00:00
}
2019-06-19 14:52:24 +00:00
2019-06-20 13:41:02 +00:00
contract Helper {
2019-06-19 14:52:24 +00:00
/**
* @dev get MakerDAO CDP engine
*/
function getSaiTubAddress() public pure returns (address sai) {
sai = 0x448a5065aeBB8E423F0896E6c5D525C040f59af3;
}
/**
* @dev get Compound Comptroller Address
*/
function getComptrollerAddress() public pure returns (address troller) {
troller = 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B;
}
/**
2019-06-20 13:49:34 +00:00
* @dev get DAI Token Addrewss
2019-06-19 14:52:24 +00:00
*/
function getDAIAddress() public pure returns (address dai) {
dai = 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359;
}
/**
2019-06-20 13:49:34 +00:00
* @dev get Compound CETH Address
*/
2019-06-20 17:14:38 +00:00
function getCETHAddress() public pure returns (address cEth) {
2019-06-20 18:21:13 +00:00
cEth = 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5;
2019-06-20 13:49:34 +00:00
}
/**
* @dev get Compound CDAI Address
2019-06-19 14:52:24 +00:00
*/
function getCDAIAddress() public pure returns (address cDai) {
cDai = 0xF5DCe57282A584D2746FaF1593d3121Fcac444dC;
}
/**
2019-06-20 13:49:34 +00:00
* @dev get MakerDAO<>Compound Bridge Contract
2019-06-19 14:52:24 +00:00
*/
function getBridgeAddress() public pure returns (address bridge) {
2019-06-29 22:50:06 +00:00
bridge = 0x37aCfEf331e6063C8507C2A69c97B4f78c770A5A;
2019-06-19 14:52:24 +00:00
}
/**
2019-06-20 13:49:34 +00:00
* @dev setting allowance if required
2019-06-19 14:52:24 +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) {
erc20Contract.approve(to, 2**255);
}
}
/**
* @dev transfer CDP ownership
*/
function give(uint cdpNum, address nextOwner) internal {
TubInterface(getSaiTubAddress()).give(bytes32(cdpNum), nextOwner);
}
2019-06-20 13:41:02 +00:00
/**
* @dev enter compound market
*/
2019-06-19 14:52:24 +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 borrow DAI from compound
2019-06-19 14:52:24 +00:00
*/
2019-06-19 18:42:39 +00:00
function borrowDAI(uint tokenAmt) internal {
enterMarket(getCETHAddress());
2019-06-19 14:52:24 +00:00
enterMarket(getCDAIAddress());
2019-06-19 20:30:22 +00:00
require(CTokenInterface(getCDAIAddress()).borrow(tokenAmt) == 0, "got collateral?");
2019-06-19 14:52:24 +00:00
}
2019-06-19 15:03:25 +00:00
}
2019-06-20 13:49:34 +00:00
contract Bridge is Helper {
2019-06-19 15:03:25 +00:00
2019-06-20 13:41:02 +00:00
/**
* @dev MakerDAO to Compound
*/
2019-06-19 15:03:25 +00:00
function makerToCompound(uint cdpId, uint ethCol, uint daiDebt) public {
give(cdpId, getBridgeAddress());
BridgeInterface bridge = BridgeInterface(getBridgeAddress());
uint daiAmt = bridge.makerToCompound(cdpId, ethCol, daiDebt);
2019-06-20 20:59:09 +00:00
if (daiAmt > 0) {
2019-06-20 12:49:20 +00:00
borrowDAI(daiAmt);
2019-06-20 13:49:34 +00:00
setApproval(getDAIAddress(), daiAmt, getBridgeAddress());
2019-06-20 13:41:02 +00:00
bridge.refillFunds(daiAmt);
2019-06-20 12:49:20 +00:00
}
2019-06-19 15:03:25 +00:00
}
2019-06-20 13:41:02 +00:00
/**
* @dev Compound to MakerDAO
*/
2019-06-19 15:03:25 +00:00
function compoundToMaker(uint cdpId, uint ethCol, uint daiDebt) public {
if (cdpId != 0) {
give(cdpId, getBridgeAddress());
}
2019-06-20 12:49:20 +00:00
if (ethCol > 0) {
2019-06-20 13:49:34 +00:00
setApproval(getCETHAddress(), 2**150, getBridgeAddress());
2019-06-20 12:49:20 +00:00
}
BridgeInterface(getBridgeAddress()).compoundToMaker(cdpId, ethCol, daiDebt);
2019-06-19 15:03:25 +00:00
}
2019-06-20 12:49:20 +00:00
}
contract InstaBridge is Bridge {
uint public version;
/**
* @dev setting up variables on deployment
* 1...2...3 versioning in each subsequent deployments
*/
constructor(uint _version) public {
version = _version;
}
function() external payable {}
2019-06-19 14:52:24 +00:00
}