简体   繁体   中英

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

I ran into the same issue that is mentioned in this question: How to read a JSON file using a chainlink oracle

but, for some reason I cannot get it fixed. I am trying to read from the opensea API ( https://api.opensea.io/api/v1/events?asset_contract_address=0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D&event_type=successful&format=json&only_opensea=false&token_id=5407 )

this is my code:

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

}

the key I am trying to access is:

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

The path I entered asset_events.0.total_price in the path to get the value of "total_price" in the first item in the array asset_events (index 0 ).

For some reason I am still getting 0 as the response no matter what I tried.

What can be the issue? and how can I fix it?

I just tried this with v0.10.11 of the node (using the JSON format), and I got the following response. This is likely why you aren't getting a response

HTTP response too large, must be less than 32768 bytes

For large amounts of JSON like this request, we suggest creating an external adapter that handles the request and then returns smaller amounts of data. ie if there's 5 different things you want returned in that API, the adapter could take a parameter, and depending on the parameter it could return one of five different things

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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