Reputation: 53
I am trying to fetch the prices of different cryptocurrencies with provable oracle in the smart contract but I am getting error in fetching different coins prize at the same time, if you have any example smart contract which is doing the same thing please share.
How to fetch different coin price in smart contract using oracle
pragma solidity ^0.4.25;
import "github.com/provable-things/ethereum-api/provableAPI_0.4.25.sol";
contract DateOracle is usingProvable {
bytes32 coin_pointer; // variable to differentiate different callbacks
bytes32 temp_ID;
address public owner;
bytes32 public BTC=bytes32("BTC"); //32-bytes equivalent of BTC
bytes32 public ETH=bytes32("ETH");
bytes32 public USDT=bytes32("USD");
bytes32 public USDC=bytes32("USD");
bytes32 public TUSD=bytes32("TUSD");
bytes32 public BUSD=bytes32("USD");
bytes32 public BCH=bytes32("BCH");
bytes32 public XTZ=bytes32("XTZ");
bytes32 public COMP=bytes32("COMP");
uint constant CUSTOM_GASLIMIT = 150000;
mapping (bytes32 => bytes32) oraclizeIndex; // mapping oraclize IDs with coins
mapping(bytes32=>bool) validIds;
// tracking events
event newOraclizeQuery(string description);
event newPriceTicker(uint price);
event LogConstructorInitiated(string nextStep);
event LogPriceUpdated(string price);
modifier onlyOwner {
require(owner == msg.sender);
_;
}
function changeOraclizeGasPrice(uint _newGasPrice) external onlyOwner {
provable_setCustomGasPrice(_newGasPrice);
}
// constructor
constructor()public payable {
provable_setProof(proofType_TLSNotary | proofStorage_IPFS);
emit LogConstructorInitiated("Constructor was initiated. Call 'updatePrice()' to send the Provable Query.");
owner = msg.sender;
provable_setCustomGasPrice(1000000000 wei);
}
//oraclize callback method
function __callback(bytes32 myid, bytes32 result) public {
if (!validIds[myid]) revert();
if (msg.sender != provable_cbAddress()) revert();
coin_pointer = oraclizeIndex[myid];
delete validIds[myid];
ETH = result;
BTC = result;
BCH = result;
TUSD = result;
BUSD = result;
USDT = result;
USDC = result;
COMP = result;
XTZ = result;
updatePrice();
}
// method to place the oraclize queries
function updatePrice() onlyOwner public payable returns(bool) {
if (provable_getPrice("URL") > (owner.balance)) {
emit newOraclizeQuery("Oraclize query was NOT sent, please add some ETH to cover for the query fee");
} else {
emit newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
temp_ID = provable_query(60, "URL", "json(https://api.pro.coinbase.com/products/ETH-USD/ticker).price", CUSTOM_GASLIMIT);
oraclizeIndex[temp_ID] = ETH;
temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/BTC-USD/ticker).price",CUSTOM_GASLIMIT);
oraclizeIndex[temp_ID] = BTC;
temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/USD/ticker).price",CUSTOM_GASLIMIT);
oraclizeIndex[temp_ID] = USDT;
temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/USD/ticker).price",CUSTOM_GASLIMIT);
oraclizeIndex[temp_ID] = USDC;
temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/TUSD-USD/ticker).price",CUSTOM_GASLIMIT);
oraclizeIndex[temp_ID] = TUSD;
temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/USD/ticker).price",CUSTOM_GASLIMIT);
oraclizeIndex[temp_ID] = BUSD;
temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/BCH-USD/ticker).price",CUSTOM_GASLIMIT);
oraclizeIndex[temp_ID] = BCH;
temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/XTZ-USD/ticker).price",CUSTOM_GASLIMIT);
oraclizeIndex[temp_ID] = XTZ;
temp_ID = provable_query(360, "URL", "json(https://api.pro.coinbase.com/products/COMP-USD/ticker).price",CUSTOM_GASLIMIT);
oraclizeIndex[temp_ID] = COMP;
validIds[temp_ID] = true;
}
return true;
}
}'
Upvotes: 3
Views: 2773
Reputation: 6131
You'd use Chainlink Price Feeds.
You can try this demo on remix on the kovan chain getting the Eth latest price. This will get you the decentralized value of the price of ETH, or any valid price pair.
/** This example code is designed to quickly deploy an example contract using Remix.
* If you have never used Remix, try our example walkthrough: https://docs.chain.link/docs/example-walkthrough
* You will need testnet ETH and LINK.
* - Kovan ETH faucet: https://faucet.kovan.network/
* - Kovan LINK faucet: https://kovan.chain.link/
*/
pragma solidity ^0.6.7;
import "https://github.com/smartcontractkit/chainlink/blob/master/evm-contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
contract PriceConsumerV3 {
AggregatorV3Interface internal priceFeed;
/**
* Network: Kovan
* Aggregator: ETH/USD
* Address: 0x9326BFA02ADD2366b30bacB125260Af641031331
*/
constructor() public {
priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331);
}
/**
* Returns the latest price
*/
function getLatestPrice() public view returns (int) {
(
uint80 roundID,
int price,
uint startedAt,
uint timeStamp,
uint80 answeredInRound
) = priceFeed.latestRoundData();
// If the round is not complete yet, timestamp is 0
require(timeStamp > 0, "Round not complete");
return price;
}
}
Note, I am Chainlink DevRel
Upvotes: 3