繁体   English   中英

如何使用 chainlink oracle 读取 json 文件中的数组

[英]How to read an array in a json file using a chainlink oracle

我遇到了这个问题中提到的相同问题: How to read a JSON file using a chainlink oracle

但是,出于某种原因,我无法修复它。 我正在尝试从 opensea 读取 API( https://api.opensea.io/api/v1/events?asset_contract_address=0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D&event_type=successful&format=json&only_5

这是我的代码:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";

/**
 * THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY.
 * PLEASE DO NOT USE THIS CODE IN PRODUCTION.
 */
contract GetSalePrice is ChainlinkClient {
    using Chainlink for Chainlink.Request;
  
    //bytes32 public salePriceBytes32;
    string  public salePrice;
    
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;
    
    /**
     * Network: Kovan
     * Oracle: 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8 (Chainlink Devrel   
     * Node)
     * Job ID: 7401f318127148a894c00c292e486ffd
     * Fee: 0.1 LINK
     */
    constructor() {
        setPublicChainlinkToken();
        oracle = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8;
        jobId = "7401f318127148a894c00c292e486ffd";
        fee = 0.1 * 10 ** 18; // (Varies by network and job)
    }
    
    /**
     * Create a Chainlink request to retrieve API response, find the target
     * data, then multiply by 1000000000000000000 (to remove decimal places from data).
     */
    function requestSalePrice() public returns (bytes32 requestId) 
    {
        Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
        
        // Set the URL to perform the GET request on
        request.add("get", "https://api.opensea.io/api/v1/events?asset_contract_address=0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D&event_type=successful&format=json&only_opensea=false&token_id=5407");

        request.add("path", "asset_events.0.total_price");
        
        // Sends the request
        return sendChainlinkRequestTo(oracle, request, fee);
    }
    
    /**
     * Receive the response in the form of uint256
     */ 
     function bytes32ToString(bytes32 _bytes32) public pure returns (string memory) {
        uint8 i = 0;
        while(i < 32 && _bytes32[i] != 0) {
            i++;
        }
        bytes memory bytesArray = new bytes(i);
        for (i = 0; i < 32 && _bytes32[i] != 0; i++) {
            bytesArray[i] = _bytes32[i];
        }
        return string(bytesArray);
    }
    
    /**
     * Receive the response in the form of uint256
     */ 
    function fulfill(bytes32 _requestId, bytes32 _salePrice) public recordChainlinkFulfillment(_requestId)
    {
        salePrice = bytes32ToString(_salePrice);
    }

}

我试图访问的关键是:

{
    "asset_events": [
        {
            ......
            "total_price": "42000000000000000000",
            ......
        },
        {
        ...
        }
    ]
}

我在路径中输入asset_events.0.total_price获取数组asset_events (索引0 )中第一项中"total_price"的值的路径。

出于某种原因,无论我尝试什么,我仍然得到 0 作为响应。

可能是什么问题? 我该如何解决?

我刚刚用节点的 v0.10.11 尝试了这个(使用 JSON 格式),我得到了以下响应。 这可能是您没有收到回复的原因

HTTP 响应太大,必须小于 32768 字节

对于像这样的大量 JSON 请求,我们建议创建一个外部适配器来处理请求,然后返回更少量的数据。 即,如果您希望在 API 中返回 5 个不同的东西,则适配器可以采用一个参数,并且根据参数它可以返回五个不同的东西之一

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM