0

在下面的代码中,我有 2 个 URL,每个 url 都有一个路径

  These are 2 urls 
    api[0] = "https://www.bitstamp.net/api/v2/ticker/ethusd/";
    api[1] = "https://api.pro.coinbase.com/products/eth-usd/ticker";

  The path for URL 0 is last and the path for URL 1 is price

我希望chainlink节点从这2个网址获取数据(最后和价格)并计算来自2个网址的数据中位数,即中位数= {last+price)/2; 如何在 Chainlink 中为此逻辑编写代码

4

1 回答 1

0

首先,您需要按照 Chainlink 文档进行 2 个 API 调用。https://docs.chain.link/docs/make-a-http-get-request/

就像是:

function makeAPICall() public returns (bytes32 requestId) 
    {
        Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
        request.add("get", "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD");
        int timesAmount = 10**18;
        request.addInt("times", timesAmount);
        return sendChainlinkRequestTo(oracle, request, fee);
    }

然后,在您的完成功能中,您将采用列表的中位数。

function fulfill(bytes32 _requestId, uint256 _volume) public recordChainlinkFulfillment(_requestId)
    {
        answers.push(_volume);
        // loop through list and take median
    }
于 2021-07-16T19:36:11.607 回答