如何使用oracle在智能合约中获取不同的硬币价格



我正试图在智能合约中使用可证明的预言机获取不同加密货币的价格,但我在同时获取不同硬币奖品时出错,如果您有任何正在做相同事情的智能合约示例,请分享。

如何使用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;
}


}'

您可以使用Chainlink价格源。

  1. 根据网络和价格对从目录中选择价格馈送合同地址
  2. 将地址弹出到构造函数中(请参阅下面的代码(
  3. 部署智能合约,点击查看功能即可获取价格

您可以在kovan链上的混音上尝试此演示,以获得Eth的最新价格。这将为您提供ETH价格的去中心化价值,或任何有效的价格对。

/** 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;
}
}

注意,我是Chainlink DevRel

最新更新