mirror of
https://github.com/Instadapp/smart-contract.git
synced 2024-07-29 22:08:07 +00:00
added new maker mcd 0.2.16 basic actions
This commit is contained in:
parent
5b2142fe38
commit
4e91eded16
|
@ -1,125 +1,124 @@
|
|||
pragma solidity ^0.5.8;
|
||||
pragma solidity 0.5.12;
|
||||
|
||||
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;
|
||||
contract GemLike {
|
||||
function approve(address, uint) public;
|
||||
function transfer(address, uint) public;
|
||||
function transferFrom(address, address, uint) public;
|
||||
function deposit() public payable;
|
||||
function withdraw(uint) public;
|
||||
}
|
||||
|
||||
interface JugLike {
|
||||
function drip(bytes32) external;
|
||||
contract ManagerLike {
|
||||
function cdpCan(address, uint, address) public view returns (uint);
|
||||
function ilks(uint) public view returns (bytes32);
|
||||
function owns(uint) public view returns (address);
|
||||
function urns(uint) public view returns (address);
|
||||
function vat() public view returns (address);
|
||||
function open(bytes32, address) public returns (uint);
|
||||
function give(uint, address) public;
|
||||
function cdpAllow(uint, address, uint) public;
|
||||
function urnAllow(address, uint) public;
|
||||
function frob(uint, int, int) public;
|
||||
function flux(uint, address, uint) public;
|
||||
function move(uint, address, uint) public;
|
||||
function exit(address, uint, address, uint) public;
|
||||
function quit(uint, address) public;
|
||||
function enter(address, uint) public;
|
||||
function shift(uint, uint) public;
|
||||
}
|
||||
|
||||
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;
|
||||
contract VatLike {
|
||||
function can(address, address) public view returns (uint);
|
||||
function ilks(bytes32) public view returns (uint, uint, uint, uint, uint);
|
||||
function dai(address) public view returns (uint);
|
||||
function urns(bytes32, address) public view returns (uint, uint);
|
||||
function frob(bytes32, address, address, address, int, int) public;
|
||||
function hope(address) public;
|
||||
function move(address, address, uint) public;
|
||||
}
|
||||
|
||||
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;
|
||||
contract GemJoinLike {
|
||||
function dec() public returns (uint);
|
||||
function gem() public returns (GemLike);
|
||||
function join(address, uint) public payable;
|
||||
function exit(address, uint) public;
|
||||
}
|
||||
|
||||
interface GemJoinLike {
|
||||
function dec() external returns (uint);
|
||||
function gem() external returns (GemLike);
|
||||
function join(address, uint) external payable;
|
||||
function exit(address, uint) external;
|
||||
contract GNTJoinLike {
|
||||
function bags(address) public view returns (address);
|
||||
function make(address) public returns (address);
|
||||
}
|
||||
|
||||
interface GNTJoinLike {
|
||||
function bags(address) external view returns (address);
|
||||
function make(address) external returns (address);
|
||||
contract DaiJoinLike {
|
||||
function vat() public returns (VatLike);
|
||||
function dai() public returns (GemLike);
|
||||
function join(address, uint) public payable;
|
||||
function exit(address, uint) public;
|
||||
}
|
||||
|
||||
interface DaiJoinLike {
|
||||
function vat() external returns (VatLike);
|
||||
function dai() external returns (GemLike);
|
||||
function join(address, uint) external payable;
|
||||
function exit(address, uint) external;
|
||||
contract HopeLike {
|
||||
function hope(address) public;
|
||||
function nope(address) public;
|
||||
}
|
||||
|
||||
interface TokenInterface {
|
||||
function allowance(address, address) external view returns (uint);
|
||||
function balanceOf(address) external view returns (uint);
|
||||
function approve(address, uint) external;
|
||||
function transfer(address, uint) external returns (bool);
|
||||
function transferFrom(address, address, uint) external returns (bool);
|
||||
function deposit() external payable;
|
||||
function withdraw(uint) external;
|
||||
contract EndLike {
|
||||
function fix(bytes32) public view returns (uint);
|
||||
function cash(bytes32, uint) public;
|
||||
function free(bytes32) public;
|
||||
function pack(uint) public;
|
||||
function skim(bytes32, address) public;
|
||||
}
|
||||
|
||||
contract JugLike {
|
||||
function drip(bytes32) public returns (uint);
|
||||
}
|
||||
|
||||
contract DSMath {
|
||||
contract PotLike {
|
||||
function pie(address) public view returns (uint);
|
||||
function drip() public returns (uint);
|
||||
function join(uint) public;
|
||||
function exit(uint) public;
|
||||
}
|
||||
|
||||
function add(uint x, uint y) internal pure returns (uint z) {
|
||||
require((z = x + y) >= x, "math-not-safe");
|
||||
}
|
||||
contract ProxyRegistryLike {
|
||||
function proxies(address) public view returns (address);
|
||||
function build(address) public returns (address);
|
||||
}
|
||||
|
||||
function sub(uint x, uint y) internal pure returns (uint z) {
|
||||
z = x - y <= x ? x - y : 0;
|
||||
}
|
||||
contract ProxyLike {
|
||||
function owner() public view returns (address);
|
||||
}
|
||||
|
||||
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
// WARNING: These functions meant to be used as a a library for a DSProxy. Some are unsafe if you call them directly.
|
||||
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
contract Common {
|
||||
uint256 constant RAY = 10 ** 27;
|
||||
|
||||
// Internal functions
|
||||
|
||||
function mul(uint x, uint y) internal pure returns (uint z) {
|
||||
require(y == 0 || (z = x * y) / y == x, "math-not-safe");
|
||||
require(y == 0 || (z = x * y) / y == x, "mul-overflow");
|
||||
}
|
||||
|
||||
uint constant WAD = 10 ** 18;
|
||||
uint constant RAY = 10 ** 27;
|
||||
// Public functions
|
||||
|
||||
function rmul(uint x, uint y) internal pure returns (uint z) {
|
||||
z = add(mul(x, y), RAY / 2) / RAY;
|
||||
function daiJoin_join(address apt, address urn, uint wad) public {
|
||||
// Gets DAI from the user's wallet
|
||||
DaiJoinLike(apt).dai().transferFrom(msg.sender, address(this), wad);
|
||||
// Approves adapter to take the DAI amount
|
||||
DaiJoinLike(apt).dai().approve(apt, wad);
|
||||
// Joins DAI into the vat
|
||||
DaiJoinLike(apt).join(urn, wad);
|
||||
}
|
||||
}
|
||||
|
||||
function wmul(uint x, uint y) internal pure returns (uint z) {
|
||||
z = add(mul(x, y), WAD / 2) / WAD;
|
||||
}
|
||||
contract DssProxyActions is Common {
|
||||
// Internal functions
|
||||
|
||||
function rdiv(uint x, uint y) internal pure returns (uint z) {
|
||||
z = add(mul(x, RAY), y / 2) / y;
|
||||
}
|
||||
|
||||
function wdiv(uint x, uint y) internal pure returns (uint z) {
|
||||
z = add(mul(x, WAD), y / 2) / y;
|
||||
function sub(uint x, uint y) internal pure returns (uint z) {
|
||||
require((z = x - y) <= x, "sub-overflow");
|
||||
}
|
||||
|
||||
function toInt(uint x) internal pure returns (int y) {
|
||||
|
@ -131,55 +130,300 @@ contract DSMath {
|
|||
rad = mul(wad, 10 ** 27);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
contract Helpers is DSMath {
|
||||
|
||||
/**
|
||||
* @dev get MCD Manager Address
|
||||
*/
|
||||
function getmanagerAddress() public pure returns (address managerAddr) { // not in use
|
||||
managerAddr;
|
||||
function convertTo18(address gemJoin, uint256 amt) internal returns (uint256 wad) {
|
||||
// For those collaterals that have less than 18 decimals precision we need to do the conversion before passing to frob function
|
||||
// Adapters will automatically handle the difference of precision
|
||||
wad = mul(
|
||||
amt,
|
||||
10 ** (18 - GemJoinLike(gemJoin).dec())
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
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);
|
||||
|
||||
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) {
|
||||
bytes32 ilk = 0x4554482d41000000000000000000000000000000000000000000000000000000;
|
||||
cdp = ManagerLike(manager).open(ilk);
|
||||
emit LogOpen(cdp, address(this));
|
||||
// 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 give(address manager, uint cdp,address nextOwner) public {
|
||||
ManagerLike(manager).give(cdp, nextOwner);
|
||||
emit LogGive(cdp, address(this), nextOwner);
|
||||
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 lockETH(address manager, address ethJoin, uint cdp) public payable {
|
||||
GemJoinLike(ethJoin).gem().deposit.value(msg.value)();
|
||||
GemJoinLike(ethJoin).gem().approve(address(ethJoin), msg.value);
|
||||
GemJoinLike(ethJoin).join(address(this), msg.value);
|
||||
function _getFreeDink(
|
||||
address vat,
|
||||
uint wadC,
|
||||
address urn,
|
||||
bytes32 ilk
|
||||
) internal view returns (uint dink) {
|
||||
// Gets actual art value of the urn
|
||||
(dink,) = VatLike(vat).urns(ilk, urn);
|
||||
|
||||
// Uses the whole dai balance in the vat to reduce the debt
|
||||
// Checks the calculated dart is not higher than urn.art (total debt), otherwise uses its value
|
||||
dink = wadC <= dink ? wadC : dink;
|
||||
}
|
||||
|
||||
function _getWipeAllWad(
|
||||
address vat,
|
||||
address usr,
|
||||
address urn,
|
||||
bytes32 ilk
|
||||
) internal view returns (uint wad) {
|
||||
// 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);
|
||||
// Gets actual dai amount in the urn
|
||||
uint dai = VatLike(vat).dai(usr);
|
||||
|
||||
uint rad = sub(mul(art, rate), dai);
|
||||
wad = rad / RAY;
|
||||
|
||||
// If the rad precision has some dust, it will need to request for 1 extra wad wei
|
||||
wad = mul(wad, RAY) < rad ? wad + 1 : wad;
|
||||
}
|
||||
|
||||
// Public functions
|
||||
|
||||
function transfer(address gem, address dst, uint wad) public {
|
||||
GemLike(gem).transfer(dst, wad);
|
||||
}
|
||||
|
||||
function ethJoin_join(address apt, address urn) public payable {
|
||||
// Wraps ETH in WETH
|
||||
GemJoinLike(apt).gem().deposit.value(msg.value)();
|
||||
// Approves adapter to take the WETH amount
|
||||
GemJoinLike(apt).gem().approve(address(apt), msg.value);
|
||||
// Joins WETH collateral into the vat
|
||||
GemJoinLike(apt).join(urn, msg.value);
|
||||
}
|
||||
|
||||
function gemJoin_join(address apt, address urn, uint wad, bool transferFrom) public {
|
||||
// Only executes for tokens that have approval/transferFrom implementation
|
||||
if (transferFrom) {
|
||||
// Gets token from the user's wallet
|
||||
GemJoinLike(apt).gem().transferFrom(msg.sender, address(this), wad);
|
||||
// Approves adapter to take the token amount
|
||||
GemJoinLike(apt).gem().approve(apt, wad);
|
||||
}
|
||||
// Joins token collateral into the vat
|
||||
GemJoinLike(apt).join(urn, wad);
|
||||
}
|
||||
|
||||
function hope(
|
||||
address obj,
|
||||
address usr
|
||||
) public {
|
||||
HopeLike(obj).hope(usr);
|
||||
}
|
||||
|
||||
function nope(
|
||||
address obj,
|
||||
address usr
|
||||
) public {
|
||||
HopeLike(obj).nope(usr);
|
||||
}
|
||||
|
||||
function open(
|
||||
address manager,
|
||||
bytes32 ilk,
|
||||
address usr
|
||||
) public returns (uint cdp) {
|
||||
cdp = ManagerLike(manager).open(ilk, usr);
|
||||
}
|
||||
|
||||
function give(
|
||||
address manager,
|
||||
uint cdp,
|
||||
address usr
|
||||
) public {
|
||||
ManagerLike(manager).give(cdp, usr);
|
||||
}
|
||||
|
||||
function giveToProxy(
|
||||
address proxyRegistry,
|
||||
address manager,
|
||||
uint cdp,
|
||||
address dst
|
||||
) public {
|
||||
// Gets actual proxy address
|
||||
address proxy = ProxyRegistryLike(proxyRegistry).proxies(dst);
|
||||
// Checks if the proxy address already existed and dst address is still the owner
|
||||
if (proxy == address(0) || ProxyLike(proxy).owner() != dst) {
|
||||
uint csize;
|
||||
assembly {
|
||||
csize := extcodesize(dst)
|
||||
}
|
||||
// We want to avoid creating a proxy for a contract address that might not be able to handle proxies, then losing the CDP
|
||||
require(csize == 0, "Dst-is-a-contract");
|
||||
// Creates the proxy for the dst address
|
||||
proxy = ProxyRegistryLike(proxyRegistry).build(dst);
|
||||
}
|
||||
// Transfers CDP to the dst proxy
|
||||
give(manager, cdp, proxy);
|
||||
}
|
||||
|
||||
function cdpAllow(
|
||||
address manager,
|
||||
uint cdp,
|
||||
address usr,
|
||||
uint ok
|
||||
) public {
|
||||
ManagerLike(manager).cdpAllow(cdp, usr, ok);
|
||||
}
|
||||
|
||||
function urnAllow(
|
||||
address manager,
|
||||
address usr,
|
||||
uint ok
|
||||
) public {
|
||||
ManagerLike(manager).urnAllow(usr, ok);
|
||||
}
|
||||
|
||||
function flux(
|
||||
address manager,
|
||||
uint cdp,
|
||||
address dst,
|
||||
uint wad
|
||||
) public {
|
||||
ManagerLike(manager).flux(cdp, dst, wad);
|
||||
}
|
||||
|
||||
function move(
|
||||
address manager,
|
||||
uint cdp,
|
||||
address dst,
|
||||
uint rad
|
||||
) public {
|
||||
ManagerLike(manager).move(cdp, dst, rad);
|
||||
}
|
||||
|
||||
function frob(
|
||||
address manager,
|
||||
uint cdp,
|
||||
int dink,
|
||||
int dart
|
||||
) public {
|
||||
ManagerLike(manager).frob(cdp, dink, dart);
|
||||
}
|
||||
|
||||
function quit(
|
||||
address manager,
|
||||
uint cdp,
|
||||
address dst
|
||||
) public {
|
||||
ManagerLike(manager).quit(cdp, dst);
|
||||
}
|
||||
|
||||
function enter(
|
||||
address manager,
|
||||
address src,
|
||||
uint cdp
|
||||
) public {
|
||||
ManagerLike(manager).enter(src, cdp);
|
||||
}
|
||||
|
||||
function shift(
|
||||
address manager,
|
||||
uint cdpSrc,
|
||||
uint cdpOrg
|
||||
) public {
|
||||
ManagerLike(manager).shift(cdpSrc, cdpOrg);
|
||||
}
|
||||
|
||||
function makeGemBag(
|
||||
address gemJoin
|
||||
) public returns (address bag) {
|
||||
bag = GNTJoinLike(gemJoin).make(address(this));
|
||||
}
|
||||
|
||||
function lockETH(
|
||||
address manager,
|
||||
address ethJoin,
|
||||
uint cdp
|
||||
) public payable {
|
||||
// Receives ETH amount, converts it to WETH and joins it into the vat
|
||||
ethJoin_join(ethJoin, address(this));
|
||||
// Locks WETH amount into the CDP
|
||||
VatLike(ManagerLike(manager).vat()).frob(
|
||||
ManagerLike(manager).ilks(cdp),
|
||||
ManagerLike(manager).urns(cdp),
|
||||
address(this),
|
||||
address(this),
|
||||
int(msg.value),
|
||||
toInt(msg.value),
|
||||
0
|
||||
);
|
||||
emit LogLock(cdp, msg.value, address(this));
|
||||
}
|
||||
|
||||
function safeLockETH(
|
||||
address manager,
|
||||
address ethJoin,
|
||||
uint cdp,
|
||||
address owner
|
||||
) public payable {
|
||||
require(ManagerLike(manager).owns(cdp) == owner, "owner-missmatch");
|
||||
lockETH(manager, ethJoin, cdp);
|
||||
}
|
||||
|
||||
function lockGem(
|
||||
address manager,
|
||||
address gemJoin,
|
||||
uint cdp,
|
||||
uint wad,
|
||||
bool transferFrom
|
||||
) public {
|
||||
// Takes token amount from user's wallet and joins into the vat
|
||||
gemJoin_join(gemJoin, address(this), wad, transferFrom);
|
||||
// Locks token amount into the CDP
|
||||
VatLike(ManagerLike(manager).vat()).frob(
|
||||
ManagerLike(manager).ilks(cdp),
|
||||
ManagerLike(manager).urns(cdp),
|
||||
address(this),
|
||||
address(this),
|
||||
toInt(convertTo18(gemJoin, wad)),
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
function safeLockGem(
|
||||
address manager,
|
||||
address gemJoin,
|
||||
uint cdp,
|
||||
uint wad,
|
||||
bool transferFrom,
|
||||
address owner
|
||||
) public {
|
||||
require(ManagerLike(manager).owns(cdp) == owner, "owner-missmatch");
|
||||
lockGem(manager, gemJoin, cdp, wad, transferFrom);
|
||||
}
|
||||
|
||||
function freeETH(
|
||||
|
@ -187,20 +431,81 @@ contract CDPResolver is Helpers {
|
|||
address ethJoin,
|
||||
uint cdp,
|
||||
uint wad
|
||||
) public
|
||||
{
|
||||
ManagerLike(manager).frob(
|
||||
cdp,
|
||||
address(this),
|
||||
-int(wad),
|
||||
0
|
||||
) public {
|
||||
// Unlocks WETH amount from the CDP
|
||||
address urn = ManagerLike(manager).urns(cdp);
|
||||
address vat = ManagerLike(manager).vat();
|
||||
bytes32 ilk = ManagerLike(manager).ilks(cdp);
|
||||
uint dink = _getFreeDink(
|
||||
vat,
|
||||
wad,
|
||||
urn,
|
||||
ilk
|
||||
);
|
||||
GemJoinLike(ethJoin).exit(address(this), wad);
|
||||
frob(manager, cdp, -toInt(dink), 0);
|
||||
// Moves the amount from the CDP urn to proxy's address
|
||||
flux(manager, cdp, address(this), dink);
|
||||
// Exits WETH amount to proxy address as a token
|
||||
GemJoinLike(ethJoin).exit(address(this), dink);
|
||||
// Converts WETH to ETH
|
||||
GemJoinLike(ethJoin).gem().withdraw(dink);
|
||||
// Sends ETH back to the user's wallet
|
||||
msg.sender.transfer(dink);
|
||||
}
|
||||
|
||||
function freeGem(
|
||||
address manager,
|
||||
address gemJoin,
|
||||
uint cdp,
|
||||
uint wad
|
||||
) public {
|
||||
address urn = ManagerLike(manager).urns(cdp);
|
||||
address vat = ManagerLike(manager).vat();
|
||||
bytes32 ilk = ManagerLike(manager).ilks(cdp);
|
||||
uint dink = _getFreeDink(
|
||||
vat,
|
||||
wad,
|
||||
urn,
|
||||
ilk
|
||||
);
|
||||
|
||||
uint wad18 = convertTo18(gemJoin, dink);
|
||||
// Unlocks token amount from the CDP
|
||||
frob(manager, cdp, -toInt(wad18), 0);
|
||||
// Moves the amount from the CDP urn to proxy's address
|
||||
flux(manager, cdp, address(this), wad18);
|
||||
// Exits token amount to the user's wallet as a token
|
||||
GemJoinLike(gemJoin).exit(msg.sender, wad18);
|
||||
}
|
||||
|
||||
function exitETH(
|
||||
address manager,
|
||||
address ethJoin,
|
||||
uint cdp,
|
||||
uint wad
|
||||
) public {
|
||||
// Moves the amount from the CDP urn to proxy's address
|
||||
flux(manager, cdp, address(this), wad);
|
||||
|
||||
// Exits WETH amount to proxy address as a token
|
||||
GemJoinLike(ethJoin).exit(address(this), wad);
|
||||
// Converts WETH to ETH
|
||||
GemJoinLike(ethJoin).gem().withdraw(wad);
|
||||
// Sends ETH back to the user's wallet
|
||||
msg.sender.transfer(wad);
|
||||
emit LogFree(cdp, wad, address(this));
|
||||
}
|
||||
|
||||
function exitGem(
|
||||
address manager,
|
||||
address gemJoin,
|
||||
uint cdp,
|
||||
uint wad
|
||||
) public {
|
||||
// Moves the amount from the CDP urn to proxy's address
|
||||
flux(manager, cdp, address(this), convertTo18(gemJoin, wad));
|
||||
|
||||
// Exits token amount to the user's wallet as a token
|
||||
GemJoinLike(gemJoin).exit(msg.sender, wad);
|
||||
}
|
||||
|
||||
function draw(
|
||||
|
@ -209,93 +514,139 @@ contract CDPResolver is Helpers {
|
|||
address daiJoin,
|
||||
uint cdp,
|
||||
uint wad
|
||||
) public
|
||||
{
|
||||
) public {
|
||||
address urn = ManagerLike(manager).urns(cdp);
|
||||
address vat = ManagerLike(manager).vat();
|
||||
bytes32 ilk = ManagerLike(manager).ilks(cdp);
|
||||
// Updates stability fee rate before generating new debt
|
||||
JugLike(jug).drip(ilk);
|
||||
|
||||
int dart;
|
||||
// Gets actual rate from the vat
|
||||
(, uint rate,,,) = VatLike(vat).ilks(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 = int(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;
|
||||
}
|
||||
|
||||
ManagerLike(manager).frob(cdp, 0, dart);
|
||||
// Generates debt in the CDP
|
||||
frob(manager, cdp, 0, _getDrawDart(vat, jug, urn, ilk, wad));
|
||||
// Moves the DAI amount (balance in the vat in rad) to proxy's address
|
||||
ManagerLike(manager).move(cdp, address(this), toRad(wad));
|
||||
move(manager, 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(msg.sender, wad);
|
||||
emit LogDraw(cdp, wad, address(this));
|
||||
}
|
||||
|
||||
function wipe (
|
||||
function wipe(
|
||||
address manager,
|
||||
address daiJoin,
|
||||
uint cdp,
|
||||
uint wad
|
||||
) public
|
||||
{
|
||||
) public {
|
||||
address vat = ManagerLike(manager).vat();
|
||||
address urn = ManagerLike(manager).urns(cdp);
|
||||
bytes32 ilk = ManagerLike(manager).ilks(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().approve(daiJoin, wad);
|
||||
|
||||
int dart;
|
||||
|
||||
if (own == address(this) || ManagerLike(manager).cdpCan(own, cdp, address(this)) == 1) {
|
||||
DaiJoinLike(daiJoin).join(urn, wad);
|
||||
|
||||
// Uses the whole dai balance in the vat to reduce the debt
|
||||
dart = toInt(VatLike(vat).dai(urn) / rate);
|
||||
// Checks the calculated dart is not higher than urn.art (total debt), otherwise uses its value
|
||||
dart = uint(dart) <= art ? - dart : - toInt(art);
|
||||
|
||||
ManagerLike(manager).frob(cdp, 0, dart);
|
||||
// Joins DAI amount into the vat
|
||||
daiJoin_join(daiJoin, urn, wad);
|
||||
// Paybacks debt to the CDP
|
||||
frob(manager, cdp, 0, _getWipeDart(vat, VatLike(vat).dai(urn), urn, ilk));
|
||||
} else {
|
||||
DaiJoinLike(daiJoin).join(address(this), wad);
|
||||
|
||||
// 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);
|
||||
|
||||
// Joins DAI amount into the vat
|
||||
daiJoin_join(daiJoin, address(this), wad);
|
||||
// Paybacks debt to the CDP
|
||||
VatLike(vat).frob(
|
||||
ilk,
|
||||
urn,
|
||||
address(this),
|
||||
address(this),
|
||||
0,
|
||||
dart
|
||||
_getWipeDart(vat, wad * RAY, urn, ilk)
|
||||
);
|
||||
}
|
||||
emit LogWipe(cdp, wad, address(this));
|
||||
}
|
||||
|
||||
function safeWipe(
|
||||
address manager,
|
||||
address daiJoin,
|
||||
uint cdp,
|
||||
uint wad,
|
||||
address owner
|
||||
) public {
|
||||
require(ManagerLike(manager).owns(cdp) == owner, "owner-missmatch");
|
||||
wipe(manager, daiJoin, cdp, wad);
|
||||
}
|
||||
|
||||
function lockETHAndDraw(
|
||||
address manager,
|
||||
address jug,
|
||||
address ethJoin,
|
||||
address daiJoin,
|
||||
uint cdp,
|
||||
uint wadD
|
||||
) public payable {
|
||||
address urn = ManagerLike(manager).urns(cdp);
|
||||
address vat = ManagerLike(manager).vat();
|
||||
bytes32 ilk = ManagerLike(manager).ilks(cdp);
|
||||
// Receives ETH amount, converts it to WETH and joins it into the vat
|
||||
ethJoin_join(ethJoin, urn);
|
||||
// Locks WETH amount into the CDP and generates debt
|
||||
frob(manager, cdp, toInt(msg.value), _getDrawDart(vat, jug, urn, ilk, wadD));
|
||||
// Moves the DAI amount (balance in the vat in rad) to proxy's address
|
||||
move(manager, cdp, address(this), toRad(wadD));
|
||||
// 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(msg.sender, wadD);
|
||||
}
|
||||
|
||||
function openLockETHAndDraw(
|
||||
address manager,
|
||||
address jug,
|
||||
address ethJoin,
|
||||
address daiJoin,
|
||||
bytes32 ilk,
|
||||
uint wadD
|
||||
) public payable returns (uint cdp) {
|
||||
cdp = open(manager, ilk, address(this));
|
||||
lockETHAndDraw(manager, jug, ethJoin, daiJoin, cdp, wadD);
|
||||
}
|
||||
|
||||
function lockGemAndDraw(
|
||||
address manager,
|
||||
address jug,
|
||||
address gemJoin,
|
||||
address daiJoin,
|
||||
uint cdp,
|
||||
uint wadC,
|
||||
uint wadD,
|
||||
bool transferFrom
|
||||
) public {
|
||||
address urn = ManagerLike(manager).urns(cdp);
|
||||
address vat = ManagerLike(manager).vat();
|
||||
bytes32 ilk = ManagerLike(manager).ilks(cdp);
|
||||
// Takes token amount from user's wallet and joins into the vat
|
||||
gemJoin_join(gemJoin, urn, wadC, transferFrom);
|
||||
// Locks token amount into the CDP and generates debt
|
||||
frob(manager, cdp, toInt(convertTo18(gemJoin, wadC)), _getDrawDart(vat, jug, urn, ilk, wadD));
|
||||
// Moves the DAI amount (balance in the vat in rad) to proxy's address
|
||||
move(manager, cdp, address(this), toRad(wadD));
|
||||
// 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(msg.sender, wadD);
|
||||
}
|
||||
|
||||
function openLockGemAndDraw(
|
||||
address manager,
|
||||
address jug,
|
||||
address gemJoin,
|
||||
address daiJoin,
|
||||
bytes32 ilk,
|
||||
uint wadC,
|
||||
uint wadD,
|
||||
bool transferFrom
|
||||
) public returns (uint cdp) {
|
||||
cdp = open(manager, ilk, address(this));
|
||||
lockGemAndDraw(manager, jug, gemJoin, daiJoin, cdp, wadC, wadD, transferFrom);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
contract InstaMCD is CDPResolver {
|
||||
|
||||
function() external payable {}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user