繁体   English   中英

为什么解析json路径数组时Chainlink作业出错

[英]Why Chainlink job errored when parsejson array of path

我正在尝试使用 chainlink 节点从 api 读取信息。 但是,我遇到了一个我无法解决的错误。

jsonparseError: 无法解析 {"assets":[{"id":47699248,"num_sales":0,"background_color":null,"image_url":"https:// 中的路径 ["assets.0.id"] storage.opensea.io/files/190273b6d8a335c4428d58caed63e8ee.svg","image_preview_url":"https://storage.opensea.io/files/190273b6d8a335c4428d58caed63e8ee.svg","image_thumbnail_url":"https://storage.opensea.io/ files/190273b6d8a335c4428d58caed63e8ee.svg","image_original_url":null,"animation_url":null,"animation_original_url":null,"name":"RoundRobin #0","description":"RoundRobin 是一个完整的链上 Robin。" ,"external_link":null,"asset_contract":{"address":"0x15e49d9b58d91b781c264d1b828de6e5fe3feab9","asset_contract_type":"non-fungible","created_date":"2022-03-09T09:04:44.891296","name": "RoundRobin","nft_version":"3.0","opensea_version":null,"owner":null,"schema_name":"ERC721","symbol":"Robin","total_supply":null,"description": null,"external_link":null,"image_url":null,"default_to_fiat":false,"dev_buyer_fee_basis_points":0,"dev_seller_fee_basis_points":0, "only_proxied_transfers":false,"opensea_buyer_fee_basis_points":0,"opensea_seller_fee_basis_points":250,"buyer_fee_basis_points":0,"seller_fee_basis_points":250,"payout_address":null},"permalink":"https://testnets.opensea. io/assets/0x15e49d9b58d91b781c264d1b828de6e5fe3feab9/0","collection":{"banner_image_url":null,"chat_url":null,"created_date":"2022-03-09T09:04:45.528117","default_to_fiat":false,"description ":null,"dev_buyer_fee_basis_points":"0","dev_seller_fee_basis_points":"0","discord_url":null,"display_data":{"card_display_style":"contain","images":[]},"external_url" :null,"featured":false,"featured_image_url":null,"hidden":false,"safelist_request_status":"not_requested","image_url":null,"is_subject_to_whitelist":false,"large_image_url":null,"medium_username" :null,"name":"RoundRobin - EQt3xOiVtN","only_proxied_transfers":false,"opensea_buyer_fee_basis_points":"0","opensea_seller_fee_basis_points":"250","payout_address":null,"require_email":false,"short_description" :null,"蛞蝓":"ro undrobin-eqt3xoivtn","telegram_url":null,"twitter_username":null,"instagram_username":null,"wiki_url":null,"is_nsfw":false},"decimals":0,"token_metadata":"data:application /json;base64,=","is_nsfw":false,"owner":{"user":{"username":"tomoking"},"profile_img_url":"https://storage.googleapis.com/opensea-静态/opensea-profile/19.png","address":"0x16ea840cfa174fdac738905c4e5db59fd86912a1","config":""},"sell_orders":null,"creator":null,"traits":[],"last_sale": null,"top_bid":null,"listing_date":null,"is_presale":false,"transfer_fee_payment_token":null,"transfer_fee":null,"token_id":"0"}]}:未找到密钥路径

代码在这里

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

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

contract APIConsumer is ChainlinkClient {
    using Chainlink for Chainlink.Request;
  
    uint256 public volume;
    
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;

    constructor() {
        setPublicChainlinkToken();
        oracle = 0xD8269ebfE7fCdfCF6FaB16Bb4A782dC8Ab59b53C;
        jobId = "2e0441fe857e45bca8f0d349bb680afd";
        fee = 0.1 * 10 ** 18; 
    }
    
    function requestVolumeData() public returns (bytes32 requestId) 
    {
        Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
        
        request.add("get", "https://testnets-api.opensea.io/api/v1/assets?owner=0x16ea840cfA174FdAC738905C4E5dB59Fd86912a1&order_direction=desc&offset=0&limit=1");

        request.add("path", "assets");
        
        int timesAmount = 10**18;
        request.addInt("times", timesAmount);

        return sendChainlinkRequestTo(oracle, request, fee);
    }

    function fulfill(bytes32 _requestId, uint256 _volume) public recordChainlinkFulfillment(_requestId)
    {
        volume = _volume;
    }
}

工作代码在这里。

type = "directrequest"
schemaVersion = 1
name = "Get > Uint256"
externalJobID = "2e0441fe-857e-45bc-a8f0-d349bb680afd"
maxTaskDuration = "0s"
contractAddress = "0xD8269ebfE7fCdfCF6FaB16Bb4A782dC8Ab59b53C"
minIncomingConfirmations = 0
observationSource = """
    decode_log   [type="ethabidecodelog"
                  abi="OracleRequest(bytes32 indexed specId, address requester, bytes32 requestId, uint256 payment, address callbackAddr, bytes4 callbackFunctionId, uint256 cancelExpiration, uint256 dataVersion, bytes data)"
                  data="$(jobRun.logData)"
                  topics="$(jobRun.logTopics)"]

    decode_cbor  [type="cborparse" data="$(decode_log.data)"]
    fetch        [type="http" method=GET url="$(decode_cbor.get)"]
    parse        [type="jsonparse" path="$(decode_cbor.path)" data="$(fetch)"]
    multiply     [type="multiply" input="$(parse)" times=100]
    encode_data  [type="ethabiencode" abi="(uint256 value)" data="{ \\"value\\": $(multiply) }"]
    encode_tx    [type="ethabiencode"
                  abi="fulfillOracleRequest(bytes32 requestId, uint256 payment, address callbackAddress, bytes4 callbackFunctionId, uint256 expiration, bytes32 data)"
                  data="{\\"requestId\\": $(decode_log.requestId), \\"payment\\": $(decode_log.payment), \\"callbackAddress\\": $(decode_log.callbackAddr), \\"callbackFunctionId\\": $(decode_log.callbackFunctionId), \\"expiration\\": $(decode_log.cancelExpiration), \\"data\\": $(encode_data)}"
                 ]
    submit_tx    [type="ethtx" to="0xD8269ebfE7fCdfCF6FaB16Bb4A782dC8Ab59b53C" data="$(encode_tx)"]

    decode_log -> decode_cbor -> fetch -> parse -> multiply -> encode_data -> encode_tx -> submit_tx
"""

我学会了使用 A.0.B 而不是 A [0] .B 来处理数组中发生的错误,但这不是我的解决方案。 ......

您提交的节点可能正在运行 chainlink 1.x。 您需要使用逗号作为分隔符来指定 JSON 路径。 在您的情况下,请尝试替换request.add("path", "assets"); 使用request.add("path", "assets,0,id"); .

暂无
暂无

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

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