Completed MoatResolver.

This commit is contained in:
Sowmayjain 2018-10-27 02:25:26 +05:30
parent 7e0da54225
commit e6539e089a
4 changed files with 205 additions and 63 deletions

View File

@ -22,14 +22,6 @@ contract Registry {
address public registryAddress; address public registryAddress;
AddressRegistry aRegistry = AddressRegistry(registryAddress); AddressRegistry aRegistry = AddressRegistry(registryAddress);
modifier onlyAdmin() {
require(
msg.sender == getAddress("admin"),
"Permission Denied"
);
_;
}
modifier onlyAllowedResolver(address user) { modifier onlyAllowedResolver(address user) {
require( require(
aRegistry.isApprovedResolver(user), aRegistry.isApprovedResolver(user),

View File

@ -1,17 +1,32 @@
// Global Freeze Variable // Global Freeze Variable
// withdraw store the 0.5% on the contract itself and can be withdrawn by admin addresses // withdraw store the 0.5% on the contract itself and can be withdrawn by admin addresses
// after sometime of inactivity, admin have power to change the ownership of the wealth. What say? // after sometime of inactivity, admin have power to change the ownership of the wealth. What say?
// still didn't factor 18 decimal thing on Kyber
pragma solidity ^0.4.24; pragma solidity ^0.4.24;
interface AddressRegistry { interface AddressRegistry {
function getAddr(string AddrName) external returns(address); function getAddr(string addrName) external returns(address);
}
interface token {
function transfer(address receiver, uint amount) external returns(bool);
} }
interface MoatAsset { interface MoatAsset {
function getBalance(address assetHolder, address tokenAddr) external view returns (uint256 balance); function getBalance(address assetHolder, address tokenAddr) external view returns (uint256 balance);
function transferAssets(address tokenAddress, uint amount, address sendTo, address target) external; function transferAssets(
function updateBalance(address tokenAddress, uint amount, bool credit, address user) external; address tokenAddress,
uint amount,
address sendTo,
address target
) external;
function updateBalance(
address tokenAddress,
uint amount,
bool credit,
address user
) external;
} }
interface MoatKyber { interface MoatKyber {
@ -25,9 +40,18 @@ interface MoatKyber {
) external returns (uint); ) external returns (uint);
} }
interface MoatMaker {
function getLoan(
address borrower,
uint lockETH,
uint loanDAI
) external returns (uint, address);
}
contract Registry { contract Registry {
address public RegistryAddress;
address public registryAddress;
modifier onlyAdmin() { modifier onlyAdmin() {
require( require(
msg.sender == getAddress("admin"), msg.sender == getAddress("admin"),
@ -35,50 +59,71 @@ contract Registry {
); );
_; _;
} }
function getAddress(string AddressName) internal view returns(address) {
AddressRegistry aRegistry = AddressRegistry(RegistryAddress); function getAddress(string name) internal view returns(address addr) {
address realAddress = aRegistry.getAddr(AddressName); AddressRegistry aRegistry = AddressRegistry(registryAddress);
require(realAddress != address(0), "Invalid Address"); addr = aRegistry.getAddr(name);
return realAddress; require(addr != address(0), "Invalid Address");
} }
} }
contract Protocols is Registry { contract Protocols is Registry {
event KyberExecute(
address trader,
address src,
address dest,
uint srcAmt,
uint destAmt,
uint slipRate,
uint fees
);
event MakerLoan(
address borrower,
uint lockETH,
uint loanDAI,
uint feeDeduct
);
address eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee; address eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
uint public fees; uint public fees;
bool public feesBool; bool public feesBool;
function getFees() public view returns(uint, bool) {
return (fees, feesBool);
event KyberExecute(address src, address dest, uint srcAmt, uint destAmt, uint slipRate, uint fees); }
function kyberTrade( function kyberTrade(
uint weiAmt,
address src, address src,
address dest, address dest,
uint srcAmt, uint srcAmt,
uint slipRate uint slipRate
) public payable { ) public payable
{
MoatAsset initMA = MoatAsset(getAddress("asset"));
MoatAsset MAFunctions = MoatAsset(getAddress("asset"));
uint ethVal; uint ethVal;
if (src == eth) {
if (msg.value > 0) { ethVal = srcAmt;
ethVal = msg.value;
getAddress("moatkyber").transfer(msg.value);
} else {
ethVal = weiAmt;
MAFunctions.transferAssets(src, srcAmt, getAddress("moatkyber"), msg.sender);
} }
// get assets from MoatAsset or user individual wallet if (msg.value > 0 && msg.value == srcAmt) {
// send that asset to MoatKyber getAddress("moatkyber").transfer(srcAmt);
} else {
initMA.transferAssets(
src,
srcAmt,
getAddress("moatkyber"),
msg.sender
);
}
// initiate kyber trade // initiate kyber trade
MoatKyber kmoat = MoatKyber(getAddress("moatkyber")); MoatKyber kybermoat = MoatKyber(getAddress("moatkyber"));
uint destAmt = kmoat.executeTrade( uint destAmt = kybermoat.executeTrade(
ethVal, ethVal,
src, src,
dest, dest,
@ -87,22 +132,98 @@ contract Protocols is Registry {
getAddress("admin") getAddress("admin")
); );
MAFunctions.updateBalance(dest, destAmt, true, msg.sender); uint feeCut;
uint modifiedDestAmt = destAmt;
// fees deduction only if the user have ETH balance if (feesBool) {
uint assetBal = MAFunctions.getBalance(msg.sender, eth); feeCut = destAmt/fees;
if (assetBal > 0 && feesBool) { modifiedDestAmt = destAmt - feeCut;
if (src == eth) { // if selling ETH
MAFunctions.transferAssets(eth, ethVal/200, address(this), msg.sender);
emit KyberExecute(src, dest, srcAmt, destAmt, slipRate, ethVal/200);
} else { // if buying ETH
MAFunctions.transferAssets(eth, destAmt/200, address(this), msg.sender);
emit KyberExecute(src, dest, srcAmt, destAmt, slipRate, destAmt/200);
} }
if (dest == eth) {
getAddress("asset").transfer(modifiedDestAmt);
} else { } else {
emit KyberExecute(src, dest, srcAmt, destAmt, slipRate, 0); token tokenFunctions = token(dest);
tokenFunctions.transfer(getAddress("asset"), destAmt);
} }
initMA.updateBalance(
src,
srcAmt,
false,
msg.sender
);
initMA.updateBalance(
dest,
modifiedDestAmt,
true,
msg.sender
);
emit KyberExecute(
msg.sender,
src,
dest,
srcAmt,
destAmt,
slipRate,
feeCut
);
}
function makerBorrow(
uint lockETH,
uint loanDAI
) public payable
{
MoatAsset initMA = MoatAsset(getAddress("asset"));
if (msg.value > 0) {
require(lockETH == msg.value, "Possibility of glitch in the Tx");
getAddress("moatmaker").transfer(msg.value);
} else {
initMA.transferAssets(
eth,
lockETH,
getAddress("moatmaker"),
msg.sender
);
initMA.updateBalance(
eth,
lockETH,
false,
msg.sender
);
}
MoatMaker makermoat = MoatMaker(getAddress("moatmaker"));
uint daiMinted;
address daiAddr;
(daiMinted, daiAddr) = makermoat.getLoan(
msg.sender,
lockETH,
loanDAI
);
uint modifiedLoanDAI;
uint feeDeduct;
if (loanDAI > 0) {
if (feesBool) {
feeDeduct = loanDAI/fees;
modifiedLoanDAI = loanDAI - feeDeduct;
}
token tokenFunctions = token(daiAddr);
tokenFunctions.transfer(getAddress("asset"), modifiedLoanDAI);
initMA.updateBalance(
daiAddr,
modifiedLoanDAI,
true,
msg.sender
);
}
emit MakerLoan(msg.sender, lockETH, loanDAI, feeDeduct);
} }
} }
@ -113,8 +234,25 @@ contract MoatResolver is Protocols {
function () public payable {} function () public payable {}
constructor(address rAddr, uint cut) public { // 200 means 0.5% constructor(address rAddr, uint cut) public { // 200 means 0.5%
RegistryAddress = rAddr; registryAddress = rAddr;
fees = cut; fees = cut;
} }
function collectFees(address tokenAddress, uint amount) public onlyAdmin {
if (tokenAddress == eth) {
msg.sender.transfer(amount);
} else {
token tokenFunctions = token(tokenAddress);
tokenFunctions.transfer(msg.sender, amount);
}
}
function enableFees() public onlyAdmin {
feesBool = true;
}
function disableFees() public onlyAdmin {
feesBool = false;
}
} }

View File

@ -29,6 +29,7 @@ interface Kyber {
contract Registry { contract Registry {
address public registryAddress; address public registryAddress;
modifier onlyResolver() { modifier onlyResolver() {
require( require(
msg.sender == getAddress("resolver"), msg.sender == getAddress("resolver"),
@ -37,6 +38,14 @@ contract Registry {
_; _;
} }
modifier onlyAdmin() {
require(
msg.sender == getAddress("admin"),
"Permission Denied"
);
_;
}
function getAddress(string name) internal view returns(address addr) { function getAddress(string name) internal view returns(address addr) {
AddressRegistry aRegistry = AddressRegistry(registryAddress); AddressRegistry aRegistry = AddressRegistry(registryAddress);
addr = aRegistry.getAddr(name); addr = aRegistry.getAddr(name);
@ -48,7 +57,7 @@ contract Registry {
contract Trade is Registry { contract Trade is Registry {
event Swapped(address src, uint srcAmt, address dest, uint destAmt); address eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
function executeTrade( function executeTrade(
uint weiAmt, uint weiAmt,
@ -64,17 +73,11 @@ contract Trade is Registry {
src, src,
srcAmt, srcAmt,
dest, dest,
getAddress("asset"), getAddress("resolver"),
2**256 - 1, 2**256 - 1,
slipRate, slipRate,
walletId walletId
); );
emit Swapped(
src,
srcAmt,
dest,
destAmt
);
} }
function allowKyber(address[] tokenArr) public { function allowKyber(address[] tokenArr) public {
@ -95,4 +98,13 @@ contract MoatKyber is Trade {
function () public payable {} function () public payable {}
function collectFees(address tokenAddress, uint amount) public onlyAdmin {
if (tokenAddress == eth) {
msg.sender.transfer(amount);
} else {
token tokenFunctions = token(tokenAddress);
tokenFunctions.transfer(msg.sender, amount);
}
}
} }

View File

@ -114,7 +114,7 @@ contract Borrow is BorrowTasks {
if (borrower != msg.sender) { if (borrower != msg.sender) {
require( require(
msg.sender == getAddress("resolver"), msg.sender == getAddress("resolver"),
"Message Sender is not MoatResolver." "Message sender is not MoatResolver."
); );
require( require(
aRegistry.isApprovedResolver(borrower), aRegistry.isApprovedResolver(borrower),
@ -128,25 +128,25 @@ contract Borrow is BorrowTasks {
address borrower, address borrower,
uint lockETH, uint lockETH,
uint loanDAI uint loanDAI
) public securedResolver(borrower) ) public securedResolver(borrower) returns (uint daiMinted, address daiAddr)
{ {
if (borrowerCDPs[borrower] == 0x0000000000000000000000000000000000000000000000000000000000000000) { if (borrowerCDPs[borrower] == 0x0000000000000000000000000000000000000000000000000000000000000000) {
borrowerCDPs[borrower] = openCDP(); borrowerCDPs[borrower] = openCDP();
} }
if (lockETH != 0) { if (lockETH != 0) {
convertToWETH(lockETH); convertToWETH(lockETH);
convertToPETH(lockETH - lockETH/1000); convertToPETH(lockETH - lockETH/1000);
lockPETH(borrower, lockETH - lockETH/1000); lockPETH(borrower, lockETH - lockETH/1000);
// event for locking ETH // event for locking ETH
} }
if (loanDAI != 0) { if (loanDAI != 0) {
loanMaster.draw(borrowerCDPs[borrower], loanDAI); loanMaster.draw(borrowerCDPs[borrower], loanDAI);
token tokenFunctions = token(dai); token tokenFunctions = token(dai);
tokenFunctions.transfer(getAddress("asset"), loanDAI); tokenFunctions.transfer(getAddress("resolver"), loanDAI);
daiMinted = loanDAI;
// event for drawing DAI // event for drawing DAI
} }
return (daiMinted, daiAddr);
} }
} }