mirror of
https://github.com/Instadapp/InstaContract.git
synced 2024-07-29 22:47:45 +00:00
MaxDestAmt Implementation in Maker - Kyber Interaction.
This commit is contained in:
parent
70f48cb607
commit
46ba513ad3
|
@ -29,13 +29,20 @@ interface WETHFace {
|
||||||
function withdraw(uint wad) external;
|
function withdraw(uint wad) external;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface MoatKyber {
|
interface InstaKyber {
|
||||||
function executeTrade(
|
function executeTrade(
|
||||||
address src,
|
address src,
|
||||||
address dest,
|
address dest,
|
||||||
uint srcAmt,
|
uint srcAmt,
|
||||||
uint minConversionRate
|
uint minConversionRate,
|
||||||
|
uint maxDestAmt
|
||||||
) external payable returns (uint destAmt);
|
) external payable returns (uint destAmt);
|
||||||
|
|
||||||
|
function getExpectedPrice(
|
||||||
|
address src,
|
||||||
|
address dest,
|
||||||
|
uint srcAmt
|
||||||
|
) external view returns (uint, uint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -128,47 +135,58 @@ contract RepayLoan is IssueLoan {
|
||||||
|
|
||||||
function repay(
|
function repay(
|
||||||
uint daiWipe,
|
uint daiWipe,
|
||||||
uint ethFree,
|
uint ethFree
|
||||||
uint mkrFees, // either this...
|
// bool mkrFees, // either this...
|
||||||
uint feeMinConRate // or this is 0
|
// uint feeMinConRate // or this is 0
|
||||||
) public payable
|
) public payable
|
||||||
{
|
{
|
||||||
if (daiWipe > 0) {wipeDAI(daiWipe, mkrFees, feeMinConRate);}
|
if (daiWipe > 0) {wipeDAI(daiWipe);}
|
||||||
if (ethFree > 0) {unlockETH(ethFree);}
|
if (ethFree > 0) {unlockETH(ethFree);}
|
||||||
}
|
}
|
||||||
|
|
||||||
function wipeDAI(uint daiWipe, uint mkrFees, uint feeMinConRate) public payable {
|
function wipeDAI(uint daiWipe) public payable {
|
||||||
IERC20 daiTkn = IERC20(getAddress("dai"));
|
address dai = getAddress("dai");
|
||||||
IERC20 mkrTkn = IERC20(getAddress("mkr"));
|
address mkr = getAddress("mkr");
|
||||||
|
address eth = getAddress("eth");
|
||||||
|
|
||||||
// MKR now balance
|
IERC20 daiTkn = IERC20(dai);
|
||||||
uint nowBal = mkrTkn.balanceOf(address(this));
|
IERC20 mkrTkn = IERC20(mkr);
|
||||||
|
|
||||||
// fetch DAI
|
// contract MKR balance before wiping
|
||||||
|
uint contractMKR = mkrTkn.balanceOf(address(this));
|
||||||
|
// get DAI
|
||||||
daiTkn.transferFrom(msg.sender, address(this), daiWipe); // DAI to pay the debt
|
daiTkn.transferFrom(msg.sender, address(this), daiWipe); // DAI to pay the debt
|
||||||
// wipe DAI
|
// wipe DAI
|
||||||
loanMaster.wipe(cdps[msg.sender], daiWipe);
|
loanMaster.wipe(cdps[msg.sender], daiWipe);
|
||||||
|
// MKR fee = before wiping bal - after wiping bal
|
||||||
|
uint mkrCharged = contractMKR - mkrTkn.balanceOf(address(this));
|
||||||
|
|
||||||
// MKR after wiping
|
// claiming paid MKR back
|
||||||
uint mkrCharged = nowBal - mkrTkn.balanceOf(address(this));
|
if (msg.value > 0) {
|
||||||
|
// Interacting with Kyber to swap ETH with MKR
|
||||||
// if fees paid in MKR
|
InstaKyber instak = InstaKyber(getAddress("InstaKyber"));
|
||||||
if (mkrFees > 0) {
|
uint minRate;
|
||||||
mkrTkn.transferFrom(msg.sender, address(this), mkrCharged); // user paying MKR fees
|
(, minRate) = instak.getExpectedPrice(eth, mkr, msg.value);
|
||||||
} else { // otherwise swap ETH via MoatKyber
|
uint mkrBought = instak.executeTrade.value(msg.value)(
|
||||||
MoatKyber mtky = MoatKyber(getAddress("moatkyber"));
|
eth,
|
||||||
uint mkrBought = mtky.executeTrade.value(msg.value)(
|
mkr,
|
||||||
getAddress("eth"),
|
|
||||||
getAddress("mkr"),
|
|
||||||
msg.value,
|
msg.value,
|
||||||
feeMinConRate
|
minRate,
|
||||||
|
mkrCharged
|
||||||
);
|
);
|
||||||
if (mkrBought > mkrCharged) {
|
|
||||||
mkrTkn.transfer(msg.sender, mkrBought - mkrCharged); // pay back balanced MKR tokens
|
require(mkrCharged == mkrBought, "ETH not sufficient to cover the MKR fees.");
|
||||||
}
|
|
||||||
|
// the ether will always belong to sender as there's no way contract can accept ether
|
||||||
|
if (address(this).balance > 0) {
|
||||||
|
msg.sender.transfer(address(this).balance);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// take MKR directly from address
|
||||||
|
mkrTkn.transferFrom(msg.sender, address(this), mkrCharged); // user paying MKR fees
|
||||||
}
|
}
|
||||||
|
|
||||||
require(mkrTkn.balanceOf(address(this)) == nowBal, "MKR balance not reimbursed");
|
|
||||||
emit WipedDAI(msg.sender, daiWipe, mkrCharged);
|
emit WipedDAI(msg.sender, daiWipe, mkrCharged);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,17 +248,6 @@ contract MoatMaker is BorrowTasks {
|
||||||
approveERC20();
|
approveERC20();
|
||||||
}
|
}
|
||||||
|
|
||||||
function () public payable {}
|
|
||||||
|
|
||||||
function collectAsset(address tokenAddress, uint amount) public onlyAdmin {
|
|
||||||
if (tokenAddress == getAddress("eth")) {
|
|
||||||
msg.sender.transfer(amount);
|
|
||||||
} else {
|
|
||||||
IERC20 tokenFunctions = IERC20(tokenAddress);
|
|
||||||
tokenFunctions.transfer(msg.sender, amount);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function freeze(bool stop) public onlyAdmin {
|
function freeze(bool stop) public onlyAdmin {
|
||||||
freezed = stop;
|
freezed = stop;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user