dsa-resolvers-deprecated/contracts/protocols/mainnet/chainlink.sol

133 lines
4.2 KiB
Solidity
Raw Normal View History

2020-08-14 17:19:25 +00:00
pragma solidity ^0.6.0;
2020-08-16 23:47:25 +00:00
pragma experimental ABIEncoderV2;
2020-08-14 17:19:25 +00:00
interface ChainLinkInterface {
function latestAnswer() external view returns (int256);
2020-08-16 23:47:25 +00:00
function decimals() external view returns (uint256);
2020-08-14 17:19:25 +00:00
}
interface ConnectorsInterface {
function chief(address) external view returns (bool);
}
interface IndexInterface {
function master() external view returns (address);
}
contract Basic {
address public constant connectors = 0xD6A602C01a023B98Ecfb29Df02FBA380d3B21E0c;
address public constant instaIndex = 0x2971AdFa57b20E5a416aE5a708A8655A9c74f723;
uint public version = 1;
modifier isChief {
require(
ConnectorsInterface(connectors).chief(msg.sender) ||
IndexInterface(instaIndex).master() == msg.sender, "not-Chief");
_;
}
event LogAddChainLinkMapping(
2020-08-16 23:47:25 +00:00
string tokenSymbol,
2020-08-14 17:19:25 +00:00
address chainlinkFeed
);
event LogRemoveChainLinkMapping(
2020-08-16 23:47:25 +00:00
string tokenSymbol,
2020-08-14 17:19:25 +00:00
address chainlinkFeed
);
2020-08-16 23:47:25 +00:00
mapping (string => address) public chainLinkMapping;
2020-08-14 17:19:25 +00:00
function _addChainLinkMapping(
2020-08-16 23:47:25 +00:00
string memory token,
2020-08-14 17:19:25 +00:00
address chainlinkFeed
) internal {
2020-08-16 23:47:25 +00:00
require(bytes(token).length > 0, "token-not-vaild");
2020-08-14 17:19:25 +00:00
require(chainlinkFeed != address(0), "chainlinkFeed-not-vaild");
2020-08-16 23:47:25 +00:00
require(chainLinkMapping[token] == address(0), "chainlinkFeed-already-added");
2020-08-14 17:19:25 +00:00
2020-08-16 23:47:25 +00:00
chainLinkMapping[token] = chainlinkFeed;
emit LogAddChainLinkMapping(token, chainlinkFeed);
2020-08-14 17:19:25 +00:00
}
2020-08-16 23:47:25 +00:00
function _removeChainLinkMapping(string memory token) internal {
require(bytes(token).length > 0, "token-not-vaild");
require(chainLinkMapping[token] != address(0), "chainlinkFeed-not-added-yet");
2020-08-14 17:19:25 +00:00
2020-08-16 23:47:25 +00:00
emit LogRemoveChainLinkMapping(token, chainLinkMapping[token]);
delete chainLinkMapping[token];
2020-08-14 17:19:25 +00:00
}
function addChainLinkMapping(
2020-08-16 23:47:25 +00:00
string[] memory tokens,
2020-08-14 17:19:25 +00:00
address[] memory chainlinkFeeds
) public isChief {
2020-08-16 23:47:25 +00:00
require(tokens.length == chainlinkFeeds.length, "lenght-not-same");
for (uint i = 0; i < tokens.length; i++) {
_addChainLinkMapping(tokens[i], chainlinkFeeds[i]);
2020-08-14 17:19:25 +00:00
}
}
2020-08-16 23:47:25 +00:00
function removeChainLinkMapping(string[] memory tokens) public isChief {
for (uint i = 0; i < tokens.length; i++) {
_removeChainLinkMapping(tokens[i]);
2020-08-14 17:19:25 +00:00
}
}
}
contract Resolver is Basic {
2020-08-16 23:47:25 +00:00
struct PriceData {
uint price;
uint decimals;
}
function getPrice(string[] memory tokens)
public
view
returns (
PriceData memory ethPriceInUsd,
PriceData memory btcPriceInUsd,
PriceData[] memory tokensPriceInETH
) {
tokensPriceInETH = new PriceData[](tokens.length);
2020-08-14 17:19:25 +00:00
for (uint i = 0; i < tokens.length; i++) {
2020-08-16 23:47:25 +00:00
ChainLinkInterface feedContract = ChainLinkInterface(chainLinkMapping[tokens[i]]);
if (address(feedContract) != address(0)) {
tokensPriceInETH[i] = PriceData({
price: uint(feedContract.latestAnswer()),
decimals: feedContract.decimals()
});
} else {
tokensPriceInETH[i] = PriceData({
price: 0,
decimals: 0
});
}
2020-08-14 17:19:25 +00:00
}
2020-08-16 23:47:25 +00:00
ChainLinkInterface ethFeed = ChainLinkInterface(chainLinkMapping["ETH"]);
ChainLinkInterface btcFeed = ChainLinkInterface(chainLinkMapping["BTC"]);
ethPriceInUsd = PriceData({
price: uint(ethFeed.latestAnswer()),
decimals: ethFeed.decimals()
});
btcPriceInUsd = PriceData({
price: uint(btcFeed.latestAnswer()),
decimals: btcFeed.decimals()
});
2020-08-14 17:19:25 +00:00
}
function getGasPrice() public view returns (uint gasPrice) {
2020-08-16 23:47:25 +00:00
gasPrice = uint(ChainLinkInterface(chainLinkMapping["gasFast"]).latestAnswer());
2020-08-14 17:19:25 +00:00
}
}
contract InstaChainLinkResolver is Resolver {
2020-08-16 23:47:25 +00:00
constructor (string[] memory tokens, address[] memory chainlinkFeeds) public {
require(tokens.length == chainlinkFeeds.length, "Lenght-not-same");
for (uint i = 0; i < tokens.length; i++) {
_addChainLinkMapping(tokens[i], chainlinkFeeds[i]);
}
2020-08-14 17:19:25 +00:00
}
string public constant name = "ChainLink-Resolver-v1";
}