From 36dce595ae3a03b2455a31f8397fbed75148d986 Mon Sep 17 00:00:00 2001 From: Samyak Jain Date: Fri, 25 Oct 2019 02:39:08 +0530 Subject: [PATCH 1/6] Split CDP MCD Migration --- contracts/ProxyLogics/MCD/InstaMcdMigrate.sol | 186 ++++++++---------- 1 file changed, 82 insertions(+), 104 deletions(-) diff --git a/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol b/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol index 181927e..dd17a71 100644 --- a/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol +++ b/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol @@ -182,87 +182,8 @@ contract Helpers is DSMath { } -contract SCDResolver is Helpers { +contract MKRSwapper is Helpers { - function open() internal returns (bytes32 cup) { - cup = TubInterface(getSaiTubAddress()).open(); - } - - function wipe(bytes32 cup, uint _wad) internal { - if (_wad > 0) { - TubInterface tub = TubInterface(getSaiTubAddress()); - TokenInterface dai = tub.sai(); - TokenInterface mkr = tub.gov(); - - (address lad,,,) = tub.cups(cup); - require(lad == address(this), "cup-not-owned"); - - setAllowance(dai, getSaiTubAddress()); - setAllowance(mkr, getSaiTubAddress()); - - tub.wipe(cup, _wad); - } - } - - function free(bytes32 cup, uint jam) internal { - if (jam > 0) { - address tubAddr = getSaiTubAddress(); - - TubInterface tub = TubInterface(tubAddr); - TokenInterface peth = tub.skr(); - - uint ink = rdiv(jam, tub.per()); - ink = rmul(ink, tub.per()) <= jam ? ink : ink - 1; - tub.free(cup, ink); - - setAllowance(peth, tubAddr); - - tub.exit(ink); - // weth.withdraw(freeJam); // No need because we will lock it in small cdp - } - } - - function lock(bytes32 cup, uint jam) internal { - if (jam > 0) { - 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(msg.value)(); // no need because withdrawn Weth is not convert back - - uint ink = rdiv(jam, tub.per()); - ink = rmul(ink, tub.per()) <= jam ? ink : ink - 1; - - setAllowance(weth, tubAddr); - tub.join(ink); - - setAllowance(peth, tubAddr); - tub.lock(cup, ink); - } - } - - function draw(bytes32 cup, uint _wad) internal { - if (_wad > 0) { - TubInterface tub = TubInterface(getSaiTubAddress()); - tub.draw(cup, _wad); - } - } - - function setAllowance(TokenInterface _token, address _spender) private { - if (_token.allowance(address(this), _spender) != uint(-1)) { - _token.approve(_spender, uint(-1)); - } - } - -} - - -contract MkrResolver is SCDResolver { function swapToMkrOtc(address tokenAddr, uint govFee) internal { TokenInterface mkr = TubInterface(getSaiTubAddress()).gov(); uint payAmt = OtcInterface(getOtcAddress()).getPayAmount(tokenAddr, address(mkr), govFee); @@ -305,11 +226,81 @@ contract MkrResolver is SCDResolver { } } + } -contract MCDResolver is MkrResolver { - function swapDaiToSai( +contract SCDResolver is MKRSwapper { + + function open() internal returns (bytes32 cup) { + cup = TubInterface(getSaiTubAddress()).open(); + } + + function wipe(bytes32 cup, uint _wad, address payFeeWith) internal { + if (_wad > 0) { + TubInterface tub = TubInterface(getSaiTubAddress()); + TokenInterface dai = tub.sai(); + TokenInterface mkr = tub.gov(); + + (address lad,,,) = tub.cups(cup); + require(lad == address(this), "cup-not-owned"); + + setAllowance(dai, getSaiTubAddress()); + setAllowance(mkr, getSaiTubAddress()); + + (bytes32 val, bool ok) = tub.pep().peek(); + + if (ok && val != 0) { + // MKR required for wipe = Stability fees accrued in Dai / MKRUSD value + uint mkrFee = rdiv(tub.rap(cup), tub.tab(cup)); // I had to split because it was showing error in remix. + mkrFee = rmul(_wad, mkrFee); + mkrFee = wdiv(mkrFee, uint(val)); + + // swapToMkrUniswap(payFeeWith, mkrFee); //Uniswap + swapToMkrOtc(payFeeWith, mkrFee); //otc + } + + tub.wipe(cup, _wad); + } + } + + function free(bytes32 cup, uint ink) internal { + if (ink > 0) { + TubInterface(getSaiTubAddress()).free(cup, ink); // No need to convert PETH in WETH because we will directly lock PETH in split CDP + } + } + + function lock(bytes32 cup, uint ink) internal { + if (ink > 0) { + TubInterface tub = TubInterface(getSaiTubAddress()); + TokenInterface peth = tub.skr(); + + (address lad,,,) = tub.cups(cup); + require(lad == address(this), "cup-not-owned"); + + setAllowance(peth, getSaiTubAddress()); + tub.lock(cup, ink); + } + } + + function draw(bytes32 cup, uint _wad) internal { + if (_wad > 0) { + TubInterface tub = TubInterface(getSaiTubAddress()); + tub.draw(cup, _wad); + } + } + + function setAllowance(TokenInterface _token, address _spender) private { + if (_token.allowance(address(this), _spender) != uint(-1)) { + _token.approve(_spender, uint(-1)); + } + } + +} + + +contract MCDResolver is SCDResolver { + function swapDaiToSai( // Check Samyak - It's not needed for this contract address payable scdMcdMigration, // Migration contract address uint wad // Amount to swap ) internal @@ -341,7 +332,7 @@ contract MCDResolver is MkrResolver { if (payGem != address(0)) { swapToMkrOtc(payGem, govFee); } else { - require(tub.gov().transferFrom(msg.sender, address(this), govFee), "transfer-failed"); + require(tub.gov().transferFrom(msg.sender, address(this), govFee), "transfer-failed"); // Check Samyak - We can directly transfer MKR to address(scdMcdMigration). Right? } require(tub.gov().transfer(address(scdMcdMigration), govFee), "transfer-failed"); } @@ -387,35 +378,22 @@ contract MigrateResolver is LiquidityResolver { TubInterface tub = TubInterface(getSaiTubAddress()); bytes32 scdCup = bytes32(scdCDP); - uint _jam = rmul(tub.ink(scdCup), tub.per()); - uint _wad = tub.tab(scdCup); - if (toConvert < 10**18) { uint initialPoolBal = sub(getPoolAddress().balance, 10000000000); bytes32 splitCup = TubInterface(getSaiTubAddress()).open(); - _jam = wmul(_jam, toConvert); - _wad = wmul(_wad, toConvert); + // Check Samyak - have to check max limit to convert. Hint: SAI balance in "x" contract. If toConvert > maxConvert then toConvert = maxConvert; - //get liquidity from InstaDApp Pool. + uint _ink = wmul(tub.ink(scdCup), toConvert); // Taking collateral in PETH only + uint _wad = wmul(tub.tab(scdCup), toConvert); + + // getting liquidity from InstaDApp Pool. getLiquidity(_wad); - (bytes32 val, bool ok) = tub.pep().peek(); + wipe(scdCup, _wad, payFeeWith); + free(scdCup, _ink); - if (ok && val != 0) { - // MKR required for wipe = Stability fees accrued in Dai / MKRUSD value - uint mkrFee = rdiv(tub.rap(scdCup), tub.tab(scdCup)); // I had to split because it was showing error in remix. - mkrFee = rmul(_wad, mkrFee); - mkrFee = wdiv(mkrFee, uint(val)); - - // swapToMkrUniswap(payFeeWith, mkrFee); //Uniswap - swapToMkrOtc(payFeeWith, mkrFee); //otc - } - - wipe(scdCup, _wad); - free(scdCup, _jam); - - lock(splitCup, _jam); + lock(splitCup, _ink); draw(splitCup, _wad); //transfer and payback liquidity to InstaDApp Pool. From f3d7c03f4cf4eb227c378f76ea362bff1037b99b Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Sun, 27 Oct 2019 00:39:27 +0530 Subject: [PATCH 2/6] added maxConvert to mcd migrate --- contracts/ProxyLogics/MCD/InstaMcdMigrate.sol | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol b/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol index dd17a71..66c1387 100644 --- a/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol +++ b/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol @@ -129,9 +129,9 @@ contract Helpers is DSMath { /** * @dev get Dai (Dai v2) address */ - function getDaiAddress() public pure returns (address dai) { - dai = 0x448a5065aeBB8E423F0896E6c5D525C040f59af3; - } + // function getDaiAddress() public pure returns (address dai) { + // dai = 0x448a5065aeBB8E423F0896E6c5D525C040f59af3; + // } /** * @dev get Compound WETH Address @@ -300,20 +300,6 @@ contract SCDResolver is MKRSwapper { contract MCDResolver is SCDResolver { - function swapDaiToSai( // Check Samyak - It's not needed for this contract - address payable scdMcdMigration, // Migration contract address - uint wad // Amount to swap - ) internal - { - // TokenInterface sai = TokenInterface(getSaiAddress()); - TokenInterface dai = TokenInterface(getDaiAddress()); - dai.transferFrom(msg.sender, address(this), wad); - if (dai.allowance(address(this), scdMcdMigration) < wad) { - dai.approve(scdMcdMigration, wad); - } - MCDInterface(scdMcdMigration).swapDaiToSai(wad); - } - function migrateToMCD( address payable scdMcdMigration, // Migration contract address bytes32 cup, // SCD CDP Id to migrate @@ -372,20 +358,31 @@ contract MigrateResolver is LiquidityResolver { // uint mcdCDP, for merge uint toConvert, address payFeeWith, - address payable scdMcdMigration + address payable scdMcdMigration, + address daiJoin // Check Thrilok - will remove in the last, when we get final address ) external payable returns (uint cdp) { TubInterface tub = TubInterface(getSaiTubAddress()); bytes32 scdCup = bytes32(scdCDP); + uint maxConvert = toConvert; if (toConvert < 10**18) { uint initialPoolBal = sub(getPoolAddress().balance, 10000000000); bytes32 splitCup = TubInterface(getSaiTubAddress()).open(); - // Check Samyak - have to check max limit to convert. Hint: SAI balance in "x" contract. If toConvert > maxConvert then toConvert = maxConvert; + // Check Thrilok - check if ratio is good. + uint saiBal = tub.sai().balanceOf(daiJoin); + uint _wadTotal = tub.tab(scdCup); + uint _wad = wmul(_wadTotal, toConvert); - uint _ink = wmul(tub.ink(scdCup), toConvert); // Taking collateral in PETH only - uint _wad = wmul(tub.tab(scdCup), toConvert); + // Check if sai_join has enough sai to migrate. + if (saiBal < _wad) { + // Set new convert ratio according to sai_join balance. + maxConvert = sub(wdiv(saiBal, _wadTotal), 1000000000000000); + _wad = wmul(_wadTotal, maxConvert); + } + + uint _ink = wmul(tub.ink(scdCup), maxConvert); // Taking collateral in PETH only // getting liquidity from InstaDApp Pool. getLiquidity(_wad); @@ -413,7 +410,7 @@ contract MigrateResolver is LiquidityResolver { emit LogMigrate( uint(scdCup), - toConvert, + maxConvert, payFeeWith, cdp ); From 03a4949753e4ebc36172c9f29e3a4c2ca7933ed0 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Sun, 27 Oct 2019 01:12:48 +0530 Subject: [PATCH 3/6] Migrate Thrilok check done --- contracts/ProxyLogics/MCD/InstaMcdMigrate.sol | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol b/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol index 66c1387..4e5cca2 100644 --- a/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol +++ b/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol @@ -352,7 +352,9 @@ contract LiquidityResolver is MCDResolver { contract MigrateResolver is LiquidityResolver { + event LogMigrate(uint scdCdp, uint toConvert, address payFeeWith, uint mcdCdp); + function migrate( uint scdCDP, // uint mcdCDP, for merge @@ -377,9 +379,9 @@ contract MigrateResolver is LiquidityResolver { // Check if sai_join has enough sai to migrate. if (saiBal < _wad) { + _wad = saiBal; // Set new convert ratio according to sai_join balance. - maxConvert = sub(wdiv(saiBal, _wadTotal), 1000000000000000); - _wad = wmul(_wadTotal, maxConvert); + maxConvert = sub(wdiv(saiBal, _wadTotal), 10); } uint _ink = wmul(tub.ink(scdCup), maxConvert); // Taking collateral in PETH only From 23b0dc519e696d6c244c931763501fdc1ca195a2 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Tue, 29 Oct 2019 04:16:25 +0530 Subject: [PATCH 4/6] Added merge in mcd migrate --- contracts/ProxyLogics/MCD/InstaMcdMigrate.sol | 124 ++++++++++++++++-- 1 file changed, 113 insertions(+), 11 deletions(-) diff --git a/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol b/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol index 4e5cca2..23d4b07 100644 --- a/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol +++ b/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol @@ -73,6 +73,84 @@ interface OtcInterface { ) external; } +interface GemLike { + function approve(address, uint) external; + function transfer(address, uint) external; + function transferFrom(address, address, uint) external; + function deposit() external payable; + function withdraw(uint) external; +} + +interface JugLike { + function drip(bytes32) external; +} + +interface ManagerLike { + function cdpCan(address, uint, address) external view returns (uint); + function ilks(uint) external view returns (bytes32); + function owns(uint) external view returns (address); + function urns(uint) external view returns (address); + function vat() external view returns (address); + function open(bytes32) external returns (uint); + function give(uint, address) external; + function cdpAllow(uint, address, uint) external; + function urnAllow(address, uint) external; + function frob(uint, int, int) external; + function frob( + uint, + address, + int, + int + ) external; + function flux(uint, address, uint) external; + function move(uint, address, uint) external; + function exit( + address, + uint, + address, + uint + ) external; + function quit(uint, address) external; + function enter(address, uint) external; + function shift(uint, uint) external; +} + +interface VatLike { + function can(address, address) external view returns (uint); + function ilks(bytes32) external view returns (uint, uint, uint, uint, uint); + function dai(address) external view returns (uint); + function urns(bytes32, address) external view returns (uint, uint); + function frob( + bytes32, + address, + address, + address, + int, + int + ) external; + function hope(address) external; + function move(address, address, uint) external; +} + +interface GemJoinLike { + function dec() external returns (uint); + function gem() external returns (GemLike); + function join(address, uint) external payable; + function exit(address, uint) external; +} + +interface GNTJoinLike { + function bags(address) external view returns (address); + function make(address) external returns (address); +} + +interface DaiJoinLike { + function vat() external returns (VatLike); + function dai() external returns (GemLike); + function join(address, uint) external payable; + function exit(address, uint) external; +} + contract DSMath { @@ -325,6 +403,15 @@ contract MCDResolver is SCDResolver { // Execute migrate function cdp = MCDInterface(scdMcdMigration).migrate(cup); } + + function shiftCDP( // Check Thrilok - in build function. + address manager, + uint cdpSrc, + uint cdpOrg + ) internal + { + ManagerLike(manager).shift(cdpSrc, cdpOrg); + } } @@ -354,15 +441,17 @@ contract LiquidityResolver is MCDResolver { contract MigrateResolver is LiquidityResolver { event LogMigrate(uint scdCdp, uint toConvert, address payFeeWith, uint mcdCdp); + event LogMigrateAndMerge(uint scdCdp, uint toConvert, address payFeeWith, uint mcdMergeCdp); function migrate( uint scdCDP, - // uint mcdCDP, for merge + uint mergeCDP, //for merge uint toConvert, address payFeeWith, address payable scdMcdMigration, + address manager, // Check Thrilok - will remove in the last, when we get final address address daiJoin // Check Thrilok - will remove in the last, when we get final address - ) external payable returns (uint cdp) + ) external payable returns (uint newMcdCdp) { TubInterface tub = TubInterface(getSaiTubAddress()); bytes32 scdCup = bytes32(scdCDP); @@ -379,7 +468,7 @@ contract MigrateResolver is LiquidityResolver { // Check if sai_join has enough sai to migrate. if (saiBal < _wad) { - _wad = saiBal; + _wad = sub(saiBal, 1000); // Set new convert ratio according to sai_join balance. maxConvert = sub(wdiv(saiBal, _wadTotal), 10); } @@ -401,21 +490,34 @@ contract MigrateResolver is LiquidityResolver { uint finalPoolBal = getPoolAddress().balance; assert(finalPoolBal >= initialPoolBal); - cdp = migrateToMCD(scdMcdMigration, splitCup, payFeeWith); + newMcdCdp = migrateToMCD(scdMcdMigration, splitCup, payFeeWith); } else { - cdp = migrateToMCD(scdMcdMigration, scdCup, payFeeWith); + newMcdCdp = migrateToMCD(scdMcdMigration, scdCup, payFeeWith); } TokenInterface weth = TokenInterface(getWETHAddress()); weth.withdraw(weth.balanceOf(address(this))); //withdraw WETH, if any leftover. msg.sender.transfer(address(this).balance); //transfer leftover ETH. - emit LogMigrate( - uint(scdCup), - maxConvert, - payFeeWith, - cdp - ); + // merge - Check Thrilok + if (mergeCDP != 0) { + require(ManagerLike(manager).owns(mergeCDP) == address(this), "NOT-OWNER"); + shiftCDP(manager, newMcdCdp, mergeCDP); + + emit LogMigrateAndMerge( + uint(scdCup), + maxConvert, + payFeeWith, + mergeCDP + ); + } else { + emit LogMigrate( + uint(scdCup), + maxConvert, + payFeeWith, + newMcdCdp + ); + } } } From 1052fd7d24475d6d2aa8e61259d09c25ea2bda59 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Tue, 29 Oct 2019 17:00:30 +0530 Subject: [PATCH 5/6] code refactor mcd migrate --- contracts/ProxyLogics/MCD/InstaMcdMigrate.sol | 191 ++++++++---------- 1 file changed, 87 insertions(+), 104 deletions(-) diff --git a/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol b/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol index 23d4b07..964db79 100644 --- a/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol +++ b/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol @@ -73,18 +73,6 @@ interface OtcInterface { ) external; } -interface GemLike { - function approve(address, uint) external; - function transfer(address, uint) external; - function transferFrom(address, address, uint) external; - function deposit() external payable; - function withdraw(uint) external; -} - -interface JugLike { - function drip(bytes32) external; -} - interface ManagerLike { function cdpCan(address, uint, address) external view returns (uint); function ilks(uint) external view returns (bytes32); @@ -115,42 +103,6 @@ interface ManagerLike { function shift(uint, uint) external; } -interface VatLike { - function can(address, address) external view returns (uint); - function ilks(bytes32) external view returns (uint, uint, uint, uint, uint); - function dai(address) external view returns (uint); - function urns(bytes32, address) external view returns (uint, uint); - function frob( - bytes32, - address, - address, - address, - int, - int - ) external; - function hope(address) external; - function move(address, address, uint) external; -} - -interface GemJoinLike { - function dec() external returns (uint); - function gem() external returns (GemLike); - function join(address, uint) external payable; - function exit(address, uint) external; -} - -interface GNTJoinLike { - function bags(address) external view returns (address); - function make(address) external returns (address); -} - -interface DaiJoinLike { - function vat() external returns (VatLike); - function dai() external returns (GemLike); - function join(address, uint) external payable; - function exit(address, uint) external; -} - contract DSMath { @@ -410,6 +362,7 @@ contract MCDResolver is SCDResolver { uint cdpOrg ) internal { + require(ManagerLike(manager).owns(cdpOrg) == address(this), "NOT-OWNER"); ManagerLike(manager).shift(cdpSrc, cdpOrg); } } @@ -438,86 +391,116 @@ contract LiquidityResolver is MCDResolver { } -contract MigrateResolver is LiquidityResolver { +contract MigrateHelper is LiquidityResolver { + function setSplitAmount(bytes32 cup, uint toConvert, address daiJoin) internal returns (uint _wad, uint _ink, uint maxConvert) { + // Set ratio according to user. + TubInterface tub = TubInterface(getSaiTubAddress()); + maxConvert = toConvert; + uint saiBal = tub.sai().balanceOf(daiJoin); + uint _wadTotal = tub.tab(cup); + // wad according to toConvert ratio + _wad = wmul(_wadTotal, toConvert); - event LogMigrate(uint scdCdp, uint toConvert, address payFeeWith, uint mcdCdp); + //if sai_join has enough sai to migrate. + if (saiBal < _wad) { + // set saiBal as wad amount. + _wad = sub(saiBal, 1000); + // set new convert ratio according to sai_join balance. + maxConvert = sub(wdiv(saiBal, _wadTotal), 100); + } + // ink according to maxConvert ratio. + _ink = wmul(tub.ink(cup), maxConvert); // Taking collateral in PETH only + } + + function splitCdp( + bytes32 scdCup, + bytes32 splitCup, + uint _wad, + uint _ink, + address payFeeWith + ) internal + { + //getting InstaDApp Pool Balance. + uint initialPoolBal = sub(getPoolAddress().balance, 10000000000); + + //fetch liquidity from InstaDApp Pool. + getLiquidity(_wad); + + //transfer assets from scdCup to splitCup. + wipe(scdCup, _wad, payFeeWith); + free(scdCup, _ink); + lock(splitCup, _ink); + draw(splitCup, _wad); + + //transfer and payback liquidity to InstaDApp Pool. + paybackLiquidity(_wad); + + uint finalPoolBal = getPoolAddress().balance; + assert(finalPoolBal >= initialPoolBal); + } +} + + +contract MigrateResolver is MigrateHelper { + + event LogMigrate(uint scdCdp, uint toConvert, address payFeeWith, uint mcdCdp, uint newMcdCdp); event LogMigrateAndMerge(uint scdCdp, uint toConvert, address payFeeWith, uint mcdMergeCdp); function migrate( uint scdCDP, - uint mergeCDP, //for merge + uint mergeCDP, uint toConvert, address payFeeWith, address payable scdMcdMigration, - address manager, // Check Thrilok - will remove in the last, when we get final address - address daiJoin // Check Thrilok - will remove in the last, when we get final address + address manager, + address daiJoin ) external payable returns (uint newMcdCdp) { - TubInterface tub = TubInterface(getSaiTubAddress()); bytes32 scdCup = bytes32(scdCDP); uint maxConvert = toConvert; if (toConvert < 10**18) { - uint initialPoolBal = sub(getPoolAddress().balance, 10000000000); + //new cdp for spliting assets. bytes32 splitCup = TubInterface(getSaiTubAddress()).open(); - // Check Thrilok - check if ratio is good. - uint saiBal = tub.sai().balanceOf(daiJoin); - uint _wadTotal = tub.tab(scdCup); - uint _wad = wmul(_wadTotal, toConvert); + //set split amount according to toConvert and dai_join balance. + uint _wad; + uint _ink; + (_wad, _ink, maxConvert) = setSplitAmount(scdCup, toConvert, daiJoin); - // Check if sai_join has enough sai to migrate. - if (saiBal < _wad) { - _wad = sub(saiBal, 1000); - // Set new convert ratio according to sai_join balance. - maxConvert = sub(wdiv(saiBal, _wadTotal), 10); - } - - uint _ink = wmul(tub.ink(scdCup), maxConvert); // Taking collateral in PETH only - - // getting liquidity from InstaDApp Pool. - getLiquidity(_wad); - - wipe(scdCup, _wad, payFeeWith); - free(scdCup, _ink); - - lock(splitCup, _ink); - draw(splitCup, _wad); - - //transfer and payback liquidity to InstaDApp Pool. - paybackLiquidity(_wad); - - uint finalPoolBal = getPoolAddress().balance; - assert(finalPoolBal >= initialPoolBal); + //split the assets into split cdp. + splitCdp( + scdCup, + splitCup, + _wad, + _ink, + payFeeWith + ); + //migrate the split cdp. newMcdCdp = migrateToMCD(scdMcdMigration, splitCup, payFeeWith); } else { + //migrate the scd cdp. newMcdCdp = migrateToMCD(scdMcdMigration, scdCup, payFeeWith); } - TokenInterface weth = TokenInterface(getWETHAddress()); - weth.withdraw(weth.balanceOf(address(this))); //withdraw WETH, if any leftover. - msg.sender.transfer(address(this).balance); //transfer leftover ETH. - - // merge - Check Thrilok - if (mergeCDP != 0) { - require(ManagerLike(manager).owns(mergeCDP) == address(this), "NOT-OWNER"); - shiftCDP(manager, newMcdCdp, mergeCDP); - - emit LogMigrateAndMerge( - uint(scdCup), - maxConvert, - payFeeWith, - mergeCDP - ); - } else { - emit LogMigrate( - uint(scdCup), - maxConvert, - payFeeWith, - newMcdCdp - ); + //Transfer if any ETH leftover. + if (address(this).balance > 0) { + msg.sender.transfer(address(this).balance); } + + //merge the already existing mcd cdp with the new migrated cdp. + if (mergeCDP != 0) { + shiftCDP(manager, newMcdCdp, mergeCDP); + } + + emit LogMigrate( + uint(scdCup), + maxConvert, + payFeeWith, + mergeCDP, + newMcdCdp + ); } } From d081ace6ec74d9878fff777360d4b2c976942f88 Mon Sep 17 00:00:00 2001 From: Thrilok kumar Date: Tue, 29 Oct 2019 17:28:50 +0530 Subject: [PATCH 6/6] code refactor --- contracts/ProxyLogics/MCD/InstaMcdMigrate.sol | 92 ++++++------------- 1 file changed, 30 insertions(+), 62 deletions(-) diff --git a/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol b/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol index 964db79..6296e35 100644 --- a/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol +++ b/contracts/ProxyLogics/MCD/InstaMcdMigrate.sol @@ -156,13 +156,6 @@ contract Helpers is DSMath { sai = 0x448a5065aeBB8E423F0896E6c5D525C040f59af3; } - /** - * @dev get Dai (Dai v2) address - */ - // function getDaiAddress() public pure returns (address dai) { - // dai = 0x448a5065aeBB8E423F0896E6c5D525C040f59af3; - // } - /** * @dev get Compound WETH Address */ @@ -212,7 +205,31 @@ contract Helpers is DSMath { } -contract MKRSwapper is Helpers { +contract LiquidityResolver is Helpers { + //Had to write seprate for pool, remix was showing error. + function getLiquidity(uint _wad) internal { + uint[] memory _wadArr = new uint[](1); + _wadArr[0] = _wad; + + address[] memory addrArr = new address[](1); + addrArr[0] = address(0); + + // Get liquidity assets to payback user wallet borrowed assets + PoolInterface(getPoolAddress()).accessToken(addrArr, _wadArr, false); + } + + function paybackLiquidity(uint _wad) internal { + address[] memory addrArr = new address[](1); + addrArr[0] = address(0); + + // transfer and payback dai to InstaDApp pool. + require(TokenInterface(getSaiAddress()).transfer(getPoolAddress(), _wad), "Not-enough-dai"); + PoolInterface(getPoolAddress()).paybackToken(addrArr, false); + } +} + + +contract MKRSwapper is LiquidityResolver { function swapToMkrOtc(address tokenAddr, uint govFee) internal { TokenInterface mkr = TubInterface(getSaiTubAddress()).gov(); @@ -232,31 +249,6 @@ contract MKRSwapper is Helpers { ); } - function swapToMkrUniswap(address tokenAddr, uint govFee) internal { - UniswapExchange mkrEx = UniswapExchange(getUniswapMKRExchange()); - address uniSwapFactoryAddr = 0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95; - TokenInterface mkr = TubInterface(getSaiTubAddress()).gov(); - - if (tokenAddr == getWETHAddress()) { - uint ethNeeded = mkrEx.getEthToTokenOutputPrice(govFee); - mkrEx.ethToTokenSwapOutput.value(ethNeeded)(govFee, uint(1899063809)); - } else { - address buyTknExAddr = UniswapFactoryInterface(uniSwapFactoryAddr).getExchange(tokenAddr); - UniswapExchange buyTknEx = UniswapExchange(buyTknExAddr); - uint tknAmt = buyTknEx.getTokenToEthOutputPrice(mkrEx.getEthToTokenOutputPrice(govFee)); //Check thrilok is this correct - require(TokenInterface(tokenAddr).transferFrom(msg.sender, address(this), tknAmt), "not-approved-yet"); - setApproval(tokenAddr, tknAmt, buyTknExAddr); - buyTknEx.tokenToTokenSwapOutput( - govFee, - tknAmt, - uint(999000000000000000000), - uint(1899063809), // 6th March 2030 GMT // no logic - address(mkr) - ); - } - - } - } @@ -282,11 +274,10 @@ contract SCDResolver is MKRSwapper { if (ok && val != 0) { // MKR required for wipe = Stability fees accrued in Dai / MKRUSD value - uint mkrFee = rdiv(tub.rap(cup), tub.tab(cup)); // I had to split because it was showing error in remix. + uint mkrFee = rdiv(tub.rap(cup), tub.tab(cup)); mkrFee = rmul(_wad, mkrFee); mkrFee = wdiv(mkrFee, uint(val)); - // swapToMkrUniswap(payFeeWith, mkrFee); //Uniswap swapToMkrOtc(payFeeWith, mkrFee); //otc } @@ -296,7 +287,7 @@ contract SCDResolver is MKRSwapper { function free(bytes32 cup, uint ink) internal { if (ink > 0) { - TubInterface(getSaiTubAddress()).free(cup, ink); // No need to convert PETH in WETH because we will directly lock PETH in split CDP + TubInterface(getSaiTubAddress()).free(cup, ink); // free PETH } } @@ -333,7 +324,7 @@ contract MCDResolver is SCDResolver { function migrateToMCD( address payable scdMcdMigration, // Migration contract address bytes32 cup, // SCD CDP Id to migrate - address payGem // Token address (only if gov fee will be paid with another token) + address payGem // Token address ) internal returns (uint cdp) { TubInterface tub = TubInterface(getSaiTubAddress()); @@ -356,7 +347,7 @@ contract MCDResolver is SCDResolver { cdp = MCDInterface(scdMcdMigration).migrate(cup); } - function shiftCDP( // Check Thrilok - in build function. + function shiftCDP( address manager, uint cdpSrc, uint cdpOrg @@ -368,30 +359,7 @@ contract MCDResolver is SCDResolver { } -contract LiquidityResolver is MCDResolver { - //Had to write seprate for pool, remix was showing error. - function getLiquidity(uint _wad) internal { - uint[] memory _wadArr = new uint[](1); - _wadArr[0] = _wad; - - address[] memory addrArr = new address[](1); - addrArr[0] = address(0); - - // Get liquidity assets to payback user wallet borrowed assets - PoolInterface(getPoolAddress()).accessToken(addrArr, _wadArr, false); - } - - function paybackLiquidity(uint _wad) internal { - address[] memory addrArr = new address[](1); - addrArr[0] = address(0); - - require(TokenInterface(getSaiAddress()).transfer(getPoolAddress(), _wad), "Not-enough-dai"); - PoolInterface(getPoolAddress()).paybackToken(addrArr, false); - } -} - - -contract MigrateHelper is LiquidityResolver { +contract MigrateHelper is MCDResolver { function setSplitAmount(bytes32 cup, uint toConvert, address daiJoin) internal returns (uint _wad, uint _ink, uint maxConvert) { // Set ratio according to user. TubInterface tub = TubInterface(getSaiTubAddress());