2019-03-27 20:38:13 +00:00
|
|
|
pragma solidity ^0.5.0;
|
|
|
|
|
|
|
|
|
|
|
|
library SafeMath {
|
2019-03-27 22:56:19 +00:00
|
|
|
|
2019-03-27 20:38:13 +00:00
|
|
|
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
|
|
if (a == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint256 c = a * b;
|
|
|
|
require(c / a == b, "Assertion Failed");
|
|
|
|
return c;
|
|
|
|
}
|
2019-03-27 22:56:19 +00:00
|
|
|
|
|
|
|
function div(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
|
|
require(b > 0, "Assertion Failed");
|
|
|
|
uint256 c = a / b;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2019-03-27 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
contract DSMath {
|
|
|
|
|
|
|
|
function add(uint x, uint y) internal pure returns (uint z) {
|
|
|
|
require((z = x + y) >= x, "math-not-safe");
|
|
|
|
}
|
|
|
|
|
|
|
|
function mul(uint x, uint y) internal pure returns (uint z) {
|
|
|
|
require(y == 0 || (z = x * y) / y == x, "math-not-safe");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint constant WAD = 10 ** 18;
|
|
|
|
uint constant RAY = 10 ** 27;
|
|
|
|
|
|
|
|
function rmul(uint x, uint y) internal pure returns (uint z) {
|
|
|
|
z = add(mul(x, y), RAY / 2) / RAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-27 20:38:13 +00:00
|
|
|
contract IERC20 {
|
2019-03-27 22:56:19 +00:00
|
|
|
function balanceOf(address who) external view returns (uint256);
|
|
|
|
function transfer(address to, uint256 value) external returns (bool);
|
|
|
|
function approve(address spender, uint256 value) external returns (bool);
|
|
|
|
function transferFrom(address from, address to, uint256 value) external returns (bool);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
contract TubInterface {
|
|
|
|
function open() public returns (bytes32);
|
|
|
|
function join(uint) public;
|
|
|
|
function exit(uint) public;
|
|
|
|
function lock(bytes32, uint) public;
|
|
|
|
function free(bytes32, uint) public;
|
|
|
|
function draw(bytes32, uint) public;
|
|
|
|
function wipe(bytes32, uint) public;
|
|
|
|
function give(bytes32, address) public;
|
|
|
|
function shut(bytes32) public;
|
|
|
|
function cups(bytes32) public view returns (address, uint, uint, uint);
|
|
|
|
function gem() public view returns (TokenInterface);
|
|
|
|
function gov() public view returns (TokenInterface);
|
|
|
|
function skr() public view returns (TokenInterface);
|
|
|
|
function sai() public view returns (TokenInterface);
|
|
|
|
function ink(bytes32) public view returns (uint);
|
|
|
|
function tab(bytes32) public view returns (uint);
|
|
|
|
function rap(bytes32) public view returns (uint);
|
|
|
|
function per() public view returns (uint);
|
|
|
|
function pep() public view returns (PepInterface);
|
2019-03-27 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
contract TokenInterface {
|
|
|
|
function allowance(address, address) public view returns (uint);
|
|
|
|
function balanceOf(address) public view returns (uint);
|
|
|
|
function approve(address, uint) public;
|
|
|
|
function transfer(address, uint) public returns (bool);
|
|
|
|
function transferFrom(address, address, uint) public returns (bool);
|
|
|
|
function deposit() public payable;
|
|
|
|
function withdraw(uint) public;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract PepInterface {
|
|
|
|
function peek() public returns (bytes32, bool);
|
2019-03-27 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-27 22:56:19 +00:00
|
|
|
contract WETHFace {
|
|
|
|
function deposit() external payable;
|
|
|
|
function withdraw(uint wad) external;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
contract Helpers is DSMath {
|
2019-03-27 20:38:13 +00:00
|
|
|
|
|
|
|
using SafeMath for uint;
|
|
|
|
using SafeMath for uint256;
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
/**
|
|
|
|
* @dev get MakerDAO CDP engine
|
|
|
|
*/
|
|
|
|
function getPriceFeedAddress() public pure returns (address eth) {
|
2019-03-29 11:43:21 +00:00
|
|
|
// eth = 0x729D19f657BD0614b4985Cf1D82531c67569197B; // mainnet
|
|
|
|
eth = 0xA944bd4b25C9F186A846fd5668941AA3d3B8425F; // kovan
|
2019-03-27 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
/**
|
|
|
|
* @dev get ETH price feed
|
|
|
|
*/
|
|
|
|
function getSaiTubAddress() public pure returns (address sai) {
|
2019-03-29 11:43:21 +00:00
|
|
|
// sai = 0x448a5065aeBB8E423F0896E6c5D525C040f59af3; // mainnet
|
|
|
|
sai = 0xa71937147b55Deb8a530C7229C442Fd3F31b7db2; // kovan
|
2019-03-27 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
/**
|
|
|
|
* @dev get uniswap MKR/DAI exchange
|
|
|
|
*/
|
|
|
|
function getUniswapMKRExchange() public pure returns (address ume) {
|
2019-03-28 22:42:09 +00:00
|
|
|
ume = 0x2C4Bd064b998838076fa341A83d007FC2FA50957; // (SAMYAK) - check if it correct
|
2019-03-27 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
/**
|
|
|
|
* @dev get admin address
|
|
|
|
*/
|
|
|
|
function getAddressAdmin() public pure returns (address admin) {
|
|
|
|
admin = 0x7284a8451d9a0e7Dc62B3a71C0593eA2eC5c5638;
|
2019-03-27 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
/**
|
|
|
|
* @dev get onchain ethereum price
|
|
|
|
*/
|
|
|
|
function getRate() public returns (uint) {
|
|
|
|
(bytes32 ethrate, ) = PepInterface(getPriceFeedAddress()).peek();
|
|
|
|
return uint(ethrate);
|
2019-03-27 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 22:41:43 +00:00
|
|
|
/**
|
|
|
|
* @dev get stability fees in DAI
|
|
|
|
* @param wad is the DAI to wipe
|
|
|
|
*/
|
|
|
|
function getStabilityFees(uint cdpNum, uint wad) public view returns (uint saiDebtFee) {
|
|
|
|
bytes32 cup = bytes32(cdpNum);
|
|
|
|
TubInterface tub = TubInterface(getSaiTubAddress());
|
|
|
|
saiDebtFee = rmul(wad, rdiv(tub.rap(cup), tub.tab(cup)));
|
|
|
|
}
|
|
|
|
|
2019-03-29 10:33:15 +00:00
|
|
|
/** (SAMYAK)
|
|
|
|
* @dev get DAI required to buy MKR fees
|
|
|
|
* @param feesMKR is the stability fee needs to paid in MKR
|
|
|
|
*/
|
|
|
|
function getDAIRequired(uint feesMKR) public pure returns (uint reqDAI) {
|
|
|
|
reqDAI = feesMKR * 0; // get price from Uniswap
|
|
|
|
}
|
|
|
|
|
|
|
|
/** (SAMYAK)
|
|
|
|
* @dev swapping given DAI with MKR
|
|
|
|
* @param reqDAI is the DAI to swap with MKR
|
|
|
|
*/
|
|
|
|
function swapMKR(uint reqDAI) public {
|
|
|
|
TokenInterface(address(0)).transfer(msg.sender, reqDAI); // just wrote this code to remove the error - DELETE IT
|
|
|
|
}
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
/**
|
|
|
|
* @dev handling stability fees payment
|
|
|
|
*/
|
|
|
|
function handleGovFee(TubInterface tub, uint saiDebtFee) internal {
|
|
|
|
address _otc = getUniswapMKRExchange();
|
|
|
|
(bytes32 val, bool ok) = tub.pep().peek();
|
|
|
|
if (ok && val != 0) {
|
|
|
|
uint govAmt = wdiv(saiDebtFee, uint(val)); // Fees in MKR
|
2019-03-29 10:33:15 +00:00
|
|
|
uint saiGovAmt = getDAIRequired(govAmt); // get price
|
2019-03-28 22:26:52 +00:00
|
|
|
if (tub.sai().allowance(address(this), _otc) != uint(-1)) {
|
|
|
|
tub.sai().approve(_otc, uint(-1));
|
|
|
|
}
|
|
|
|
tub.sai().transferFrom(msg.sender, address(this), saiGovAmt);
|
2019-03-29 10:33:15 +00:00
|
|
|
swapMKR(saiGovAmt); // swap DAI with MKR
|
2019-03-27 20:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-27 22:56:19 +00:00
|
|
|
|
2019-03-27 20:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
contract CDPResolver is Helpers {
|
2019-03-27 20:38:13 +00:00
|
|
|
|
2019-03-29 11:43:21 +00:00
|
|
|
function open() public returns (uint) {
|
|
|
|
bytes32 cup = TubInterface(getSaiTubAddress()).open();
|
|
|
|
return uint(cup);
|
2019-03-28 22:26:52 +00:00
|
|
|
}
|
2019-03-27 20:38:13 +00:00
|
|
|
|
2019-03-28 22:41:43 +00:00
|
|
|
/**
|
|
|
|
* @dev transfer CDP ownership
|
|
|
|
*/
|
2019-03-28 22:26:52 +00:00
|
|
|
function give(uint cdpNum, address nextOwner) public {
|
|
|
|
TubInterface(getSaiTubAddress()).give(bytes32(cdpNum), nextOwner);
|
2019-03-27 22:56:19 +00:00
|
|
|
}
|
2019-03-27 20:38:13 +00:00
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
function lock(uint cdpNum) public payable {
|
|
|
|
bytes32 cup = bytes32(cdpNum);
|
|
|
|
address tubAddr = getSaiTubAddress();
|
|
|
|
if (msg.value > 0) {
|
|
|
|
TubInterface tub = TubInterface(tubAddr);
|
|
|
|
|
|
|
|
(address lad,,,) = tub.cups(cup);
|
|
|
|
require(lad == address(this), "cup-not-owned");
|
2019-03-27 22:56:19 +00:00
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
tub.gem().deposit.value(msg.value)();
|
2019-03-27 22:56:19 +00:00
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
uint ink = rdiv(msg.value, tub.per());
|
|
|
|
ink = rmul(ink, tub.per()) <= msg.value ? ink : ink - 1;
|
2019-03-27 22:56:19 +00:00
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
if (tub.gem().allowance(address(this), tubAddr) != uint(-1)) {
|
|
|
|
tub.gem().approve(tubAddr, uint(-1));
|
|
|
|
}
|
|
|
|
tub.join(ink);
|
|
|
|
|
|
|
|
if (tub.skr().allowance(address(this), tubAddr) != uint(-1)) {
|
|
|
|
tub.skr().approve(tubAddr, uint(-1));
|
|
|
|
}
|
|
|
|
tub.lock(cup, ink);
|
2019-03-27 20:38:13 +00:00
|
|
|
}
|
2019-03-28 22:26:52 +00:00
|
|
|
}
|
2019-03-27 20:38:13 +00:00
|
|
|
|
2019-03-28 22:41:43 +00:00
|
|
|
function free(uint cdpNum, uint jam) public {
|
|
|
|
bytes32 cup = bytes32(cdpNum);
|
|
|
|
address tubAddr = getSaiTubAddress();
|
|
|
|
if (jam > 0) {
|
|
|
|
TubInterface tub = TubInterface(tubAddr);
|
|
|
|
uint ink = rdiv(jam, tub.per());
|
|
|
|
ink = rmul(ink, tub.per()) <= jam ? ink : ink - 1;
|
|
|
|
tub.free(cup, ink);
|
|
|
|
if (tub.skr().allowance(address(this), tubAddr) != uint(-1)) {
|
|
|
|
tub.skr().approve(tubAddr, uint(-1));
|
|
|
|
}
|
|
|
|
tub.exit(ink);
|
|
|
|
uint freeJam = tub.gem().balanceOf(address(this)); // Withdraw possible previous stuck WETH as well
|
|
|
|
tub.gem().withdraw(freeJam);
|
|
|
|
address(msg.sender).transfer(freeJam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
function draw(uint cdpNum, uint wad) public {
|
|
|
|
bytes32 cup = bytes32(cdpNum);
|
|
|
|
if (wad > 0) {
|
|
|
|
TubInterface tub = TubInterface(getSaiTubAddress());
|
2019-03-28 22:41:43 +00:00
|
|
|
|
|
|
|
(address lad,,,) = tub.cups(cup);
|
|
|
|
require(lad == address(this), "cup-not-owned");
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
tub.draw(cup, wad);
|
|
|
|
tub.sai().transfer(msg.sender, wad);
|
2019-03-27 22:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-27 20:38:13 +00:00
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
function wipe(uint cdpNum, uint wad) public {
|
|
|
|
bytes32 cup = bytes32(cdpNum);
|
|
|
|
address tubAddr = getSaiTubAddress();
|
|
|
|
if (wad > 0) {
|
|
|
|
TubInterface tub = TubInterface(tubAddr);
|
|
|
|
|
|
|
|
tub.sai().transferFrom(msg.sender, address(this), wad);
|
|
|
|
handleGovFee(tub, rmul(wad, rdiv(tub.rap(cup), tub.tab(cup))));
|
|
|
|
|
|
|
|
if (tub.sai().allowance(address(this), tubAddr) != uint(-1)) {
|
|
|
|
tub.sai().approve(tubAddr, uint(-1));
|
|
|
|
}
|
|
|
|
if (tub.gov().allowance(address(this), tubAddr) != uint(-1)) {
|
|
|
|
tub.gov().approve(tubAddr, uint(-1));
|
|
|
|
}
|
|
|
|
tub.wipe(cup, wad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-27 20:38:13 +00:00
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
contract CDPCluster is CDPResolver {
|
|
|
|
|
|
|
|
function wipeAndFree(uint cdpNum, uint jam, uint wad) public payable {
|
|
|
|
wipe(cdpNum, wad);
|
|
|
|
free(cdpNum, jam);
|
|
|
|
}
|
|
|
|
|
2019-03-28 22:41:43 +00:00
|
|
|
/**
|
|
|
|
* @dev close CDP
|
|
|
|
*/
|
2019-03-28 22:26:52 +00:00
|
|
|
function shut(uint cdpNum) public {
|
|
|
|
bytes32 cup = bytes32(cdpNum);
|
|
|
|
TubInterface tub = TubInterface(getSaiTubAddress());
|
|
|
|
wipeAndFree(cdpNum, rmul(tub.ink(cup), tub.per()), tub.tab(cup));
|
|
|
|
tub.shut(cup);
|
|
|
|
}
|
|
|
|
|
2019-03-28 22:41:43 +00:00
|
|
|
/**
|
|
|
|
* @dev open a new CDP and lock ETH
|
|
|
|
*/
|
2019-03-29 11:43:21 +00:00
|
|
|
function openAndLock() public payable returns (uint cdpNum) {
|
|
|
|
cdpNum = open();
|
|
|
|
lock(cdpNum);
|
2019-03-28 22:26:52 +00:00
|
|
|
}
|
2019-03-27 20:38:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-28 22:26:52 +00:00
|
|
|
|
|
|
|
contract InstaMaker is CDPResolver {
|
2019-03-27 20:38:13 +00:00
|
|
|
|
|
|
|
uint public version;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev setting up variables on deployment
|
|
|
|
* 1...2...3 versioning in each subsequent deployments
|
|
|
|
*/
|
|
|
|
constructor(uint _version) public {
|
|
|
|
version = _version;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|