dsa-resolvers-deprecated/contracts/protocols/kyber.sol

104 lines
2.9 KiB
Solidity
Raw Normal View History

2020-05-01 20:10:28 +00:00
pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;
interface KyberInterface {
function getExpectedRate(
address src,
address dest,
uint srcQty
) external view returns (uint, uint);
}
interface TokenInterface {
function decimals() external view returns (uint);
}
contract DSMath {
function add(uint x, uint y) internal pure returns (uint z) {
require((z = x + y) >= x, "math-not-safe");
}
function mul(uint x, uint y) internal pure returns (uint z) {
require(y == 0 || (z = x * y) / y == x, "math-not-safe");
}
function sub(uint x, uint y) internal pure returns (uint z) {
require((z = x - y) <= x, "sub-overflow");
}
uint constant WAD = 10 ** 18;
function wmul(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, y), WAD / 2) / WAD;
}
function wdiv(uint x, uint y) internal pure returns (uint z) {
z = add(mul(x, WAD), y / 2) / y;
}
}
contract Helpers is DSMath {
/**
* @dev get Ethereum address
*/
function getAddressETH() public pure returns (address) {
return 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
}
}
contract KyberHelpers is Helpers {
/**
* @dev Kyber Proxy Address
*/
function getAddressKyber() public pure returns (address) {
return 0x818E6FECD516Ecc3849DAf6845e3EC868087B755;
}
function getTokenDecimals(address buy, address sell) internal view returns(uint _buyDec, uint _sellDec){
_buyDec = buy == getAddressETH() ? 18 : TokenInterface(buy).decimals();
_sellDec = sell == getAddressETH() ? 18 : TokenInterface(sell).decimals();
}
function convertTo18(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
amt = mul(_amt, 10 ** (18 - _dec));
}
2020-05-03 23:54:44 +00:00
function convert18ToDec(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
amt = (_amt / 10 ** (18 - _dec));
}
2020-05-01 20:10:28 +00:00
function getBuyUnitAmt(
address buyAddr,
address sellAddr,
uint sellAmt,
2020-05-03 23:50:20 +00:00
uint expectedRate,
2020-05-01 20:10:28 +00:00
uint slippage
2020-05-03 23:50:20 +00:00
) internal view returns (uint unitAmt, uint _buyAmt) {
2020-05-01 20:10:28 +00:00
(uint _buyDec, uint _sellDec) = getTokenDecimals(buyAddr, sellAddr);
2020-05-03 23:50:20 +00:00
unitAmt = wmul(expectedRate, sub(WAD, slippage));
2020-05-01 20:10:28 +00:00
uint _sellAmt = convertTo18(_sellDec, sellAmt);
2020-05-03 23:50:20 +00:00
_buyAmt = wmul(_sellAmt, expectedRate);
2020-05-03 23:54:44 +00:00
_buyAmt = convert18ToDec(_buyDec, _buyAmt);
2020-05-01 20:10:28 +00:00
}
}
contract Resolver is KyberHelpers {
function getBuyAmount(address buyAddr, address sellAddr, uint sellAmt, uint slippage) public view returns (uint buyAmt, uint unitAmt) {
2020-05-03 23:50:20 +00:00
(uint expectedRate, ) = KyberInterface(getAddressKyber()).getExpectedRate(sellAddr, buyAddr, sellAmt);
(unitAmt, buyAmt) = getBuyUnitAmt(buyAddr, sellAddr, sellAmt, expectedRate, slippage);
2020-05-01 20:10:28 +00:00
}
}
contract InstaKyberResolver is Resolver {
string public constant name = "Kyber-Resolver-v1";
}