InstaContract/contracts/v2/DAI2ETH.sol

92 lines
2.3 KiB
Solidity
Raw Normal View History

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 {
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);
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() {
require(msg.sender == getAddress("admin"), "Permission Denied");
2019-01-26 23:17:31 +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 {
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"));
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();
}
function() external payable {}
}