From 7e0da542252e1417efed6649c9e958716cccfeca Mon Sep 17 00:00:00 2001 From: Sowmayjain Date: Fri, 26 Oct 2018 21:46:23 +0530 Subject: [PATCH] MoatResolver kyber integration, added fees logics & fixed minor issues. --- contracts/MoatAddress.sol | 2 + contracts/MoatAsset.sol | 15 +--- contracts/MoatResolver.sol | 120 +++++++++++++++++++++++++++++- contracts/protocols/MoatKyber.sol | 3 +- contracts/protocols/MoatMaker.sol | 3 +- 5 files changed, 127 insertions(+), 16 deletions(-) diff --git a/contracts/MoatAddress.sol b/contracts/MoatAddress.sol index a65cf8f..a2189e9 100644 --- a/contracts/MoatAddress.sol +++ b/contracts/MoatAddress.sol @@ -1,3 +1,5 @@ +// addresses name - address, asset, resolver, moatkyber, moatmaker + pragma solidity ^0.4.24; diff --git a/contracts/MoatAsset.sol b/contracts/MoatAsset.sol index 2885be1..3a3e0c0 100644 --- a/contracts/MoatAsset.sol +++ b/contracts/MoatAsset.sol @@ -1,6 +1,7 @@ // withdraw the extra assets other than global balance (in case anyone donated for free) and then no need for seperate brokerage calculation // IMPORTANT CHECK - decimals() - how the balance of tokens with less than 18 decimals are stored. Factor it. // update the balance along with "transferAssets" functions and also check the for onlyAllowedResolver +// transfer assets to different address (create 2 different mappings) - 48 hour time to transfer all - send email for this pragma solidity ^0.4.24; @@ -47,9 +48,7 @@ contract Registry { contract AssetDB is Registry { - // AssetOwner >> TokenAddress >> Balance (as per respective decimals) mapping(address => mapping(address => uint)) balances; - // mapping(address => uint) globalBalance; address eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee; function getBalance( @@ -63,19 +62,16 @@ contract AssetDB is Registry { function deposit(address tknAddr, uint amount) public payable { if (msg.value > 0) { balances[msg.sender][eth] += msg.value; - // globalBalance[eth] += msg.value; } else { token tokenFunctions = token(tknAddr); tokenFunctions.transferFrom(msg.sender, address(this), amount); balances[msg.sender][tknAddr] += amount; - // globalBalance[tknAddr] += amount; } } function withdraw(address tknAddr, uint amount) public { require(balances[msg.sender][tknAddr] >= amount, "Insufficient Balance"); balances[msg.sender][tknAddr] -= amount; - // globalBalance[tknAddr] -= amount; if (tknAddr == eth) { msg.sender.transfer(amount); } else { @@ -87,20 +83,18 @@ contract AssetDB is Registry { function updateBalance( address tokenAddr, uint amount, - bool add, + bool credit, address user ) public onlyAllowedResolver(user) { - if (add) { + if (credit) { balances[user][tokenAddr] += amount; - // globalBalance[tokenAddr] += amount; } else { balances[user][tokenAddr] -= amount; - // globalBalance[tokenAddr] -= amount; } } - function moveAssets( + function transferAssets( address tokenAddress, uint amount, address sendTo, @@ -114,7 +108,6 @@ contract AssetDB is Registry { tokenFunctions.transfer(sendTo, amount); } balances[user][tokenAddress] -= amount; - // globalBalance[tokenAddress] -= amount; } } diff --git a/contracts/MoatResolver.sol b/contracts/MoatResolver.sol index 08b7d04..7e1f461 100644 --- a/contracts/MoatResolver.sol +++ b/contracts/MoatResolver.sol @@ -1,4 +1,120 @@ // Global Freeze Variable -// no more than 10 ETH allowed as of now // withdraw store the 0.5% on the contract itself and can be withdrawn by admin addresses -// https://bitbucket.org/Sowmay/resolver-dex/src/master/contracts/MoatResolver.sol \ No newline at end of file +// after sometime of inactivity, admin have power to change the ownership of the wealth. What say? + +pragma solidity ^0.4.24; + +interface AddressRegistry { + function getAddr(string AddrName) external returns(address); +} + +interface MoatAsset { + function getBalance(address assetHolder, address tokenAddr) external view returns (uint256 balance); + function transferAssets(address tokenAddress, uint amount, address sendTo, address target) external; + function updateBalance(address tokenAddress, uint amount, bool credit, address user) external; +} + +interface MoatKyber { + function executeTrade( + uint weiAmt, + address src, + address dest, + uint srcAmt, + uint slipRate, + address walletId + ) external returns (uint); +} + + +contract Registry { + address public RegistryAddress; + modifier onlyAdmin() { + require( + msg.sender == getAddress("admin"), + "Permission Denied" + ); + _; + } + function getAddress(string AddressName) internal view returns(address) { + AddressRegistry aRegistry = AddressRegistry(RegistryAddress); + address realAddress = aRegistry.getAddr(AddressName); + require(realAddress != address(0), "Invalid Address"); + return realAddress; + } +} + + +contract Protocols is Registry { + + address eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee; + uint public fees; + bool public feesBool; + + + + event KyberExecute(address src, address dest, uint srcAmt, uint destAmt, uint slipRate, uint fees); + + function kyberTrade( + uint weiAmt, + address src, + address dest, + uint srcAmt, + uint slipRate + ) public payable { + + MoatAsset MAFunctions = MoatAsset(getAddress("asset")); + uint ethVal; + + if (msg.value > 0) { + 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 + // send that asset to MoatKyber + + // initiate kyber trade + MoatKyber kmoat = MoatKyber(getAddress("moatkyber")); + uint destAmt = kmoat.executeTrade( + ethVal, + src, + dest, + srcAmt, + slipRate, + getAddress("admin") + ); + + MAFunctions.updateBalance(dest, destAmt, true, msg.sender); + + // fees deduction only if the user have ETH balance + uint assetBal = MAFunctions.getBalance(msg.sender, eth); + if (assetBal > 0 && feesBool) { + 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); + } + } else { + emit KyberExecute(src, dest, srcAmt, destAmt, slipRate, 0); + } + + } + +} + + +contract MoatResolver is Protocols { + + function () public payable {} + + constructor(address rAddr, uint cut) public { // 200 means 0.5% + RegistryAddress = rAddr; + fees = cut; + } + +} \ No newline at end of file diff --git a/contracts/protocols/MoatKyber.sol b/contracts/protocols/MoatKyber.sol index f2d2b12..03681d1 100644 --- a/contracts/protocols/MoatKyber.sol +++ b/contracts/protocols/MoatKyber.sol @@ -1,4 +1,5 @@ // IMPORTANT CHECK - how decimal works on tokens with less than 18 decimals and accordingly store in our MoatAsset DB +// directly use kyber network address instead of getAddress(kyber) pragma solidity ^0.4.24; @@ -56,7 +57,7 @@ contract Trade is Registry { uint srcAmt, uint slipRate, address walletId - ) public onlyResolver returns (uint destAmt) + ) public onlyResolver returns (uint destAmt) { Kyber kyberFunctions = Kyber(getAddress("kyber")); destAmt = kyberFunctions.trade.value(weiAmt)( diff --git a/contracts/protocols/MoatMaker.sol b/contracts/protocols/MoatMaker.sol index 858478b..8848f05 100644 --- a/contracts/protocols/MoatMaker.sol +++ b/contracts/protocols/MoatMaker.sol @@ -1,7 +1,6 @@ // get back the ownership of CDP // mechanism to transfer an existing CDP (2 txn process) // factor the WETH to PETH conversion rate - https://chat.makerdao.com/direct/Sean -// run an event after eveything which change the DApp info like changing the CDP ownership // implement repay loan function pragma solidity 0.4.24; @@ -125,7 +124,7 @@ contract Borrow is BorrowTasks { _; } - function borrowLoan( + function getLoan( address borrower, uint lockETH, uint loanDAI