mirror of
https://github.com/Instadapp/InstaContract.git
synced 2024-07-29 22:47:45 +00:00
Basic structure of v2 smart contract completed.
This commit is contained in:
parent
ca12f19d04
commit
5766f45efd
|
@ -11,6 +11,9 @@
|
||||||
//// DAPP
|
//// DAPP
|
||||||
// check isAddress (and every important variable) before executing any contract function
|
// check isAddress (and every important variable) before executing any contract function
|
||||||
|
|
||||||
|
//// Ravindra
|
||||||
|
// How can we create a global variable for "loanMaster"?
|
||||||
|
|
||||||
pragma solidity 0.4.24;
|
pragma solidity 0.4.24;
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,15 +56,23 @@ interface MakerCDP {
|
||||||
function free(bytes32 cup, uint wad) external;
|
function free(bytes32 cup, uint wad) external;
|
||||||
function draw(bytes32 cup, uint wad) external;
|
function draw(bytes32 cup, uint wad) external;
|
||||||
function wipe(bytes32 cup, uint wad) external;
|
function wipe(bytes32 cup, uint wad) external;
|
||||||
|
function shut(bytes32 cup) external;
|
||||||
function per() external view returns (uint ray);
|
function per() external view returns (uint ray);
|
||||||
function lad(bytes32 cup) external view returns (address);
|
function lad(bytes32 cup) external view returns (address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Resolver {
|
||||||
|
function claimCDP(uint cdpNum) external;
|
||||||
|
function transferCDPInternal(uint cdpNum, address nextOwner) external;
|
||||||
|
function initAct(uint cdpNum) external payable;
|
||||||
|
}
|
||||||
|
|
||||||
interface PriceInterface {
|
interface PriceInterface {
|
||||||
function peek() external view returns (bytes32, bool);
|
function peek() external view returns (bytes32, bool);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface WETHFace {
|
interface WETHFace {
|
||||||
|
function balanceOf(address who) external view returns (uint256);
|
||||||
function deposit() external payable;
|
function deposit() external payable;
|
||||||
function withdraw(uint wad) external;
|
function withdraw(uint wad) external;
|
||||||
}
|
}
|
||||||
|
@ -107,165 +118,198 @@ contract GlobalVar is Registry {
|
||||||
using SafeMath for uint;
|
using SafeMath for uint;
|
||||||
using SafeMath for uint256;
|
using SafeMath for uint256;
|
||||||
|
|
||||||
bytes32 blankCDP = 0x0000000000000000000000000000000000000000000000000000000000000000;
|
|
||||||
address cdpAddr; // SaiTub
|
address cdpAddr; // SaiTub
|
||||||
mapping (uint => address) cdps; // CDP Number >>> Borrower
|
mapping (uint => address) cdps; // CDP Number >>> Borrower
|
||||||
|
mapping (address => bool) resolvers;
|
||||||
bool public freezed;
|
bool public freezed;
|
||||||
|
|
||||||
modifier isCupOwner(uint cdpNum) {
|
modifier isFreezed() {
|
||||||
require(cdps[cdpNum] == msg.sender, "Permission Denied");
|
require(!freezed, "Operation Denied.");
|
||||||
_;
|
_;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
modifier isCupOwner(uint cdpNum) {
|
||||||
|
require(
|
||||||
|
cdps[cdpNum] == msg.sender || cdps[cdpNum] == address(0x0) || cdpNum == 0,
|
||||||
contract IssueLoan is GlobalVar {
|
"Permission Denied");
|
||||||
|
_;
|
||||||
// uint cdpNum
|
}
|
||||||
event LockedETH(address borrower, uint lockETH, uint lockPETH, address lockedBy);
|
|
||||||
event LoanedDAI(address borrower, uint loanDAI, address payTo);
|
|
||||||
event NewCDP(address borrower, bytes32 cdpBytes);
|
|
||||||
|
|
||||||
function pethPEReth(uint ethNum) public view returns (uint rPETH) {
|
function pethPEReth(uint ethNum) public view returns (uint rPETH) {
|
||||||
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
||||||
rPETH = (ethNum.mul(10 ** 27)).div(loanMaster.per());
|
rPETH = (ethNum.mul(10 ** 27)).div(loanMaster.per());
|
||||||
}
|
}
|
||||||
|
|
||||||
function borrow(uint cdpNum, uint daiDraw, address beneficiary) public payable {
|
}
|
||||||
if (msg.value > 0) {lockETH(cdpNum, msg.sender);}
|
|
||||||
if (daiDraw > 0) {drawDAI(cdpNum, daiDraw, beneficiary);}
|
|
||||||
|
contract BorrowLoan is GlobalVar {
|
||||||
|
|
||||||
|
// uint cdpNum
|
||||||
|
event LockedETH(uint cdpNum, address borrower, uint lockETH, uint lockPETH);
|
||||||
|
event LoanedDAI(uint cdpNum, address borrower, uint loanDAI, address payTo);
|
||||||
|
event NewCDP(uint cdpNum, address borrower);
|
||||||
|
|
||||||
|
function borrow(uint cdpUint, uint daiDraw, address beneficiary) public payable isFreezed isCupOwner(cdpUint) {
|
||||||
|
require(!freezed, "Operation Disabled.");
|
||||||
|
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
||||||
|
bytes32 cup = bytes32(cdpUint);
|
||||||
|
|
||||||
|
// creating new CDP
|
||||||
|
if (cdpUint == 0) {
|
||||||
|
cup = loanMaster.open();
|
||||||
|
cdps[uint(cup)] = msg.sender;
|
||||||
|
emit NewCDP(uint(cup), msg.sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
function lockETH(uint cdpNum, address borrower) public payable {
|
// locking ETH
|
||||||
bytes32 cup = bytes32(cdpNum);
|
if (msg.value > 0) {
|
||||||
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
|
||||||
// if (cdps[borrower] == blankCDP) {
|
|
||||||
// require(msg.sender == borrower, "Creating CDP for others is not permitted at the moment.");
|
|
||||||
// cdps[msg.sender] = loanMaster.open();
|
|
||||||
// emit NewCDP(msg.sender, cdps[msg.sender]);
|
|
||||||
// }
|
|
||||||
WETHFace wethTkn = WETHFace(getAddress("weth"));
|
WETHFace wethTkn = WETHFace(getAddress("weth"));
|
||||||
wethTkn.deposit.value(msg.value)(); // ETH to WETH
|
wethTkn.deposit.value(msg.value)(); // ETH to WETH
|
||||||
uint pethToLock = pethPEReth(msg.value);
|
uint pethToLock = pethPEReth(msg.value);
|
||||||
loanMaster.join(pethToLock); // WETH to PETH
|
loanMaster.join(pethToLock); // WETH to PETH
|
||||||
loanMaster.lock(cup, pethToLock); // PETH to CDP
|
loanMaster.lock(cup, pethToLock); // PETH to CDP
|
||||||
emit LockedETH(
|
emit LockedETH(
|
||||||
borrower, msg.value, pethToLock, msg.sender
|
uint(cup), msg.sender, msg.value, pethToLock
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawDAI(uint cdpNum, uint daiDraw, address beneficiary) public {
|
// minting DAI
|
||||||
bytes32 cup = bytes32(cdpNum);
|
if (daiDraw > 0) {
|
||||||
require(!freezed, "Operation Disabled");
|
|
||||||
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
|
||||||
loanMaster.draw(cup, daiDraw);
|
loanMaster.draw(cup, daiDraw);
|
||||||
IERC20 daiTkn = IERC20(getAddress("dai"));
|
IERC20 daiTkn = IERC20(getAddress("dai"));
|
||||||
address payTo = beneficiary;
|
address payTo = beneficiary;
|
||||||
if (beneficiary != address(0)) {
|
if (beneficiary == address(0)) {
|
||||||
payTo = msg.sender;
|
payTo = msg.sender;
|
||||||
}
|
}
|
||||||
daiTkn.transfer(payTo, daiDraw);
|
daiTkn.transfer(payTo, daiDraw);
|
||||||
emit LoanedDAI(msg.sender, daiDraw, payTo);
|
emit LoanedDAI(
|
||||||
|
uint(cup), msg.sender, daiDraw, payTo
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract RepayLoan is IssueLoan {
|
contract RepayLoan is BorrowLoan {
|
||||||
|
|
||||||
event WipedDAI(address borrower, uint daiWipe, uint mkrCharged, address wipedBy);
|
event WipedDAI(uint cdpNum, address borrower, uint daiWipe, uint mkrCharged);
|
||||||
event UnlockedETH(address borrower, uint ethFree);
|
event FreedETH(uint cdpNum, address borrower, uint ethFree);
|
||||||
|
event ShutCDP(uint cdpNum, address borrower, uint daiWipe, uint ethFree);
|
||||||
|
|
||||||
function repay(uint daiWipe, uint ethFree) public payable {
|
function wipeDAI(uint cdpNum, uint daiWipe) public payable {
|
||||||
if (daiWipe > 0) {wipeDAI(daiWipe, msg.sender);}
|
|
||||||
if (ethFree > 0) {unlockETH(ethFree);}
|
|
||||||
}
|
|
||||||
|
|
||||||
function wipeDAI(uint daiWipe, address borrower) public payable {
|
|
||||||
address dai = getAddress("dai");
|
address dai = getAddress("dai");
|
||||||
address mkr = getAddress("mkr");
|
address mkr = getAddress("mkr");
|
||||||
address eth = getAddress("eth");
|
|
||||||
|
|
||||||
IERC20 daiTkn = IERC20(dai);
|
IERC20 daiTkn = IERC20(dai);
|
||||||
IERC20 mkrTkn = IERC20(mkr);
|
IERC20 mkrTkn = IERC20(mkr);
|
||||||
|
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
||||||
|
bytes32 cup = bytes32(cdpNum);
|
||||||
|
|
||||||
uint contractMKR = mkrTkn.balanceOf(address(this)); // contract MKR balance before wiping
|
uint contractMKR = mkrTkn.balanceOf(address(this)); // contract MKR balance before wiping
|
||||||
daiTkn.transferFrom(msg.sender, address(this), daiWipe); // get DAI to pay the debt
|
daiTkn.transferFrom(msg.sender, address(this), daiWipe); // get DAI to pay the debt
|
||||||
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
loanMaster.wipe(cup, daiWipe); // wipe DAI
|
||||||
loanMaster.wipe(cdps[borrower], daiWipe); // wipe DAI
|
|
||||||
uint mkrCharged = contractMKR - mkrTkn.balanceOf(address(this)); // MKR fee = before wiping bal - after wiping bal
|
uint mkrCharged = contractMKR - mkrTkn.balanceOf(address(this)); // MKR fee = before wiping bal - after wiping bal
|
||||||
|
|
||||||
// claiming paid MKR back
|
// Interacting with UniSwap to swap ETH with MKR
|
||||||
if (msg.value > 0) { // Interacting with Kyber to swap ETH with MKR
|
if (msg.value > 0) {
|
||||||
swapETHMKR(
|
// [UniSwap] claiming paid MKR back ETH <> DAI
|
||||||
eth, mkr, mkrCharged, msg.value
|
return;
|
||||||
);
|
|
||||||
} else { // take MKR directly from address
|
} else { // take MKR directly from address
|
||||||
mkrTkn.transferFrom(msg.sender, address(this), mkrCharged); // user paying MKR fees
|
mkrTkn.transferFrom(msg.sender, address(this), mkrCharged); // user paying MKR fees
|
||||||
}
|
}
|
||||||
|
|
||||||
emit WipedDAI(
|
emit WipedDAI(
|
||||||
borrower, daiWipe, mkrCharged, msg.sender
|
cdpNum, msg.sender, daiWipe, mkrCharged
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function unlockETH(uint ethFree) public {
|
function unlockETH(uint cdpNum, uint ethFree) public isFreezed isCupOwner(cdpNum) {
|
||||||
require(!freezed, "Operation Disabled");
|
require(!freezed, "Operation Disabled");
|
||||||
|
bytes32 cup = bytes32(cdpNum);
|
||||||
uint pethToUnlock = pethPEReth(ethFree);
|
uint pethToUnlock = pethPEReth(ethFree);
|
||||||
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
||||||
loanMaster.free(cdps[msg.sender], pethToUnlock); // CDP to PETH
|
loanMaster.free(cup, pethToUnlock); // CDP to PETH
|
||||||
loanMaster.exit(pethToUnlock); // PETH to WETH
|
loanMaster.exit(pethToUnlock); // PETH to WETH
|
||||||
WETHFace wethTkn = WETHFace(getAddress("weth"));
|
WETHFace wethTkn = WETHFace(getAddress("weth"));
|
||||||
wethTkn.withdraw(ethFree); // WETH to ETH
|
wethTkn.withdraw(ethFree); // WETH to ETH
|
||||||
msg.sender.transfer(ethFree);
|
msg.sender.transfer(ethFree);
|
||||||
emit UnlockedETH(msg.sender, ethFree);
|
emit FreedETH(cdpNum, msg.sender, ethFree);
|
||||||
}
|
}
|
||||||
|
|
||||||
function swapETHMKR(
|
function shut(uint cdpNum, uint daiDebt) public payable isFreezed isCupOwner(cdpNum) {
|
||||||
address eth,
|
if (daiDebt > 0) {wipeDAI(cdpNum, daiDebt);}
|
||||||
address mkr,
|
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
||||||
uint mkrCharged,
|
loanMaster.shut(bytes32(cdpNum));
|
||||||
uint ethQty
|
|
||||||
) internal
|
IERC20 pethTkn = IERC20(getAddress("peth"));
|
||||||
{
|
uint pethBal = pethTkn.balanceOf(address(this));
|
||||||
InstaKyber instak = InstaKyber(getAddress("InstaKyber"));
|
loanMaster.exit(pethBal); // PETH to WETH
|
||||||
uint minRate;
|
|
||||||
(, minRate) = instak.getExpectedPrice(eth, mkr, ethQty);
|
WETHFace wethTkn = WETHFace(getAddress("weth"));
|
||||||
uint mkrBought = instak.executeTrade.value(ethQty)(
|
uint wethBal = wethTkn.balanceOf(address(this));
|
||||||
eth, mkr, ethQty, minRate, mkrCharged
|
wethTkn.withdraw(wethBal); // WETH to ETH
|
||||||
|
msg.sender.transfer(wethBal); // ETH to borrower
|
||||||
|
|
||||||
|
cdps[cdpNum] = address(0x0);
|
||||||
|
|
||||||
|
emit ShutCDP(
|
||||||
|
cdpNum, msg.sender, daiDebt, wethBal
|
||||||
);
|
);
|
||||||
require(mkrCharged == mkrBought, "ETH not sufficient to cover the MKR fees.");
|
|
||||||
if (address(this).balance > 0) {
|
|
||||||
msg.sender.transfer(address(this).balance);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract BorrowTasks is RepayLoan {
|
contract MiscTask is RepayLoan {
|
||||||
|
|
||||||
event TranferCDP(bytes32 cdp, address owner, address nextOwner);
|
event TranferInternal(uint cdpNum, address owner, address nextOwner);
|
||||||
event CDPClaimed(bytes32 cdp, address owner);
|
event TranferExternal(uint cdpNum, address owner, address nextOwner);
|
||||||
|
event CDPClaimed(uint cdpNum, address owner);
|
||||||
|
event ResolverOneWay(uint cdpNum, address owner, address resolverAddress);
|
||||||
|
event ResolverTwoWay(uint cdpNum, address owner, address resolverAddress);
|
||||||
|
|
||||||
// nextOwner - transfer CDP owner internally.
|
function transferCDPInternal(uint cdpNum, address nextOwner) public isCupOwner(cdpNum) {
|
||||||
|
require(nextOwner != address(0x0), "Invalid Address.");
|
||||||
|
cdps[cdpNum] = nextOwner;
|
||||||
|
emit TranferInternal(cdpNum, msg.sender, nextOwner);
|
||||||
|
}
|
||||||
|
|
||||||
function transferCDP(address nextOwner) public {
|
function transferCDPExternal(uint cdpNum, address nextOwner) public isCupOwner(cdpNum) {
|
||||||
require(nextOwner != 0, "Invalid Address.");
|
require(freezed, "Operation Denied.");
|
||||||
|
require(nextOwner != address(0x0), "Invalid Address.");
|
||||||
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
||||||
loanMaster.give(cdps[msg.sender], nextOwner);
|
loanMaster.give(bytes32(cdpNum), nextOwner);
|
||||||
cdps[msg.sender] = blankCDP;
|
cdps[cdpNum] = address(0x0);
|
||||||
emit TranferCDP(cdps[msg.sender], msg.sender, nextOwner);
|
emit TranferExternal(cdpNum, msg.sender, nextOwner);
|
||||||
|
}
|
||||||
|
|
||||||
|
// transfering CDP to resolver contract
|
||||||
|
function changeResolverOneWay(uint cdpNum, address resolverAddress) public isCupOwner(cdpNum) {
|
||||||
|
Resolver resolverAct = Resolver(resolverAddress);
|
||||||
|
resolverAct.claimCDP(cdpNum);
|
||||||
|
resolverAct.transferCDPInternal(cdpNum, resolverAddress);
|
||||||
|
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
||||||
|
loanMaster.give(bytes32(cdpNum), resolverAddress);
|
||||||
|
emit ResolverOneWay(cdpNum, msg.sender, resolverAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
// transfering CDP to resolver contract
|
||||||
|
// resolver contract will transfer back CDP
|
||||||
|
function changeResolverTwoWay(uint cdpNum, address resolverAddress) public payable isCupOwner(cdpNum) {
|
||||||
|
Resolver resolverAct = Resolver(resolverAddress);
|
||||||
|
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
||||||
|
loanMaster.give(bytes32(cdpNum), resolverAddress);
|
||||||
|
resolverAct.initAct(cdpNum);
|
||||||
|
emit ResolverTwoWay(cdpNum, msg.sender, resolverAddress);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function claimCDP(uint cdpNum) public {
|
function claimCDP(uint cdpNum) public {
|
||||||
bytes32 cdpBytes = bytes32(cdpNum);
|
bytes32 cup = bytes32(cdpNum);
|
||||||
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
MakerCDP loanMaster = MakerCDP(cdpAddr);
|
||||||
address cdpOwner = loanMaster.lad(cdpBytes);
|
address cdpOwner = loanMaster.lad(cup);
|
||||||
require(cdps[cdpOwner] == blankCDP, "More than 1 CDP is not allowed.");
|
cdps[cdpNum] = cdpOwner;
|
||||||
cdps[cdpOwner] = cdpBytes;
|
emit CDPClaimed(cdpNum, msg.sender);
|
||||||
emit CDPClaimed(cdpBytes, msg.sender);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getETHRate() public view returns (uint) {
|
function getETHRate() public view returns (uint) {
|
||||||
|
@ -275,8 +319,8 @@ contract BorrowTasks is RepayLoan {
|
||||||
return uint(ethrate);
|
return uint(ethrate);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCDP(address borrower) public view returns (uint, bytes32) {
|
function getCDP(uint cdpNum) public view returns (address, bytes32) {
|
||||||
return (uint(cdps[borrower]), cdps[borrower]);
|
return (cdps[cdpNum], bytes32(cdpNum));
|
||||||
}
|
}
|
||||||
|
|
||||||
function approveERC20() public {
|
function approveERC20() public {
|
||||||
|
@ -293,7 +337,7 @@ contract BorrowTasks is RepayLoan {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contract InstaBank is BorrowTasks {
|
contract InstaBank is MiscTask {
|
||||||
|
|
||||||
event MKRCollected(uint amount);
|
event MKRCollected(uint amount);
|
||||||
|
|
||||||
|
@ -309,6 +353,10 @@ contract InstaBank is BorrowTasks {
|
||||||
freezed = stop;
|
freezed = stop;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function manageResolver(address resolverAddress, bool isAllowed) public onlyAdmin {
|
||||||
|
resolvers[resolverAddress] = isAllowed;
|
||||||
|
}
|
||||||
|
|
||||||
// collecting MKR token kept as balance to pay fees
|
// collecting MKR token kept as balance to pay fees
|
||||||
function collectMKR(uint amount) public onlyAdmin {
|
function collectMKR(uint amount) public onlyAdmin {
|
||||||
IERC20 mkrTkn = IERC20(getAddress("mkr"));
|
IERC20 mkrTkn = IERC20(getAddress("mkr"));
|
||||||
|
|
Loading…
Reference in New Issue
Block a user