InstaSave SplitSave implementation

This commit is contained in:
Samyak Jain 2019-06-27 00:38:49 +05:30
parent f390caacd5
commit 2ed12e7ab5

View File

@ -72,6 +72,12 @@ interface KyberInterface {
) external view returns (uint, uint); ) external view returns (uint, uint);
} }
interface SplitSwapInterface {
function getBest(address src, address dest, uint srcAmt) external view returns (uint bestExchange, uint destAmt);
function ethToDaiSwap(uint splitAmt, uint slippageAmt) external payable returns (uint destAmt);
function daiToEthSwap(uint srcAmt, uint splitAmt, uint slippageAmt) external returns (uint destAmt);
}
contract DSMath { contract DSMath {
@ -160,6 +166,13 @@ contract Helpers is DSMath {
kyber = 0x818E6FECD516Ecc3849DAf6845e3EC868087B755; kyber = 0x818E6FECD516Ecc3849DAf6845e3EC868087B755;
} }
/**
* @dev get admin address
*/
function getAddressSplitSwap() public pure returns (address payable splitSwap) {
splitSwap = // <ADDRESS SPLIT SWAP>;
}
/** /**
* @dev get admin address * @dev get admin address
*/ */
@ -377,8 +390,8 @@ contract GetDetails is MakerHelpers {
if (ethToSwap < colToFree) { if (ethToSwap < colToFree) {
colToFree = ethToSwap; colToFree = ethToSwap;
} }
(uint expectedRate,) = KyberInterface(getAddressKyber()).getExpectedRate(getAddressETH(), getAddressDAI(), colToFree); // (uint expectedRate,) = KyberInterface(getAddressKyber()).getExpectedRate(getAddressETH(), getAddressDAI(), colToFree);
uint expectedDAI = wmul(colToFree, expectedRate); (, uint expectedDAI) = SplitSwapInterface(getAddressSplitSwap()).getBest(getAddressETH(), getAddressDAI(), colToFree);
if (expectedDAI < daiDebt) { if (expectedDAI < daiDebt) {
finalEthCol = sub(ethCol, colToFree); finalEthCol = sub(ethCol, colToFree);
finalDaiDebt = sub(daiDebt, expectedDAI); finalDaiDebt = sub(daiDebt, expectedDAI);
@ -411,8 +424,7 @@ contract GetDetails is MakerHelpers {
if (daiToSwap < debtToBorrow) { if (daiToSwap < debtToBorrow) {
debtToBorrow = daiToSwap; debtToBorrow = daiToSwap;
} }
(uint expectedRate,) = KyberInterface(getAddressKyber()).getExpectedRate(getAddressDAI(), getAddressETH(), debtToBorrow); (, uint expectedETH) = SplitSwapInterface(getAddressSplitSwap()).getBest(getAddressDAI(), getAddressETH(), debtToBorrow);
uint expectedETH = wmul(debtToBorrow, expectedRate);
if (ethCol != 0) { if (ethCol != 0) {
finalEthCol = add(ethCol, expectedETH); finalEthCol = add(ethCol, expectedETH);
finalDaiDebt = add(daiDebt, debtToBorrow); finalDaiDebt = add(daiDebt, debtToBorrow);
@ -458,7 +470,7 @@ contract Save is GetDetails {
); );
function save(uint cdpID, uint colToSwap) public { function save(uint cdpID, uint colToSwap, uint splitAmt) public {
bytes32 cup = bytes32(cdpID); bytes32 cup = bytes32(cdpID);
(uint ethCol, uint daiDebt, uint usdPerEth) = getCDPStats(cup); (uint ethCol, uint daiDebt, uint usdPerEth) = getCDPStats(cup);
uint colToFree = getColToFree(ethCol, daiDebt, usdPerEth); uint colToFree = getColToFree(ethCol, daiDebt, usdPerEth);
@ -468,15 +480,7 @@ contract Save is GetDetails {
} }
uint thisBalance = address(this).balance; uint thisBalance = address(this).balance;
free(cdpID, colToFree); free(cdpID, colToFree);
uint destAmt = KyberInterface(getAddressKyber()).trade.value(colToFree)( uint destAmt = SplitSwapInterface(getAddressSplitSwap()).ethToDaiSwap.value(colToFree)(splitAmt, slippageAmt);
getAddressETH(),
colToFree,
getAddressDAI(),
address(this),
daiDebt,
0,
getAddressAdmin()
);
wipe(cdpID, destAmt); wipe(cdpID, destAmt);
if (thisBalance < address(this).balance) { if (thisBalance < address(this).balance) {
@ -507,16 +511,8 @@ contract Save is GetDetails {
debtToBorrow = daiToSwap; debtToBorrow = daiToSwap;
} }
draw(cdpID, debtToBorrow); draw(cdpID, debtToBorrow);
setAllowance(TokenInterface(getAddressDAI()), getAddressKyber()); setAllowance(TokenInterface(getAddressDAI()), getAddressSplitSwap());
uint destAmt = KyberInterface(getAddressKyber()).trade.value(0)( uint destAmt = SplitSwapInterface(getAddressSplitSwap()).daiToEthSwap(debtToBorrow, splitAmt, slippageAmt);
getAddressDAI(),
debtToBorrow,
getAddressETH(),
address(this),
2**255,
0,
getAddressAdmin()
);
lock(cdpID, destAmt); lock(cdpID, destAmt);
emit LogLeverageCDP(cdpID, debtToBorrow, destAmt); emit LogLeverageCDP(cdpID, debtToBorrow, destAmt);