pragma solidity ^0.4.24;
/**
* ERC20 contract interface.
*/
contract ERC20 {
function totalSupply() public view returns (uint);
function decimals() public view returns (uint);
function balanceOf(address tokenOwner) public view returns (uint balance);
function allowance(address tokenOwner, address spender) public view returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
}
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
/**
* @dev Multiplies two numbers, reverts on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b);
return c;
}
/**
* @dev Integer division of two numbers truncating the quotient, reverts on division by zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0); // Solidity only automatically asserts when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Subtracts two numbers, reverts on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a);
uint256 c = a - b;
return c;
}
/**
* @dev Adds two numbers, reverts on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a);
return c;
}
/**
* @dev Divides two numbers and returns the remainder (unsigned integer modulo),
* reverts when dividing by zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0);
return a % b;
}
/**
* @dev Returns ceil(a / b).
*/
function ceil(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a / b;
if(a % b == 0) {
return c;
}
else {
return c + 1;
}
}
}
/**
* @title KyberNetwork
* @dev Interface for KyberNetwork main contract.
*/
contract KyberNetwork {
function getExpectedRate(
ERC20 src,
ERC20 dest,
uint srcQty
)
public
view
returns (uint expectedRate, uint slippageRate);
function trade(
ERC20 src,
uint srcAmount,
ERC20 dest,
address destAddress,
uint maxDestAmount,
uint minConversionRate,
address walletId
)
public
payable
returns(uint);
}
/**
* @title TokenPriceProvider
* @dev Simple contract returning the price in ETH for ERC20 tokens listed on KyberNetworks.
* @author Olivier Van Den Biggelaar - <olivier@argent.xyz>
*/
contract TokenPriceProvider {
using SafeMath for uint256;
// Mock token address for ETH
address constant internal ETH_TOKEN_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
// Address of Kyber's trading contract
address constant internal KYBER_NETWORK_ADDRESS = 0x818E6FECD516Ecc3849DAf6845e3EC868087B755;
mapping(address => uint256) public cachedPrices;
function syncPrice(ERC20 token) public {
uint256 expectedRate;
(expectedRate,) = kyberNetwork().getExpectedRate(token, ERC20(ETH_TOKEN_ADDRESS), 10000);
cachedPrices[token] = expectedRate;
}
//
// Convenience functions
//
function syncPriceForTokenList(ERC20[] tokens) public {
for(uint16 i = 0; i < tokens.length; i++) {
syncPrice(tokens[i]);
}
}
/**
* @dev Converts the value of _amount tokens in ether.
* @param _amount the amount of tokens to convert (in 'token wei' twei)
* @param _token the ERC20 token contract
* @return the ether value (in wei) of _amount tokens with contract _token
*/
function getEtherValue(uint256 _amount, address _token) public view returns (uint256) {
uint256 decimals = ERC20(_token).decimals();
uint256 price = cachedPrices[_token];
return price.mul(_amount).div(10**decimals);
}
//
// Internal
//
function kyberNetwork() internal view returns (KyberNetwork) {
return KyberNetwork(KYBER_NETWORK_ADDRESS);
}
}
{
"compilationTarget": {
"TokenPriceProvider.sol": "TokenPriceProvider"
},
"evmVersion": "byzantium",
"libraries": {},
"optimizer": {
"enabled": true,
"runs": 999
},
"remappings": []
}
[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"cachedPrices","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_amount","type":"uint256"},{"name":"_token","type":"address"}],"name":"getEtherValue","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"tokens","type":"address[]"}],"name":"syncPriceForTokenList","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"token","type":"address"}],"name":"syncPrice","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]