MoatResolver kyber integration, added fees logics & fixed minor issues.

This commit is contained in:
Sowmayjain 2018-10-26 21:46:23 +05:30
parent 7603c59840
commit 7e0da54225
5 changed files with 127 additions and 16 deletions

View File

@ -1,3 +1,5 @@
// addresses name - address, asset, resolver, moatkyber, moatmaker
pragma solidity ^0.4.24; pragma solidity ^0.4.24;

View File

@ -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 // 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. // 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 // 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; pragma solidity ^0.4.24;
@ -47,9 +48,7 @@ contract Registry {
contract AssetDB is Registry { contract AssetDB is Registry {
// AssetOwner >> TokenAddress >> Balance (as per respective decimals)
mapping(address => mapping(address => uint)) balances; mapping(address => mapping(address => uint)) balances;
// mapping(address => uint) globalBalance;
address eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee; address eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
function getBalance( function getBalance(
@ -63,19 +62,16 @@ contract AssetDB is Registry {
function deposit(address tknAddr, uint amount) public payable { function deposit(address tknAddr, uint amount) public payable {
if (msg.value > 0) { if (msg.value > 0) {
balances[msg.sender][eth] += msg.value; balances[msg.sender][eth] += msg.value;
// globalBalance[eth] += msg.value;
} else { } else {
token tokenFunctions = token(tknAddr); token tokenFunctions = token(tknAddr);
tokenFunctions.transferFrom(msg.sender, address(this), amount); tokenFunctions.transferFrom(msg.sender, address(this), amount);
balances[msg.sender][tknAddr] += amount; balances[msg.sender][tknAddr] += amount;
// globalBalance[tknAddr] += amount;
} }
} }
function withdraw(address tknAddr, uint amount) public { function withdraw(address tknAddr, uint amount) public {
require(balances[msg.sender][tknAddr] >= amount, "Insufficient Balance"); require(balances[msg.sender][tknAddr] >= amount, "Insufficient Balance");
balances[msg.sender][tknAddr] -= amount; balances[msg.sender][tknAddr] -= amount;
// globalBalance[tknAddr] -= amount;
if (tknAddr == eth) { if (tknAddr == eth) {
msg.sender.transfer(amount); msg.sender.transfer(amount);
} else { } else {
@ -87,20 +83,18 @@ contract AssetDB is Registry {
function updateBalance( function updateBalance(
address tokenAddr, address tokenAddr,
uint amount, uint amount,
bool add, bool credit,
address user address user
) public onlyAllowedResolver(user) ) public onlyAllowedResolver(user)
{ {
if (add) { if (credit) {
balances[user][tokenAddr] += amount; balances[user][tokenAddr] += amount;
// globalBalance[tokenAddr] += amount;
} else { } else {
balances[user][tokenAddr] -= amount; balances[user][tokenAddr] -= amount;
// globalBalance[tokenAddr] -= amount;
} }
} }
function moveAssets( function transferAssets(
address tokenAddress, address tokenAddress,
uint amount, uint amount,
address sendTo, address sendTo,
@ -114,7 +108,6 @@ contract AssetDB is Registry {
tokenFunctions.transfer(sendTo, amount); tokenFunctions.transfer(sendTo, amount);
} }
balances[user][tokenAddress] -= amount; balances[user][tokenAddress] -= amount;
// globalBalance[tokenAddress] -= amount;
} }
} }

View File

@ -1,4 +1,120 @@
// Global Freeze Variable // 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 // 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 // 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;
}
}

View File

@ -1,4 +1,5 @@
// IMPORTANT CHECK - how decimal works on tokens with less than 18 decimals and accordingly store in our MoatAsset DB // 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; pragma solidity ^0.4.24;

View File

@ -1,7 +1,6 @@
// get back the ownership of CDP // get back the ownership of CDP
// mechanism to transfer an existing CDP (2 txn process) // mechanism to transfer an existing CDP (2 txn process)
// factor the WETH to PETH conversion rate - https://chat.makerdao.com/direct/Sean // 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 // implement repay loan function
pragma solidity 0.4.24; pragma solidity 0.4.24;
@ -125,7 +124,7 @@ contract Borrow is BorrowTasks {
_; _;
} }
function borrowLoan( function getLoan(
address borrower, address borrower,
uint lockETH, uint lockETH,
uint loanDAI uint loanDAI