mirror of
https://github.com/Instadapp/smart-contract.git
synced 2024-07-29 22:08:07 +00:00
MCD basic functions done.
This commit is contained in:
parent
ed07214768
commit
b12feeb9bf
|
@ -89,7 +89,6 @@ interface TokenInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
contract DSMath {
|
contract DSMath {
|
||||||
|
|
||||||
function add(uint x, uint y) internal pure returns (uint z) {
|
function add(uint x, uint y) internal pure returns (uint z) {
|
||||||
|
@ -138,57 +137,39 @@ contract DSMath {
|
||||||
contract Helpers is DSMath {
|
contract Helpers is DSMath {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev get MakerDAO CDP engine
|
* @dev get MCD Manager Address
|
||||||
*/
|
*/
|
||||||
function getSaiTubAddress() public pure returns (address sai) {
|
function getmanagerAddress() public pure returns (address managerAddr) { // not in use
|
||||||
sai = 0x448a5065aeBB8E423F0896E6c5D525C040f59af3;
|
managerAddr;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev get CDP bytes by CDP ID
|
|
||||||
*/
|
|
||||||
function getCDPBytes(uint cdpNum) public pure returns (bytes32 cup) {
|
|
||||||
cup = bytes32(cdpNum);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract CDPResolver is Helpers {
|
contract CDPResolver is Helpers {
|
||||||
|
event LogOpen(uint cdpNum, address owner);
|
||||||
|
event LogGive(uint cdpNum, address owner, address nextOwner);
|
||||||
|
event LogLock(uint cdpNum, uint amtETH, address owner);
|
||||||
|
event LogFree(uint cdpNum, uint amtETH, address owner);
|
||||||
|
event LogDraw(uint cdpNum, uint amtDAI, address owner);
|
||||||
|
event LogWipe(uint cdpNum, uint daiAmt, address owner);
|
||||||
|
|
||||||
function open(address manager) public returns (uint cdp) {
|
function open(address manager) public returns (uint cdp) {
|
||||||
bytes32 ilk = 0x4554482d41000000000000000000000000000000000000000000000000000000;
|
bytes32 ilk = 0x4554482d41000000000000000000000000000000000000000000000000000000;
|
||||||
cdp = ManagerLike(manager).open(ilk);
|
cdp = ManagerLike(manager).open(ilk);
|
||||||
|
emit LogOpen(cdp, address(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
function lockETH(
|
function give(address manager, uint cdp,address nextOwner) public {
|
||||||
address manager,
|
ManagerLike(manager).give(cdp, nextOwner);
|
||||||
address ethJoin,
|
emit LogGive(cdp, address(this), nextOwner);
|
||||||
uint cdp
|
}
|
||||||
) public payable
|
|
||||||
{
|
function lockETH(address manager, address ethJoin, uint cdp) public payable {
|
||||||
// Receives ETH amount, converts it to WETH and joins it into the vat
|
|
||||||
GemJoinLike(ethJoin).gem().deposit.value(msg.value)();
|
GemJoinLike(ethJoin).gem().deposit.value(msg.value)();
|
||||||
// Approves adapter to take the WETH amount
|
|
||||||
GemJoinLike(ethJoin).gem().approve(address(ethJoin), msg.value);
|
GemJoinLike(ethJoin).gem().approve(address(ethJoin), msg.value);
|
||||||
// Joins WETH collateral into the vat
|
|
||||||
GemJoinLike(ethJoin).join(address(this), msg.value);
|
GemJoinLike(ethJoin).join(address(this), msg.value);
|
||||||
|
|
||||||
// Locks WETH amount into the CDP
|
// Locks WETH amount into the CDP
|
||||||
VatLike(ManagerLike(manager).vat()).frob(
|
VatLike(ManagerLike(manager).vat()).frob(
|
||||||
ManagerLike(manager).ilks(cdp),
|
ManagerLike(manager).ilks(cdp),
|
||||||
|
@ -198,6 +179,7 @@ contract CDPResolver is Helpers {
|
||||||
int(msg.value),
|
int(msg.value),
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
|
emit LogLock(cdp, msg.value, address(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
function freeETH(
|
function freeETH(
|
||||||
|
@ -207,19 +189,18 @@ contract CDPResolver is Helpers {
|
||||||
uint wad
|
uint wad
|
||||||
) public
|
) public
|
||||||
{
|
{
|
||||||
// Unlocks WETH amount from the CDP
|
|
||||||
ManagerLike(manager).frob(
|
ManagerLike(manager).frob(
|
||||||
cdp,
|
cdp,
|
||||||
address(this),
|
address(this),
|
||||||
-int(wad),
|
-int(wad),
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
// Exits WETH amount to proxy address as a token
|
|
||||||
GemJoinLike(ethJoin).exit(address(this), wad);
|
GemJoinLike(ethJoin).exit(address(this), wad);
|
||||||
|
|
||||||
// Converts WETH to ETH
|
// Converts WETH to ETH
|
||||||
GemJoinLike(ethJoin).gem().withdraw(wad);
|
GemJoinLike(ethJoin).gem().withdraw(wad);
|
||||||
// Sends ETH back to the user's wallet
|
|
||||||
msg.sender.transfer(wad);
|
msg.sender.transfer(wad);
|
||||||
|
emit LogFree(cdp, wad, address(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
function draw(
|
function draw(
|
||||||
|
@ -235,7 +216,7 @@ contract CDPResolver is Helpers {
|
||||||
bytes32 ilk = ManagerLike(manager).ilks(cdp);
|
bytes32 ilk = ManagerLike(manager).ilks(cdp);
|
||||||
// Updates stability fee rate before generating new debt
|
// Updates stability fee rate before generating new debt
|
||||||
JugLike(jug).drip(ilk);
|
JugLike(jug).drip(ilk);
|
||||||
// Generates debt in the CDP
|
|
||||||
int dart;
|
int dart;
|
||||||
// Gets actual rate from the vat
|
// Gets actual rate from the vat
|
||||||
(, uint rate,,,) = VatLike(vat).ilks(ilk);
|
(, uint rate,,,) = VatLike(vat).ilks(ilk);
|
||||||
|
@ -250,8 +231,6 @@ contract CDPResolver is Helpers {
|
||||||
dart = mul(uint(dart), rate) < mul(wad, RAY) ? dart + 1 : dart;
|
dart = mul(uint(dart), rate) < mul(wad, RAY) ? dart + 1 : dart;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ManagerLike(manager).frob(cdp, 0, dart);
|
ManagerLike(manager).frob(cdp, 0, dart);
|
||||||
// Moves the DAI amount (balance in the vat in rad) to proxy's address
|
// Moves the DAI amount (balance in the vat in rad) to proxy's address
|
||||||
ManagerLike(manager).move(cdp, address(this), toRad(wad));
|
ManagerLike(manager).move(cdp, address(this), toRad(wad));
|
||||||
|
@ -259,8 +238,8 @@ contract CDPResolver is Helpers {
|
||||||
if (VatLike(vat).can(address(this), address(daiJoin)) == 0) {
|
if (VatLike(vat).can(address(this), address(daiJoin)) == 0) {
|
||||||
VatLike(vat).hope(daiJoin);
|
VatLike(vat).hope(daiJoin);
|
||||||
}
|
}
|
||||||
// Exits DAI to the user's wallet as a token
|
|
||||||
DaiJoinLike(daiJoin).exit(msg.sender, wad);
|
DaiJoinLike(daiJoin).exit(msg.sender, wad);
|
||||||
|
emit LogDraw(cdp, wad, address(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
function wipe (
|
function wipe (
|
||||||
|
@ -275,58 +254,47 @@ contract CDPResolver is Helpers {
|
||||||
bytes32 ilk = ManagerLike(manager).ilks(cdp);
|
bytes32 ilk = ManagerLike(manager).ilks(cdp);
|
||||||
|
|
||||||
address own = ManagerLike(manager).owns(cdp);
|
address own = ManagerLike(manager).owns(cdp);
|
||||||
|
|
||||||
|
(, uint rate,,,) = VatLike(vat).ilks(ilk);
|
||||||
|
(, uint art) = VatLike(vat).urns(ilk, urn);
|
||||||
|
|
||||||
DaiJoinLike(daiJoin).dai().transferFrom(msg.sender, address(this), wad);
|
DaiJoinLike(daiJoin).dai().transferFrom(msg.sender, address(this), wad);
|
||||||
// Approves adapter to take the DAI amount
|
|
||||||
DaiJoinLike(daiJoin).dai().approve(daiJoin, wad);
|
DaiJoinLike(daiJoin).dai().approve(daiJoin, wad);
|
||||||
|
|
||||||
|
int dart;
|
||||||
|
|
||||||
if (own == address(this) || ManagerLike(manager).cdpCan(own, cdp, address(this)) == 1) {
|
if (own == address(this) || ManagerLike(manager).cdpCan(own, cdp, address(this)) == 1) {
|
||||||
// Joins DAI amount into the vat
|
|
||||||
DaiJoinLike(daiJoin).join(urn, wad);
|
DaiJoinLike(daiJoin).join(urn, wad);
|
||||||
// Paybacks debt to the CDP
|
|
||||||
ManagerLike(manager).frob(cdp, 0, _getWipeDart(
|
// Uses the whole dai balance in the vat to reduce the debt
|
||||||
vat,
|
dart = toInt(VatLike(vat).dai(urn) / rate);
|
||||||
VatLike(vat).dai(urn),
|
// Checks the calculated dart is not higher than urn.art (total debt), otherwise uses its value
|
||||||
urn,
|
dart = uint(dart) <= art ? - dart : - toInt(art);
|
||||||
ilk));
|
|
||||||
|
ManagerLike(manager).frob(cdp, 0, dart);
|
||||||
} else {
|
} else {
|
||||||
// Joins DAI amount into the vat
|
|
||||||
DaiJoinLike(daiJoin).join(address(this), wad);
|
DaiJoinLike(daiJoin).join(address(this), wad);
|
||||||
// Paybacks debt to the CDP
|
|
||||||
|
// Uses the whole dai balance in the vat to reduce the debt
|
||||||
|
dart = toInt(wad * RAY / rate);
|
||||||
|
// Checks the calculated dart is not higher than urn.art (total debt), otherwise uses its value
|
||||||
|
dart = uint(dart) <= art ? - dart : - toInt(art);
|
||||||
|
|
||||||
VatLike(vat).frob(
|
VatLike(vat).frob(
|
||||||
ilk,
|
ilk,
|
||||||
urn,
|
urn,
|
||||||
address(this),
|
address(this),
|
||||||
address(this),
|
address(this),
|
||||||
0,
|
0,
|
||||||
_getWipeDart(
|
dart
|
||||||
vat,
|
|
||||||
wad * RAY,
|
|
||||||
urn,
|
|
||||||
ilk)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
emit LogWipe(cdp, wad, address(this));
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract InstaMaker is CDPResolver {
|
contract InstaMCD is CDPResolver {
|
||||||
|
|
||||||
function() external payable {}
|
function() external payable {}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user