2019-10-16 16:47:44 +00:00
|
|
|
pragma solidity ^0.5.7;
|
2019-11-15 22:14:11 +00:00
|
|
|
/**
|
|
|
|
compound debt migrate
|
|
|
|
*/
|
2019-10-16 16:47:44 +00:00
|
|
|
interface CTokenInterface {
|
2019-10-25 21:53:04 +00:00
|
|
|
function mint(uint mintAmount) external returns (uint);
|
|
|
|
function redeem(uint redeemTokens) external returns (uint);
|
2019-10-16 16:47:44 +00:00
|
|
|
function borrow(uint borrowAmount) external returns (uint);
|
|
|
|
function borrowBalanceCurrent(address account) external returns (uint);
|
2019-10-25 21:53:04 +00:00
|
|
|
function repayBorrow(uint repayAmount) external returns (uint);
|
2019-10-16 16:47:44 +00:00
|
|
|
function totalReserves() external view returns (uint);
|
|
|
|
function underlying() external view returns (address);
|
|
|
|
|
|
|
|
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 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface PoolInterface {
|
|
|
|
function accessToken(address[] calldata ctknAddr, uint[] calldata tknAmt, bool isCompound) external;
|
|
|
|
function paybackToken(address[] calldata ctknAddr, bool isCompound) external payable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Swap Functionality */
|
|
|
|
interface ScdMcdMigration {
|
|
|
|
function swapDaiToSai(uint daiAmt) external;
|
2019-10-25 21:53:04 +00:00
|
|
|
function swapSaiToDai(uint saiAmt) external;
|
2019-10-16 16:47:44 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 22:14:11 +00:00
|
|
|
interface InstaMcdAddress {
|
|
|
|
function manager() external returns (address);
|
|
|
|
function dai() external returns (address);
|
|
|
|
function daiJoin() external returns (address);
|
|
|
|
function vat() external returns (address);
|
|
|
|
function jug() external returns (address);
|
|
|
|
function cat() external returns (address);
|
|
|
|
function gov() external returns (address);
|
|
|
|
function adm() external returns (address);
|
|
|
|
function vow() external returns (address);
|
|
|
|
function spot() external returns (address);
|
|
|
|
function pot() external returns (address);
|
|
|
|
function esm() external returns (address);
|
|
|
|
function mcdFlap() external returns (address);
|
|
|
|
function mcdFlop() external returns (address);
|
|
|
|
function mcdDeploy() external returns (address);
|
|
|
|
function mcdEnd() external returns (address);
|
|
|
|
function proxyActions() external returns (address);
|
|
|
|
function proxyActionsEnd() external returns (address);
|
|
|
|
function proxyActionsDsr() external returns (address);
|
|
|
|
function getCdps() external returns (address);
|
|
|
|
function saiTub() external returns (address);
|
|
|
|
function weth() external returns (address);
|
|
|
|
function bat() external returns (address);
|
|
|
|
function sai() external returns (address);
|
|
|
|
function ethAJoin() external returns (address);
|
|
|
|
function ethAFlip() external returns (address);
|
|
|
|
function batAJoin() external returns (address);
|
|
|
|
function batAFlip() external returns (address);
|
|
|
|
function ethPip() external returns (address);
|
|
|
|
function batAPip() external returns (address);
|
|
|
|
function saiJoin() external returns (address);
|
|
|
|
function saiFlip() external returns (address);
|
|
|
|
function saiPip() external returns (address);
|
|
|
|
function migration() external returns (address payable);
|
|
|
|
}
|
|
|
|
|
2019-10-16 16:47:44 +00:00
|
|
|
|
|
|
|
contract DSMath {
|
|
|
|
|
|
|
|
function sub(uint x, uint y) internal pure returns (uint z) {
|
|
|
|
z = x - y <= x ? x - y : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev get Compound Comptroller Address
|
|
|
|
*/
|
|
|
|
function getComptrollerAddress() public pure returns (address troller) {
|
|
|
|
troller = 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B;
|
|
|
|
}
|
|
|
|
|
2019-11-15 22:14:11 +00:00
|
|
|
/**
|
|
|
|
* @dev get MakerDAO MCD Address contract
|
|
|
|
*/
|
|
|
|
function getMcdAddresses() public pure returns (address mcd) {
|
|
|
|
mcd = 0x448a5065aeBB8E423F0896E6c5D525C040f59af3; // Check Thrilok - add addr at time of deploy
|
|
|
|
}
|
|
|
|
|
2019-10-16 16:47:44 +00:00
|
|
|
/**
|
|
|
|
* @dev get Compound Comptroller Address
|
|
|
|
*/
|
2019-10-25 19:53:11 +00:00
|
|
|
function getCSaiAddress() public pure returns (address csaiAddr) {
|
|
|
|
csaiAddr = 0xF5DCe57282A584D2746FaF1593d3121Fcac444dC;
|
2019-10-16 16:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev get Compound Comptroller Address
|
|
|
|
*/
|
2019-10-25 19:53:11 +00:00
|
|
|
function getCDaiAddress() public pure returns (address cdaiAddr) {
|
2019-11-15 22:14:11 +00:00
|
|
|
cdaiAddr = 0xF5DCe57282A584D2746FaF1593d3121Fcac444dC; //Check Thrilok - add address before release
|
2019-10-16 16:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev get Compound Comptroller Address
|
|
|
|
*/
|
|
|
|
function getSaiAddress() public pure returns (address saiAddr) {
|
2019-10-25 19:53:11 +00:00
|
|
|
saiAddr = 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359;
|
2019-10-16 16:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev get Compound Comptroller Address
|
|
|
|
*/
|
|
|
|
function getDaiAddress() public pure returns (address daiAddr) {
|
2019-11-15 22:14:11 +00:00
|
|
|
daiAddr = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
|
2019-10-16 16:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev get Compound Comptroller Address
|
|
|
|
*/
|
|
|
|
function getLiquidityAddress() public pure returns (address poolAddr) {
|
2019-10-25 19:53:11 +00:00
|
|
|
poolAddr = 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B;
|
2019-10-16 16:47:44 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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, uint(-1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract PoolActions is Helpers {
|
|
|
|
|
|
|
|
function accessLiquidity(uint amt) internal {
|
|
|
|
address[] memory ctknAddrArr = new address[](1);
|
|
|
|
ctknAddrArr[0] = getCSaiAddress();
|
|
|
|
uint[] memory tknAmtArr = new uint[](1);
|
|
|
|
tknAmtArr[0] = amt;
|
|
|
|
PoolInterface(getLiquidityAddress()).accessToken(ctknAddrArr, tknAmtArr, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function paybackLiquidity(uint amt) internal {
|
|
|
|
address[] memory ctknAddrArr = new address[](1);
|
|
|
|
ctknAddrArr[0] = getCSaiAddress();
|
|
|
|
ERC20Interface(getDaiAddress()).transfer(getLiquidityAddress(), amt);
|
|
|
|
PoolInterface(getLiquidityAddress()).paybackToken(ctknAddrArr, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Swap from Dai to Sai */
|
|
|
|
contract MigrationProxyActions is PoolActions {
|
|
|
|
|
2019-10-25 21:53:04 +00:00
|
|
|
function swapSaiToDai(
|
|
|
|
uint wad // Amount to swap
|
|
|
|
) internal
|
|
|
|
{
|
2019-11-15 22:14:11 +00:00
|
|
|
address payable scdMcdMigration = InstaMcdAddress(getMcdAddresses()).migration();
|
2019-10-25 21:53:04 +00:00
|
|
|
ERC20Interface sai = ERC20Interface(getSaiAddress());
|
|
|
|
if (sai.allowance(address(this), scdMcdMigration) < wad) {
|
|
|
|
sai.approve(scdMcdMigration, wad);
|
|
|
|
}
|
|
|
|
ScdMcdMigration(scdMcdMigration).swapSaiToDai(wad);
|
|
|
|
}
|
|
|
|
|
2019-10-16 16:47:44 +00:00
|
|
|
function swapDaiToSai(
|
|
|
|
uint wad // Amount to swap
|
|
|
|
) internal
|
|
|
|
{
|
2019-11-15 22:14:11 +00:00
|
|
|
address payable scdMcdMigration = InstaMcdAddress(getMcdAddresses()).migration();
|
2019-10-16 16:47:44 +00:00
|
|
|
ERC20Interface sai = ERC20Interface(getSaiAddress());
|
|
|
|
ERC20Interface dai = ERC20Interface(getDaiAddress());
|
|
|
|
if (dai.allowance(address(this), scdMcdMigration) < wad) {
|
|
|
|
dai.approve(scdMcdMigration, wad);
|
|
|
|
}
|
|
|
|
ScdMcdMigration(scdMcdMigration).swapDaiToSai(wad);
|
|
|
|
sai.transfer(getLiquidityAddress(), wad);
|
|
|
|
paybackLiquidity(wad);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract CompoundResolver is MigrationProxyActions {
|
|
|
|
|
2019-10-25 21:53:04 +00:00
|
|
|
/**
|
|
|
|
* @dev Redeem ETH/ERC20 and burn Compound Tokens
|
|
|
|
* @param cTokenAmt Amount of CToken To burn
|
|
|
|
*/
|
2019-10-29 17:20:20 +00:00
|
|
|
function redeemCSai(address cErc20, uint cTokenAmt) internal {
|
2019-10-25 21:53:04 +00:00
|
|
|
CTokenInterface cToken = CTokenInterface(cErc20);
|
|
|
|
uint toRedeem = cToken.balanceOf(address(this));
|
|
|
|
toRedeem = toRedeem > cTokenAmt ? cTokenAmt : toRedeem;
|
|
|
|
require(cToken.redeem(toRedeem) == 0, "something went wrong");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Deposit ETH/ERC20 and mint Compound Tokens
|
|
|
|
*/
|
2019-10-29 17:20:20 +00:00
|
|
|
function mintCDai(address cErc20) internal {
|
2019-10-25 21:53:04 +00:00
|
|
|
enterMarket(cErc20);
|
|
|
|
CTokenInterface cToken = CTokenInterface(cErc20);
|
2019-10-29 17:20:20 +00:00
|
|
|
address erc20 = cToken.underlying();
|
2019-10-31 11:49:06 +00:00
|
|
|
uint toDeposit = ERC20Interface(erc20).balanceOf(address(this));
|
2019-10-25 21:53:04 +00:00
|
|
|
setApproval(erc20, toDeposit, cErc20);
|
|
|
|
assert(cToken.mint(toDeposit) == 0);
|
|
|
|
}
|
2019-10-16 16:47:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev borrow ETH/ERC20
|
|
|
|
*/
|
2019-10-25 19:53:11 +00:00
|
|
|
function borrowDAI(uint tokenAmt) internal {
|
2019-10-16 16:47:44 +00:00
|
|
|
enterMarket(getCDaiAddress());
|
|
|
|
require(CTokenInterface(getCDaiAddress()).borrow(tokenAmt) == 0, "got collateral?");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Pay Debt ETH/ERC20
|
|
|
|
*/
|
2019-10-25 19:53:11 +00:00
|
|
|
function repaySAI(uint tokenAmt) internal returns (uint toRepay) {
|
2019-10-16 16:47:44 +00:00
|
|
|
CTokenInterface cToken = CTokenInterface(getCSaiAddress());
|
2019-10-29 17:20:20 +00:00
|
|
|
toRepay = cToken.borrowBalanceCurrent(address(this));
|
2019-10-16 16:47:44 +00:00
|
|
|
toRepay = toRepay > tokenAmt ? tokenAmt : toRepay;
|
|
|
|
accessLiquidity(toRepay);
|
2019-10-29 17:20:20 +00:00
|
|
|
setApproval(cToken.underlying(), toRepay, getCSaiAddress());
|
2019-10-16 16:47:44 +00:00
|
|
|
require(cToken.repayBorrow(toRepay) == 0, "Enough Tokens?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract CompMigration is CompoundResolver {
|
2019-10-31 11:49:06 +00:00
|
|
|
|
2019-10-29 17:20:20 +00:00
|
|
|
event LogCompMigrateCSaiToCDai(uint swapAmt, address owner);
|
2019-10-31 11:49:06 +00:00
|
|
|
event LogCompMigrateDebt(uint migrateAmt, address owner);
|
2019-10-25 21:53:04 +00:00
|
|
|
|
2019-11-15 22:14:11 +00:00
|
|
|
function migrateCSaiToCDai(uint ctknToMigrate) external {
|
2019-10-29 17:20:20 +00:00
|
|
|
redeemCSai(getCSaiAddress(), ctknToMigrate);
|
2019-10-25 21:53:04 +00:00
|
|
|
uint saiBal = ERC20Interface(getSaiAddress()).balanceOf(address(this));
|
2019-11-15 22:14:11 +00:00
|
|
|
swapSaiToDai(saiBal);
|
2019-10-29 17:20:20 +00:00
|
|
|
mintCDai(getCDaiAddress());
|
|
|
|
emit LogCompMigrateCSaiToCDai(saiBal, address(this));
|
2019-10-25 21:53:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 22:14:11 +00:00
|
|
|
function migrateDebt(uint debtToMigrate) external {
|
2019-10-16 16:47:44 +00:00
|
|
|
uint initialPoolBal = sub(getLiquidityAddress().balance, 10000000000);
|
2019-10-29 17:20:20 +00:00
|
|
|
|
2019-11-15 22:14:11 +00:00
|
|
|
uint saiJoinBal = ERC20Interface(getSaiAddress()).balanceOf(InstaMcdAddress(getMcdAddresses()).saiJoin());
|
2019-10-25 19:53:11 +00:00
|
|
|
// Check SAI balance of migration contract. If less than debtToMigrate then set debtToMigrate = SAI_Bal
|
2019-10-31 11:49:06 +00:00
|
|
|
uint migrateAmt = debtToMigrate < saiJoinBal ? debtToMigrate : sub(saiJoinBal, 1000);
|
2019-10-29 17:20:20 +00:00
|
|
|
|
|
|
|
uint debtPaid = repaySAI(migrateAmt); // Repaying SAI debt using InstaDApp pool
|
2019-10-25 19:53:11 +00:00
|
|
|
borrowDAI(debtPaid); // borrowing DAI debt
|
2019-11-15 22:14:11 +00:00
|
|
|
swapDaiToSai(debtPaid); // swapping SAI into DAI and paying back to InstaDApp pool
|
2019-10-16 16:47:44 +00:00
|
|
|
uint finalPoolBal = getLiquidityAddress().balance;
|
|
|
|
assert(finalPoolBal >= initialPoolBal);
|
2019-10-31 11:49:06 +00:00
|
|
|
emit LogCompMigrateDebt(debtPaid, address(this));
|
2019-10-16 16:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-29 17:20:20 +00:00
|
|
|
contract InstaCompoundMigrate is CompMigration {
|
2019-10-16 16:47:44 +00:00
|
|
|
|
|
|
|
function() external payable {}
|
|
|
|
|
|
|
|
}
|