2019-01-27 17:29:42 +00:00
|
|
|
pragma solidity ^0.5.0;
|
2019-01-26 23:17:31 +00:00
|
|
|
|
|
|
|
interface IERC20 {
|
|
|
|
function balanceOf(address who) external view returns (uint256);
|
|
|
|
function approve(address spender, uint256 value) external returns (bool);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface AddressRegistry {
|
2019-01-27 17:29:42 +00:00
|
|
|
function getAddr(string calldata name) external view returns (address);
|
2019-01-26 23:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Kyber {
|
|
|
|
function trade(
|
|
|
|
address src,
|
|
|
|
uint srcAmount,
|
|
|
|
address dest,
|
|
|
|
address destAddress,
|
|
|
|
uint maxDestAmount,
|
|
|
|
uint minConversionRate,
|
|
|
|
address walletId
|
|
|
|
) external payable returns (uint);
|
|
|
|
|
2019-01-27 17:29:42 +00:00
|
|
|
function getExpectedRate(address src, address dest, uint srcQty) external view returns (uint, uint);
|
2019-01-26 23:17:31 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 17:50:14 +00:00
|
|
|
|
2019-01-26 23:17:31 +00:00
|
|
|
contract Registry {
|
|
|
|
address public addressRegistry;
|
|
|
|
modifier onlyAdmin() {
|
2019-01-27 17:29:42 +00:00
|
|
|
require(msg.sender == getAddress("admin"), "Permission Denied");
|
2019-01-26 23:17:31 +00:00
|
|
|
_;
|
|
|
|
}
|
2019-01-27 17:29:42 +00:00
|
|
|
function getAddress(string memory name) internal view returns (address) {
|
2019-01-26 23:17:31 +00:00
|
|
|
AddressRegistry addrReg = AddressRegistry(addressRegistry);
|
|
|
|
return addrReg.getAddr(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-27 17:50:14 +00:00
|
|
|
|
2019-01-26 23:17:31 +00:00
|
|
|
contract Trade is Registry {
|
2019-01-27 17:29:42 +00:00
|
|
|
event KyberTrade(address src, uint srcAmt, address dest, uint destAmt, address beneficiary, uint minConversionRate);
|
2019-01-26 23:17:31 +00:00
|
|
|
|
|
|
|
function approveDAIKyber() public {
|
|
|
|
IERC20 tokenFunctions = IERC20(getAddress("dai"));
|
2019-01-27 17:29:42 +00:00
|
|
|
tokenFunctions.approve(getAddress("kyber"), 2 ** 255);
|
2019-01-26 23:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function expectedETH(uint srcDAI) public view returns (uint, uint) {
|
|
|
|
Kyber kyberFunctions = Kyber(getAddress("kyber"));
|
|
|
|
return kyberFunctions.getExpectedRate(getAddress("dai"), getAddress("eth"), srcDAI);
|
|
|
|
}
|
|
|
|
|
2019-01-26 23:39:02 +00:00
|
|
|
function dai2eth(uint srcDAI) public returns (uint destAmt) {
|
2019-01-26 23:17:31 +00:00
|
|
|
address src = getAddress("dai");
|
|
|
|
address dest = getAddress("eth");
|
|
|
|
uint minConversionRate;
|
|
|
|
(, minConversionRate) = expectedETH(srcDAI);
|
|
|
|
|
|
|
|
// Interacting with Kyber Proxy Contract
|
|
|
|
Kyber kyberFunctions = Kyber(getAddress("kyber"));
|
|
|
|
|
2019-01-27 17:50:14 +00:00
|
|
|
destAmt = kyberFunctions.trade.value(0)(
|
|
|
|
src,
|
|
|
|
srcDAI,
|
|
|
|
dest,
|
|
|
|
msg.sender,
|
|
|
|
2 ** 255,
|
|
|
|
minConversionRate,
|
|
|
|
getAddress("admin")
|
|
|
|
);
|
|
|
|
|
|
|
|
emit KyberTrade(
|
|
|
|
src,
|
|
|
|
srcDAI,
|
|
|
|
dest,
|
|
|
|
destAmt,
|
|
|
|
msg.sender,
|
|
|
|
minConversionRate
|
|
|
|
);
|
2019-01-26 23:17:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-27 17:50:14 +00:00
|
|
|
|
2019-01-26 23:17:31 +00:00
|
|
|
contract DAI2ETH is Trade {
|
|
|
|
constructor(address rAddr) public {
|
|
|
|
addressRegistry = rAddr;
|
|
|
|
approveDAIKyber();
|
|
|
|
}
|
|
|
|
|
2019-01-27 17:29:42 +00:00
|
|
|
function() external payable {}
|
|
|
|
}
|