mirror of
https://github.com/Instadapp/smart-contract.git
synced 2024-07-29 22:08:07 +00:00
fixed dai bridge
This commit is contained in:
parent
97af51bb76
commit
7e175c2a88
|
|
@ -1,25 +1,77 @@
|
||||||
pragma solidity ^0.5.7;
|
pragma solidity ^0.5.7;
|
||||||
|
|
||||||
interface TubInterface {
|
interface GemLike {
|
||||||
function open() external returns (bytes32);
|
function approve(address, uint) external;
|
||||||
function join(uint) external;
|
function transfer(address, uint) external;
|
||||||
function exit(uint) external;
|
function transferFrom(address, address, uint) external;
|
||||||
function lock(bytes32, uint) external;
|
function deposit() external payable;
|
||||||
function free(bytes32, uint) external;
|
function withdraw(uint) external;
|
||||||
function draw(bytes32, uint) external;
|
}
|
||||||
function wipe(bytes32, uint) external;
|
|
||||||
function give(bytes32, address) external;
|
interface ManagerLike {
|
||||||
function shut(bytes32) external;
|
function cdpCan(address, uint, address) external view returns (uint);
|
||||||
function cups(bytes32) external view returns (address, uint, uint, uint);
|
function ilks(uint) external view returns (bytes32);
|
||||||
function gem() external view returns (TokenInterface);
|
function owns(uint) external view returns (address);
|
||||||
function gov() external view returns (TokenInterface);
|
function urns(uint) external view returns (address);
|
||||||
function skr() external view returns (TokenInterface);
|
function vat() external view returns (address);
|
||||||
function sai() external view returns (TokenInterface);
|
function open(bytes32, address) external returns (uint);
|
||||||
function ink(bytes32) external view returns (uint);
|
function give(uint, address) external;
|
||||||
function tab(bytes32) external returns (uint);
|
function cdpAllow(uint, address, uint) external;
|
||||||
function rap(bytes32) external returns (uint);
|
function urnAllow(address, uint) external;
|
||||||
function per() external view returns (uint);
|
function frob(uint, int, int) external;
|
||||||
function pep() external view returns (PepInterface);
|
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;
|
||||||
|
function gem(bytes32, address) external view returns (uint);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
interface GemJoinLike {
|
||||||
|
function dec() external returns (uint);
|
||||||
|
function gem() external returns (GemLike);
|
||||||
|
function join(address, uint) external payable;
|
||||||
|
function exit(address, uint) external;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DaiJoinLike {
|
||||||
|
function vat() external returns (VatLike);
|
||||||
|
function dai() external returns (GemLike);
|
||||||
|
function join(address, uint) external payable;
|
||||||
|
function exit(address, uint) external;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface HopeLike {
|
||||||
|
function hope(address) external;
|
||||||
|
function nope(address) external;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface JugLike {
|
||||||
|
function drip(bytes32) external returns (uint);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TokenInterface {
|
interface TokenInterface {
|
||||||
|
|
@ -105,6 +157,25 @@ interface CompOracleInterface {
|
||||||
function getUnderlyingPrice(address) external view returns (uint);
|
function getUnderlyingPrice(address) external view returns (uint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface InstaMcdAddress {
|
||||||
|
function manager() external view returns (address);
|
||||||
|
function dai() external view returns (address);
|
||||||
|
function daiJoin() external view returns (address);
|
||||||
|
function vat() external view returns (address);
|
||||||
|
function jug() external view returns (address);
|
||||||
|
function ethAJoin() external view returns (address);
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OtcInterface {
|
||||||
|
function getPayAmount(address, address, uint) external view returns (uint);
|
||||||
|
function buyAllAmount(
|
||||||
|
address,
|
||||||
|
uint,
|
||||||
|
address,
|
||||||
|
uint
|
||||||
|
) external;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
contract DSMath {
|
contract DSMath {
|
||||||
|
|
||||||
|
|
@ -139,6 +210,15 @@ contract DSMath {
|
||||||
z = add(mul(x, WAD), y / 2) / y;
|
z = add(mul(x, WAD), y / 2) / y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function toInt(uint x) internal pure returns (int y) {
|
||||||
|
y = int(x);
|
||||||
|
require(y >= 0, "int-overflow");
|
||||||
|
}
|
||||||
|
|
||||||
|
function toRad(uint wad) internal pure returns (uint rad) {
|
||||||
|
rad = mul(wad, 10 ** 27);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -152,31 +232,10 @@ contract Helper is DSMath {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev get MakerDAO CDP engine
|
* @dev get MakerDAO MCD Address contract
|
||||||
*/
|
*/
|
||||||
function getSaiTubAddress() public pure returns (address sai) {
|
function getMcdAddresses() public pure returns (address mcd) {
|
||||||
sai = 0x448a5065aeBB8E423F0896E6c5D525C040f59af3;
|
mcd = 0xF23196DF1C440345DE07feFbe556a5eF0dcD29F0;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev get MakerDAO Oracle for ETH price
|
|
||||||
*/
|
|
||||||
function getOracleAddress() public pure returns (address oracle) {
|
|
||||||
oracle = 0x729D19f657BD0614b4985Cf1D82531c67569197B;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev get uniswap MKR exchange
|
|
||||||
*/
|
|
||||||
function getUniswapMKRExchange() public pure returns (address ume) {
|
|
||||||
ume = 0x2C4Bd064b998838076fa341A83d007FC2FA50957;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev get uniswap DAI exchange
|
|
||||||
*/
|
|
||||||
function getUniswapDAIExchange() public pure returns (address ude) {
|
|
||||||
ude = 0x09cabEC1eAd1c0Ba254B09efb3EE13841712bE14;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -193,13 +252,6 @@ contract Helper is DSMath {
|
||||||
troller = 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B;
|
troller = 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev get Compound Oracle Address
|
|
||||||
*/
|
|
||||||
function getCompOracleAddress() public pure returns (address troller) {
|
|
||||||
troller = 0xe7664229833AE4Abf4E269b8F23a86B657E2338D;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev get CETH Address
|
* @dev get CETH Address
|
||||||
*/
|
*/
|
||||||
|
|
@ -211,21 +263,14 @@ contract Helper is DSMath {
|
||||||
* @dev get DAI Address
|
* @dev get DAI Address
|
||||||
*/
|
*/
|
||||||
function getDAIAddress() public pure returns (address dai) {
|
function getDAIAddress() public pure returns (address dai) {
|
||||||
dai = 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359;
|
dai = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev get MKR Address
|
|
||||||
*/
|
|
||||||
function getMKRAddress() public pure returns (address dai) {
|
|
||||||
dai = 0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev get CDAI Address
|
* @dev get CDAI Address
|
||||||
*/
|
*/
|
||||||
function getCDAIAddress() public pure returns (address cDai) {
|
function getCDAIAddress() public pure returns (address cDai) {
|
||||||
cDai = 0xF5DCe57282A584D2746FaF1593d3121Fcac444dC;
|
cDai = 0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -242,14 +287,34 @@ contract Helper is DSMath {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract MakerHelper is Helper {
|
contract InstaPoolResolver is Helper {
|
||||||
|
|
||||||
|
function accessDai(uint daiAmt, bool isCompound) internal {
|
||||||
|
address[] memory borrowAddr = new address[](1);
|
||||||
|
uint[] memory borrowAmt = new uint[](1);
|
||||||
|
borrowAddr[0] = getCDAIAddress();
|
||||||
|
borrowAmt[0] = daiAmt;
|
||||||
|
PoolInterface(getPoolAddr()).accessToken(borrowAddr, borrowAmt, isCompound);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function returnDai(uint daiAmt, bool isCompound) internal {
|
||||||
|
address[] memory borrowAddr = new address[](1);
|
||||||
|
borrowAddr[0] = getCDAIAddress();
|
||||||
|
require(TokenInterface(getDAIAddress()).transfer(getPoolAddr(), daiAmt), "Not-enough-DAI");
|
||||||
|
PoolInterface(getPoolAddr()).paybackToken(borrowAddr, isCompound);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
contract MakerHelper is InstaPoolResolver {
|
||||||
|
|
||||||
event LogOpen(uint cdpNum, address owner);
|
event LogOpen(uint cdpNum, address owner);
|
||||||
event LogLock(uint cdpNum, uint amtETH, uint amtPETH, address owner);
|
event LogLock(uint cdpNum, uint amtETH, address owner);
|
||||||
event LogFree(uint cdpNum, uint amtETH, uint amtPETH, address owner);
|
event LogFree(uint cdpNum, uint amtETH, address owner);
|
||||||
event LogDraw(uint cdpNum, uint amtDAI, address owner);
|
event LogDraw(uint cdpNum, uint daiAmt, address owner);
|
||||||
event LogWipe(uint cdpNum, uint daiAmt, uint mkrFee, uint daiFee, address owner);
|
event LogWipe(uint cdpNum, uint daiAmt, address owner);
|
||||||
event LogShut(uint cdpNum);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Allowance to Maker's contract
|
* @dev Allowance to Maker's contract
|
||||||
|
|
@ -261,13 +326,77 @@ contract MakerHelper is Helper {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev CDP stats by Bytes
|
* @dev Check if entered amt is valid or not (Used in makerToCompound)
|
||||||
*/
|
*/
|
||||||
function getCDPStats(bytes32 cup) internal view returns (uint ethCol, uint daiDebt) {
|
function checkVault(uint id, uint ethAmt, uint daiAmt) internal view returns (uint ethCol, uint daiDebt) {
|
||||||
TubInterface tub = TubInterface(getSaiTubAddress());
|
address manager = InstaMcdAddress(getMcdAddresses()).manager();
|
||||||
(, uint pethCol, uint debt,) = tub.cups(cup);
|
address urn = ManagerLike(manager).urns(id);
|
||||||
ethCol = rmul(pethCol, tub.per()); // get ETH col from PETH col
|
bytes32 ilk = ManagerLike(manager).ilks(id);
|
||||||
daiDebt = debt;
|
uint art = 0;
|
||||||
|
(ethCol, art) = VatLike(ManagerLike(manager).vat()).urns(ilk, urn);
|
||||||
|
(,uint rate,,,) = VatLike(ManagerLike(manager).vat()).ilks(ilk);
|
||||||
|
daiDebt = rmul(art,rate);
|
||||||
|
daiDebt = daiAmt < daiDebt ? daiAmt : daiDebt; // if DAI amount > max debt. Set max debt
|
||||||
|
ethCol = ethAmt < ethCol ? ethAmt : ethCol; // if ETH amount > max Col. Set max col
|
||||||
|
}
|
||||||
|
|
||||||
|
function joinDaiJoin(address urn, uint wad) internal {
|
||||||
|
address daiJoin = InstaMcdAddress(getMcdAddresses()).daiJoin();
|
||||||
|
// Approves adapter to take the DAI amount
|
||||||
|
DaiJoinLike(daiJoin).dai().approve(daiJoin, wad);
|
||||||
|
// Joins DAI into the vat
|
||||||
|
DaiJoinLike(daiJoin).join(urn, wad);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _getDrawDart(
|
||||||
|
address vat,
|
||||||
|
address jug,
|
||||||
|
address urn,
|
||||||
|
bytes32 ilk,
|
||||||
|
uint wad
|
||||||
|
) internal returns (int dart)
|
||||||
|
{
|
||||||
|
// Updates stability fee rate
|
||||||
|
uint rate = JugLike(jug).drip(ilk);
|
||||||
|
|
||||||
|
// Gets DAI balance of the urn in the vat
|
||||||
|
uint dai = VatLike(vat).dai(urn);
|
||||||
|
|
||||||
|
// If there was already enough DAI in the vat balance, just exits it without adding more debt
|
||||||
|
if (dai < mul(wad, RAY)) {
|
||||||
|
// Calculates the needed dart so together with the existing dai in the vat is enough to exit wad amount of DAI tokens
|
||||||
|
dart = toInt(sub(mul(wad, RAY), dai) / rate);
|
||||||
|
// This is neeeded due lack of precision. It might need to sum an extra dart wei (for the given DAI wad amount)
|
||||||
|
dart = mul(uint(dart), rate) < mul(wad, RAY) ? dart + 1 : dart;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _getWipeDart(
|
||||||
|
address vat,
|
||||||
|
uint dai,
|
||||||
|
address urn,
|
||||||
|
bytes32 ilk
|
||||||
|
) internal view returns (int dart)
|
||||||
|
{
|
||||||
|
// Gets actual rate from the vat
|
||||||
|
(, uint rate,,,) = VatLike(vat).ilks(ilk);
|
||||||
|
// Gets actual art value of the urn
|
||||||
|
(, uint art) = VatLike(vat).urns(ilk, urn);
|
||||||
|
|
||||||
|
// Uses the whole dai balance in the vat to reduce the debt
|
||||||
|
dart = toInt(dai / rate);
|
||||||
|
// Checks the calculated dart is not higher than urn.art (total debt), otherwise uses its value
|
||||||
|
dart = uint(dart) <= art ? - dart : - toInt(art);
|
||||||
|
}
|
||||||
|
|
||||||
|
function joinEthJoin(address urn, uint _wad) internal {
|
||||||
|
address ethJoin = InstaMcdAddress(getMcdAddresses()).ethAJoin();
|
||||||
|
// Wraps ETH in WETH
|
||||||
|
GemJoinLike(ethJoin).gem().deposit.value(_wad)();
|
||||||
|
// Approves adapter to take the WETH amount
|
||||||
|
GemJoinLike(ethJoin).gem().approve(address(ethJoin), _wad);
|
||||||
|
// Joins WETH collateral into the vat
|
||||||
|
GemJoinLike(ethJoin).join(urn, _wad);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -302,219 +431,146 @@ contract CompoundHelper is MakerHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract InstaPoolResolver is CompoundHelper {
|
contract MakerResolver is CompoundHelper {
|
||||||
|
function flux(uint cdp, address dst, uint wad) internal {
|
||||||
function accessDai(uint daiAmt, bool isCompound) internal {
|
address manager = InstaMcdAddress(getMcdAddresses()).manager();
|
||||||
address[] memory borrowAddr = new address[](1);
|
ManagerLike(manager).flux(cdp, dst, wad);
|
||||||
uint[] memory borrowAmt = new uint[](1);
|
|
||||||
borrowAddr[0] = getCDAIAddress();
|
|
||||||
borrowAmt[0] = daiAmt;
|
|
||||||
PoolInterface(getPoolAddr()).accessToken(borrowAddr, borrowAmt, isCompound);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function returnDai(uint daiAmt, bool isCompound) internal {
|
function move(uint cdp, address dst, uint rad) public {
|
||||||
address[] memory borrowAddr = new address[](1);
|
address manager = InstaMcdAddress(getMcdAddresses()).manager();
|
||||||
borrowAddr[0] = getCDAIAddress();
|
ManagerLike(manager).move(cdp, dst, rad);
|
||||||
require(TokenInterface(getDAIAddress()).transfer(getPoolAddr(), daiAmt), "Not-enough-DAI");
|
|
||||||
PoolInterface(getPoolAddr()).paybackToken(borrowAddr, isCompound);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
function frob(uint cdp, int dink, int dart) internal {
|
||||||
|
address manager = InstaMcdAddress(getMcdAddresses()).manager();
|
||||||
|
ManagerLike(manager).frob(cdp, dink, dart);
|
||||||
contract MakerResolver is InstaPoolResolver {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Open new CDP
|
|
||||||
*/
|
|
||||||
function open() internal returns (uint) {
|
|
||||||
bytes32 cup = TubInterface(getSaiTubAddress()).open();
|
|
||||||
emit LogOpen(uint(cup), address(this));
|
|
||||||
return uint(cup);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function open() public returns (uint cdp) {
|
||||||
* @dev transfer CDP ownership
|
address manager = InstaMcdAddress(getMcdAddresses()).manager();
|
||||||
*/
|
bytes32 ilk = 0x4554482d41000000000000000000000000000000000000000000000000000000;
|
||||||
function give(uint cdpNum, address nextOwner) internal {
|
cdp = ManagerLike(manager).open(ilk, address(this));
|
||||||
TubInterface(getSaiTubAddress()).give(bytes32(cdpNum), nextOwner);
|
emit LogOpen(cdp, address(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
function setWipeAllowances(TubInterface tub) internal { // to solve stack to deep error
|
function give(uint cdp, address usr) public {
|
||||||
TokenInterface dai = tub.sai();
|
address manager = InstaMcdAddress(getMcdAddresses()).manager();
|
||||||
TokenInterface mkr = tub.gov();
|
ManagerLike(manager).give(cdp, usr);
|
||||||
setMakerAllowance(dai, getSaiTubAddress());
|
|
||||||
setMakerAllowance(mkr, getSaiTubAddress());
|
|
||||||
setMakerAllowance(dai, getUniswapDAIExchange());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function lock(uint cdp, uint _wad) internal {
|
||||||
* @dev Pay CDP debt
|
address manager = InstaMcdAddress(getMcdAddresses()).manager();
|
||||||
*/
|
// Receives ETH amount, converts it to WETH and joins it into the vat
|
||||||
function wipe(uint cdpNum, uint _wad, bool isCompound) internal returns (uint daiAmt) {
|
joinEthJoin(address(this), _wad);
|
||||||
if (_wad > 0) {
|
// Locks WETH amount into the CDP
|
||||||
TubInterface tub = TubInterface(getSaiTubAddress());
|
VatLike(ManagerLike(manager).vat()).frob(
|
||||||
UniswapExchange daiEx = UniswapExchange(getUniswapDAIExchange());
|
ManagerLike(manager).ilks(cdp),
|
||||||
UniswapExchange mkrEx = UniswapExchange(getUniswapMKRExchange());
|
ManagerLike(manager).urns(cdp),
|
||||||
|
address(this),
|
||||||
|
address(this),
|
||||||
|
toInt(_wad),
|
||||||
|
0
|
||||||
|
);
|
||||||
|
emit LogLock(cdp, _wad, address(this));
|
||||||
|
}
|
||||||
|
|
||||||
bytes32 cup = bytes32(cdpNum);
|
function free(uint cdp, uint wad) internal {
|
||||||
|
address ethJoin = InstaMcdAddress(getMcdAddresses()).ethAJoin();
|
||||||
(address lad,,,) = tub.cups(cup);
|
// Unlocks WETH amount from the CDP
|
||||||
require(lad == address(this), "cup-not-owned");
|
frob(
|
||||||
|
cdp,
|
||||||
setWipeAllowances(tub);
|
-toInt(wad),
|
||||||
(bytes32 val, bool ok) = tub.pep().peek();
|
0
|
||||||
|
);
|
||||||
// MKR required for wipe = Stability fees accrued in Dai / MKRUSD value
|
// Moves the amount from the CDP urn to proxy's address
|
||||||
uint mkrFee = wdiv(rmul(_wad, rdiv(tub.rap(cup), tub.tab(cup))), uint(val));
|
flux(
|
||||||
|
cdp,
|
||||||
uint daiFeeAmt = daiEx.getTokenToEthOutputPrice(mkrEx.getEthToTokenOutputPrice(mkrFee));
|
address(this),
|
||||||
daiAmt = add(_wad, daiFeeAmt);
|
wad
|
||||||
|
);
|
||||||
// Getting Liquidity from Liquidity Contract
|
// Exits WETH amount to proxy address as a token
|
||||||
accessDai(daiAmt, isCompound);
|
GemJoinLike(ethJoin).exit(address(this), wad);
|
||||||
|
// Converts WETH to ETH
|
||||||
if (ok && val != 0) {
|
GemJoinLike(ethJoin).gem().withdraw(wad);
|
||||||
daiEx.tokenToTokenSwapOutput(
|
emit LogFree(cdp, wad, address(this));
|
||||||
mkrFee,
|
}
|
||||||
daiFeeAmt,
|
|
||||||
uint(999000000000000000000),
|
|
||||||
uint(1899063809), // 6th March 2030 GMT // no logic
|
|
||||||
getMKRAddress()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
tub.wipe(cup, _wad);
|
|
||||||
|
|
||||||
emit LogWipe(
|
|
||||||
cdpNum,
|
|
||||||
daiAmt,
|
|
||||||
mkrFee,
|
|
||||||
daiFeeAmt,
|
|
||||||
address(this)
|
|
||||||
);
|
|
||||||
|
|
||||||
|
function draw(uint cdp, uint wad) internal {
|
||||||
|
address manager = InstaMcdAddress(getMcdAddresses()).manager();
|
||||||
|
address jug = InstaMcdAddress(getMcdAddresses()).jug();
|
||||||
|
address daiJoin = InstaMcdAddress(getMcdAddresses()).daiJoin();
|
||||||
|
address urn = ManagerLike(manager).urns(cdp);
|
||||||
|
address vat = ManagerLike(manager).vat();
|
||||||
|
bytes32 ilk = ManagerLike(manager).ilks(cdp);
|
||||||
|
// Generates debt in the CDP
|
||||||
|
frob(
|
||||||
|
cdp,
|
||||||
|
0,
|
||||||
|
_getDrawDart(
|
||||||
|
vat,
|
||||||
|
jug,
|
||||||
|
urn,
|
||||||
|
ilk,
|
||||||
|
wad
|
||||||
|
)
|
||||||
|
);
|
||||||
|
// Moves the DAI amount (balance in the vat in rad) to proxy's address
|
||||||
|
move(
|
||||||
|
cdp,
|
||||||
|
address(this),
|
||||||
|
toRad(wad)
|
||||||
|
);
|
||||||
|
// Allows adapter to access to proxy's DAI balance in the vat
|
||||||
|
if (VatLike(vat).can(address(this), address(daiJoin)) == 0) {
|
||||||
|
VatLike(vat).hope(daiJoin);
|
||||||
}
|
}
|
||||||
|
// Exits DAI to the user's wallet as a token
|
||||||
|
DaiJoinLike(daiJoin).exit(address(this), wad);
|
||||||
|
emit LogDraw(cdp, wad, address(this));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function wipe(uint cdp, uint wad) internal {
|
||||||
* @dev Pay CDP debt
|
address manager = InstaMcdAddress(getMcdAddresses()).manager();
|
||||||
*/
|
address vat = ManagerLike(manager).vat();
|
||||||
function wipeWithMkr(uint cdpNum, uint _wad, bool isCompound) internal {
|
address urn = ManagerLike(manager).urns(cdp);
|
||||||
if (_wad > 0) {
|
bytes32 ilk = ManagerLike(manager).ilks(cdp);
|
||||||
TubInterface tub = TubInterface(getSaiTubAddress());
|
|
||||||
TokenInterface dai = tub.sai();
|
|
||||||
TokenInterface mkr = tub.gov();
|
|
||||||
|
|
||||||
bytes32 cup = bytes32(cdpNum);
|
address own = ManagerLike(manager).owns(cdp);
|
||||||
|
if (own == address(this) || ManagerLike(manager).cdpCan(own, cdp, address(this)) == 1) {
|
||||||
(address lad,,,) = tub.cups(cup);
|
// Joins DAI amount into the vat
|
||||||
require(lad == address(this), "cup-not-owned");
|
joinDaiJoin(urn, wad);
|
||||||
|
// Paybacks debt to the CDP
|
||||||
setMakerAllowance(dai, getSaiTubAddress());
|
frob(
|
||||||
setMakerAllowance(mkr, getSaiTubAddress());
|
cdp,
|
||||||
|
|
||||||
(bytes32 val, bool ok) = tub.pep().peek();
|
|
||||||
|
|
||||||
// MKR required for wipe = Stability fees accrued in Dai / MKRUSD value
|
|
||||||
uint mkrFee = wdiv(rmul(_wad, rdiv(tub.rap(cup), tub.tab(cup))), uint(val));
|
|
||||||
|
|
||||||
// Getting Liquidity from Liquidity Contract
|
|
||||||
accessDai(_wad, isCompound);
|
|
||||||
|
|
||||||
if (ok && val != 0) {
|
|
||||||
require(mkr.transferFrom(msg.sender, address(this), mkrFee), "MKR-Allowance?");
|
|
||||||
}
|
|
||||||
|
|
||||||
tub.wipe(cup, _wad);
|
|
||||||
|
|
||||||
emit LogWipe(
|
|
||||||
cdpNum,
|
|
||||||
_wad,
|
|
||||||
mkrFee,
|
|
||||||
0,
|
0,
|
||||||
address(this)
|
_getWipeDart(
|
||||||
|
vat,
|
||||||
|
VatLike(vat).dai(urn),
|
||||||
|
urn,
|
||||||
|
ilk
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// Joins DAI amount into the vat
|
||||||
|
joinDaiJoin(address(this), wad);
|
||||||
|
// Paybacks debt to the CDP
|
||||||
|
VatLike(vat).frob(
|
||||||
|
ilk,
|
||||||
|
urn,
|
||||||
|
address(this),
|
||||||
|
address(this),
|
||||||
|
0,
|
||||||
|
_getWipeDart(
|
||||||
|
vat,
|
||||||
|
wad * RAY,
|
||||||
|
urn,
|
||||||
|
ilk
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
emit LogWipe(cdp, wad, address(this));
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Withdraw CDP
|
|
||||||
*/
|
|
||||||
function free(uint cdpNum, uint jam) internal {
|
|
||||||
if (jam > 0) {
|
|
||||||
bytes32 cup = bytes32(cdpNum);
|
|
||||||
address tubAddr = getSaiTubAddress();
|
|
||||||
|
|
||||||
TubInterface tub = TubInterface(tubAddr);
|
|
||||||
TokenInterface peth = tub.skr();
|
|
||||||
TokenInterface weth = tub.gem();
|
|
||||||
|
|
||||||
uint ink = rdiv(jam, tub.per());
|
|
||||||
ink = rmul(ink, tub.per()) <= jam ? ink : ink - 1;
|
|
||||||
tub.free(cup, ink);
|
|
||||||
|
|
||||||
setMakerAllowance(peth, tubAddr);
|
|
||||||
|
|
||||||
tub.exit(ink);
|
|
||||||
uint freeJam = weth.balanceOf(address(this)); // withdraw possible previous stuck WETH as well
|
|
||||||
weth.withdraw(freeJam);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Deposit Collateral
|
|
||||||
*/
|
|
||||||
function lock(uint cdpNum, uint ethAmt) internal {
|
|
||||||
if (ethAmt > 0) {
|
|
||||||
bytes32 cup = bytes32(cdpNum);
|
|
||||||
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(ethAmt)();
|
|
||||||
|
|
||||||
uint ink = rdiv(ethAmt, tub.per());
|
|
||||||
ink = rmul(ink, tub.per()) <= ethAmt ? ink : ink - 1;
|
|
||||||
|
|
||||||
setMakerAllowance(weth, tubAddr);
|
|
||||||
tub.join(ink);
|
|
||||||
|
|
||||||
setMakerAllowance(peth, tubAddr);
|
|
||||||
tub.lock(cup, ink);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Borrow DAI Debt
|
|
||||||
*/
|
|
||||||
function draw(uint cdpNum, uint _wad, bool isCompound) internal {
|
|
||||||
bytes32 cup = bytes32(cdpNum);
|
|
||||||
if (_wad > 0) {
|
|
||||||
TubInterface tub = TubInterface(getSaiTubAddress());
|
|
||||||
|
|
||||||
tub.draw(cup, _wad);
|
|
||||||
|
|
||||||
// Returning Liquidity To Liquidity Contract
|
|
||||||
returnDai(_wad, isCompound);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Check if entered amt is valid or not (Used in makerToCompound)
|
|
||||||
*/
|
|
||||||
function checkCDP(bytes32 cup, uint ethAmt, uint daiAmt) internal returns (uint ethCol, uint daiDebt) {
|
|
||||||
TubInterface tub = TubInterface(getSaiTubAddress());
|
|
||||||
ethCol = rmul(tub.ink(cup), tub.per()) - 1; // get ETH col from PETH col
|
|
||||||
daiDebt = tub.tab(cup);
|
|
||||||
daiDebt = daiAmt < daiDebt ? daiAmt : daiDebt; // if DAI amount > max debt. Set max debt
|
|
||||||
ethCol = ethAmt < ethCol ? ethAmt : ethCol; // if ETH amount > max Col. Set max col
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -524,16 +580,11 @@ contract MakerResolver is InstaPoolResolver {
|
||||||
uint cdpNum,
|
uint cdpNum,
|
||||||
uint jam,
|
uint jam,
|
||||||
uint _wad,
|
uint _wad,
|
||||||
bool isCompound,
|
bool isCompound
|
||||||
bool feeInMkr
|
) internal
|
||||||
) internal returns (uint daiAmt)
|
|
||||||
{
|
{
|
||||||
if (feeInMkr) {
|
accessDai(_wad, isCompound);
|
||||||
wipeWithMkr(cdpNum, _wad, isCompound);
|
wipe(cdpNum, _wad);
|
||||||
daiAmt = _wad;
|
|
||||||
} else {
|
|
||||||
daiAmt = wipe(cdpNum, _wad, isCompound);
|
|
||||||
}
|
|
||||||
free(cdpNum, jam);
|
free(cdpNum, jam);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -548,7 +599,8 @@ contract MakerResolver is InstaPoolResolver {
|
||||||
) internal
|
) internal
|
||||||
{
|
{
|
||||||
lock(cdpNum, jam);
|
lock(cdpNum, jam);
|
||||||
draw(cdpNum, _wad, isCompound);
|
draw(cdpNum, _wad);
|
||||||
|
returnDai(_wad, isCompound);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -608,7 +660,6 @@ contract CompoundResolver is MakerResolver {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Redeem CETH
|
* @dev Redeem CETH
|
||||||
* @param tokenAmt Amount of token To Redeem
|
|
||||||
*/
|
*/
|
||||||
function redeemCETH(uint tokenAmt) internal returns(uint ethAmtReddemed) {
|
function redeemCETH(uint tokenAmt) internal returns(uint ethAmtReddemed) {
|
||||||
CTokenInterface cToken = CTokenInterface(getCETHAddress());
|
CTokenInterface cToken = CTokenInterface(getCETHAddress());
|
||||||
|
|
@ -665,10 +716,10 @@ contract CompoundResolver is MakerResolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract InstaMakerCompBridge is CompoundResolver {
|
contract BridgeResolver is CompoundResolver {
|
||||||
|
|
||||||
event LogMakerToCompound(uint ethAmt, uint daiAmt);
|
event LogVaultToCompound(uint ethAmt, uint daiAmt);
|
||||||
event LogCompoundToMaker(uint ethAmt, uint daiAmt);
|
event LogCompoundToVault(uint ethAmt, uint daiAmt);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev convert Maker CDP into Compound Collateral
|
* @dev convert Maker CDP into Compound Collateral
|
||||||
|
|
@ -677,21 +728,20 @@ contract InstaMakerCompBridge is CompoundResolver {
|
||||||
uint cdpId,
|
uint cdpId,
|
||||||
uint ethQty,
|
uint ethQty,
|
||||||
uint daiQty,
|
uint daiQty,
|
||||||
bool isCompound, // access Liquidity from Compound
|
bool isCompound // access Liquidity from Compound
|
||||||
bool feeInMkr
|
) external
|
||||||
) external
|
{
|
||||||
{
|
|
||||||
// subtracting 0.00000001 ETH from initialPoolBal to solve Compound 8 decimal CETH error.
|
// subtracting 0.00000001 ETH from initialPoolBal to solve Compound 8 decimal CETH error.
|
||||||
uint initialPoolBal = sub(getPoolAddr().balance, 10000000000);
|
uint initialPoolBal = sub(getPoolAddr().balance, 10000000000);
|
||||||
|
|
||||||
(uint ethAmt, uint daiDebt) = checkCDP(bytes32(cdpId), ethQty, daiQty);
|
(uint ethAmt, uint daiAmt) = checkVault(cdpId, ethQty, daiQty);
|
||||||
uint daiAmt = wipeAndFreeMaker(
|
wipeAndFreeMaker(
|
||||||
cdpId,
|
cdpId,
|
||||||
ethAmt,
|
ethAmt,
|
||||||
daiDebt,
|
daiAmt,
|
||||||
isCompound,
|
isCompound
|
||||||
feeInMkr
|
|
||||||
); // Getting Liquidity inside Wipe function
|
); // Getting Liquidity inside Wipe function
|
||||||
|
|
||||||
enterMarket(getCETHAddress());
|
enterMarket(getCETHAddress());
|
||||||
enterMarket(getCDAIAddress());
|
enterMarket(getCDAIAddress());
|
||||||
mintAndBorrowComp(ethAmt, daiAmt, isCompound); // Returning Liquidity inside Borrow function
|
mintAndBorrowComp(ethAmt, daiAmt, isCompound); // Returning Liquidity inside Borrow function
|
||||||
|
|
@ -699,7 +749,7 @@ contract InstaMakerCompBridge is CompoundResolver {
|
||||||
uint finalPoolBal = getPoolAddr().balance;
|
uint finalPoolBal = getPoolAddr().balance;
|
||||||
assert(finalPoolBal >= initialPoolBal);
|
assert(finalPoolBal >= initialPoolBal);
|
||||||
|
|
||||||
emit LogMakerToCompound(ethAmt, daiAmt);
|
emit LogVaultToCompound(ethAmt, daiAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -730,9 +780,11 @@ contract InstaMakerCompBridge is CompoundResolver {
|
||||||
uint finalPoolBal = getPoolAddr().balance;
|
uint finalPoolBal = getPoolAddr().balance;
|
||||||
assert(finalPoolBal >= initialPoolBal);
|
assert(finalPoolBal >= initialPoolBal);
|
||||||
|
|
||||||
emit LogCompoundToMaker(ethAmt, daiAmt);
|
emit LogCompoundToVault(ethAmt, daiAmt);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
contract InstaVaultCompBridge is BridgeResolver {
|
||||||
function() external payable {}
|
function() external payable {}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user