首页 > 解决方案 > 我可以从一个 URL 中获取两个变量吗

问题描述

`pragma solidity ^0.4.22;
import "github.com/provable-things/ethereum-api/provableAPI_0.4.25.sol";

contract ExampleContract is usingProvable {

   string public ETHUSD;
   event LogConstructorInitiated(string nextStep);
   event LogPriceUpdated(string price);
   event LogNewProvableQuery(string description);

   function ExampleContract() payable {
       LogConstructorInitiated("Constructor was initiated. Call 'updatePrice()' to send the Provable Query.");
   }

   function __callback(bytes32 myid, string result) {
       if (msg.sender != provable_cbAddress()) revert();
       ETHUSD = result;
       LogPriceUpdated(result);
   }

   function updatePrice() payable {
       if (provable_getPrice("URL") > this.balance) {
           LogNewProvableQuery("Provable query was NOT sent, please add some ETH to cover for the query fee");
       } else {
           LogNewProvableQuery("Provable query was sent, standing by for the answer..");
            provable_query("URL", "json(https://api.pro.coinbase.com/products/ETH-USD/ticker).price");
       }
   }`

这是代码,我的问题是:

在代码的最后一行,我有一个 provable_query,它包含 URL,URL 数据是 [{"trade_id":103437884,"price":"2674.1","size":"0.12890079","time":"2021- 04-28T11:19:29.475452Z","bid":"2673.78","ask":"2674.03","volume":"360649.21208837"} 所以最后我想将价格获取到区块链中。

“但我的问题是,如果这个 Url 包含 2 个不同的价格,例如 1 个美元价格和其他欧元价格,例如:{“trade_id”:103437884,“price1”:“2674.1”,“price2”:“2222”,size” :"0.12890079","time":"2021-04-28T11:19:29.475452Z","bid":"2673.78","ask":"2674.03","volume":"360649.21208837"}

所以现在我的问题是如何从上述 URL 获取价格 1 和价格 2,如果是,我如何从单个 URL 获取 2 个价格”。

提前致谢

标签: blockchainethereumsoliditysmartcontractsoraclize

解决方案


推荐阅读