Restructured kyber integration code.

This commit is contained in:
Sowmayjain 2018-10-29 16:49:01 +05:30
parent 3ac321bace
commit 7b65b2c384
4 changed files with 103 additions and 42 deletions

View File

@ -7,7 +7,7 @@
> Smart Contracts powering Moat Fund
### Show some :heart:
### Support
[![GitHub stars](https://img.shields.io/github/stars/MoatNetwork/MoatContract.svg?style=social&label=Star)](https://github.com/MoatNetwork/MoatContract) [![GitHub forks](https://img.shields.io/github/forks/MoatNetwork/MoatContract.svg?style=social&label=Fork)](https://github.com/MoatNetwork/MoatContract/fork) [![GitHub watchers](https://img.shields.io/github/watchers/MoatNetwork/MoatContract.svg?style=social&label=Watch)](https://github.com/MoatNetwork/MoatContract) [![GitHub followers](https://img.shields.io/github/followers/ravidsrk.svg?style=social&label=Follow)](https://github.com/MoatNetwork/MoatContract)
## This project uses:

View File

@ -4,7 +4,7 @@ pragma solidity ^0.4.24;
contract AddressRegistry {
event AddressSet(string name, address addr);
mapping(bytes32 => address) public registry;
mapping(bytes32 => address) registry;
constructor() public {
registry[keccak256(abi.encodePacked("admin"))] = msg.sender;

View File

@ -1,7 +1,37 @@
pragma solidity ^0.4.24;
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
// import "openzeppelin-solidity/contracts/math/SafeMath.sol";
// import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(address owner, address spender)
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);
event Transfer(
address indexed from,
address indexed to,
uint256 value
);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
interface AddressRegistry {
@ -18,6 +48,12 @@ interface Kyber {
uint minConversionRate,
address walletId
) external payable returns (uint);
function getExpectedRate(
address src,
address dest,
uint srcQty
) external view returns (uint, uint);
}
@ -32,7 +68,7 @@ contract Registry {
_;
}
function getAddress(string name) internal view returns(address addr) {
function getAddress(string name) internal view returns(address) {
AddressRegistry addrReg = AddressRegistry(addressRegistry);
return addrReg.getAddr(name);
}
@ -42,6 +78,9 @@ contract Registry {
contract Trade is Registry {
// using SafeMath for uint;
// using SafeMath for uint256;
uint public fees;
event KyberTrade(
@ -50,35 +89,45 @@ contract Trade is Registry {
address dest,
uint destAmt,
address beneficiary,
uint fees,
uint slipRate,
uint feecut,
uint minConversionRate,
address affiliate
);
// ropsten network
address public kyberAddr = 0x818E6FECD516Ecc3849DAf6845e3EC868087B755;
address public eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
function executeTrade(
address src,
address dest,
uint srcAmt,
uint slipRate
uint minConversionRate
) public payable returns (uint destAmt)
{
address protocolAdmin = getAddress("admin");
uint sellQty = srcAmt;
uint feecut;
if (fees > 0) {
feecut = srcAmt / fees;
sellQty = srcAmt - feecut;
}
fetchToken(src, srcAmt);
uint feecut = deductFees(src, srcAmt);
// fetch token & deduct fees
IERC20 tokenFunctions = IERC20(src);
if (src == getAddress("eth")) {
require(msg.value == srcAmt, "Invalid Operation");
if (feecut > 0) {protocolAdmin.transfer(feecut);}
} else {
tokenFunctions.transferFrom(msg.sender, address(this), srcAmt);
if (feecut > 0) {tokenFunctions.transfer(protocolAdmin, feecut);}
}
Kyber kyberFunctions = Kyber(kyberAddr);
destAmt = kyberFunctions.trade.value(msg.value)(
Kyber kyberFunctions = Kyber(getAddress("kyber"));
destAmt = kyberFunctions.trade.value(sellQty)(
src,
srcAmt - feecut,
sellQty,
dest,
msg.sender,
2**256 - 1,
slipRate,
getAddress("admin")
minConversionRate,
protocolAdmin
);
emit KyberTrade(
@ -88,32 +137,26 @@ contract Trade is Registry {
destAmt,
msg.sender,
feecut,
slipRate,
getAddress("admin")
minConversionRate,
protocolAdmin
);
}
function fetchToken(address src, uint srcAmt) internal {
if (src != eth) {
IERC20 tokenFunctions = IERC20(src);
tokenFunctions.transferFrom(msg.sender, address(this), srcAmt);
}
function getExpectedPrice(
address src,
address dest,
uint srcAmt
) public view returns (uint, uint) {
Kyber kyberFunctions = Kyber(getAddress("kyber"));
return kyberFunctions.getExpectedRate(
src,
dest,
srcAmt
);
}
function deductFees(address src, uint volume) internal returns(uint brokerage) {
if (fees > 0) {
brokerage = volume / fees;
if (src == eth) {
getAddress("admin").transfer(brokerage);
} else {
IERC20 tokenFunctions = IERC20(src);
tokenFunctions.transfer(getAddress("admin"), brokerage);
}
}
}
function allowKyber(address[] tokenArr) public {
function approveKyber(address[] tokenArr) public {
for (uint i = 0; i < tokenArr.length; i++) {
IERC20 tokenFunctions = IERC20(tokenArr[i]);
tokenFunctions.approve(getAddress("kyber"), 2**256 - 1);
@ -132,7 +175,7 @@ contract MoatKyber is Trade {
function () public payable {}
function collectAsset(address tokenAddress, uint amount) public onlyAdmin {
if (tokenAddress == eth) {
if (tokenAddress == getAddress("eth")) {
msg.sender.transfer(amount);
} else {
IERC20 tokenFunctions = IERC20(tokenAddress);

View File

@ -20,6 +20,10 @@ interface MakerCDP {
function per() external view returns (uint ray);
}
interface PriceInterface {
function peek() public view returns (bytes32, bool);
}
interface WETHFace {
function deposit() external payable;
function withdraw(uint wad) external;
@ -37,7 +41,7 @@ contract Registry {
_;
}
function getAddress(string name) internal view returns(address addr) {
function getAddress(string name) internal view returns(address) {
AddressRegistry addrReg = AddressRegistry(addressRegistry);
return addrReg.getAddr(name);
}
@ -46,13 +50,18 @@ contract Registry {
contract GlobalVar is Registry {
using SafeMath for uint;
using SafeMath for uint256;
// kovan network
address public weth = 0xd0A1E359811322d97991E03f863a0C30C2cF029C;
address public peth = 0xf4d791139cE033Ad35DB2B2201435fAd668B1b64;
address public mkr = 0xAaF64BFCC32d0F15873a02163e7E500671a4ffcD;
address public dai = 0xC4375B7De8af5a38a93548eb8453a498222C4fF2;
address public eth = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee;
address public pricefeed = 0xA944bd4b25C9F186A846fd5668941AA3d3B8425F;
address public cdpAddr = 0xa71937147b55Deb8a530C7229C442Fd3F31b7db2;
MakerCDP loanMaster = MakerCDP(cdpAddr);
@ -161,6 +170,15 @@ contract BorrowTasks is RepayLoan {
cdps[msg.sender] = blankCDP;
}
function getETHRate() public view returns (uint ethprice) {
PriceInterface ethRate = PriceInterface(pricefeed);
(ethprice, ) = uint(ethRate.peek()) / 10**18;
}
function getCDPID(address borrower) public view returns (uint) {
return uint(cdps[borrower]);
}
function approveERC20() public {
IERC20 wethTkn = IERC20(weth);
wethTkn.approve(cdpAddr, 2**256 - 1);