链链接 - 以`bytes32`的形式获取API时问题

发布于 2025-01-19 02:02:35 字数 3524 浏览 1 评论 0原文

我一直在学习链条API,并试图修改示例文档以从API获取byets32值。该示例的原始代码运行良好,但是由于API I正在返回byets32 foem,因此需要配置链条作业以处理此类型的返回。该节点 eres with kovan testnet。这是我的代码

pragma solidity ^0.8.7;

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

/**
 * Request testnet LINK and ETH here: https://faucets.chain.link/
 * Find information on LINK Token Contracts and get the latest ETH and LINK faucets here: https://docs.chain.link/docs/link-token-contracts/
 */

/**
 * THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY.
 * PLEASE DO NOT USE THIS CODE IN PRODUCTION.
 */
contract APIConsumer is ChainlinkClient {
    using Chainlink for Chainlink.Request;
  
     //Result of the api
     bytes32 public martket;
    
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;
    
    /**
     * Get -> bytes32
     * Network: Kovan
     * Oracle: 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8 (Chainlink Devrel   
     * Node)
     * Job ID: 7401f318127148a894c00c292e486ffd
     * Fee: 0.1 LINK
     */
    constructor() {
        setPublicChainlinkToken();
        // Get -> bytes32 node taken from documentation
        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 requestVolumeData() 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://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD");
        
        // Set the path to find the desired data in the API response, where the response format is:
        // {"RAW":
        //   {"ETH":
        //    {"USD":
        //     {
        //      "MARKET": "CCCAGG",
        //     }
        //    }
        //   }
        //  }
        
        //Get the MARKET field of API
        request.add("path", "RAW,ETH,USD,MARKET"); // Chainlink nodes 1.0.0 and later support this format
        
       
        
        // Sends the request
        return sendChainlinkRequestTo(oracle, request, fee);
    }
    
    /**
     * Receive the response in the form of bytes32
     */ 
    function fulfill(bytes32 _requestId, bytes32 _market) public recordChainlinkFulfillment(_requestId)
    {
        martket = _market;
    }

    // function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract
}

市场的值应为byets32在API中显示的“ cccagg”。但是我所得到的只是0x0 ... 00,这意味着Market尚未修改。我已经检查了各种方式,并发现实现功能永远不会得到运行。然后,当我更改jobidoracle以处理get->时,也会发生同样的事情。 int256get-> bool(当然,我确实更改了变量的返回类型,以使其与API的返回形式一致)。我还注意到只有作业获取 - > UINT256效果很好(文档中的示例也使用了此作业)。有人知道为什么吗?我的代码错误还是问题来自节点/作业?由于我能够正确地说明示例,所以我认为钱包里的问题凸轮不可能。

任何帮助将不胜感激!

I've been learning the chainlink API and trying to modify the example from the Chainlin's documentation to get a byets32 value from an API. The original code of the example works well, but since the API I was hitting is returning a byets32 foem, the Chainlink job need to be configured to handle this type of return. The node is given here with Kovan testnet. Here is my code

pragma solidity ^0.8.7;

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

/**
 * Request testnet LINK and ETH here: https://faucets.chain.link/
 * Find information on LINK Token Contracts and get the latest ETH and LINK faucets here: https://docs.chain.link/docs/link-token-contracts/
 */

/**
 * THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY.
 * PLEASE DO NOT USE THIS CODE IN PRODUCTION.
 */
contract APIConsumer is ChainlinkClient {
    using Chainlink for Chainlink.Request;
  
     //Result of the api
     bytes32 public martket;
    
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;
    
    /**
     * Get -> bytes32
     * Network: Kovan
     * Oracle: 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8 (Chainlink Devrel   
     * Node)
     * Job ID: 7401f318127148a894c00c292e486ffd
     * Fee: 0.1 LINK
     */
    constructor() {
        setPublicChainlinkToken();
        // Get -> bytes32 node taken from documentation
        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 requestVolumeData() 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://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD");
        
        // Set the path to find the desired data in the API response, where the response format is:
        // {"RAW":
        //   {"ETH":
        //    {"USD":
        //     {
        //      "MARKET": "CCCAGG",
        //     }
        //    }
        //   }
        //  }
        
        //Get the MARKET field of API
        request.add("path", "RAW,ETH,USD,MARKET"); // Chainlink nodes 1.0.0 and later support this format
        
       
        
        // Sends the request
        return sendChainlinkRequestTo(oracle, request, fee);
    }
    
    /**
     * Receive the response in the form of bytes32
     */ 
    function fulfill(bytes32 _requestId, bytes32 _market) public recordChainlinkFulfillment(_requestId)
    {
        martket = _market;
    }

    // function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract
}

The value of market should be a byets32 reprsenting "CCCAGG" as shown in the API. But what I got was just 0x0...00 all the time, which means market has not been modified yet. I've checked this various ways and found out that the fulfill function never get rans. Then same thing happens when I changed the jobId and oracle to handle get-> int256, get -> bool (of course I did change the return type of the variable such that it's consistent with the returning form of API). I also noticed that only the job get -> uint256 works well (the example from documentation also used this job). Does anyon know why? Was my code wrong or the problem came from the node/job? Since I was able the get the example right, I don't think the problem cam from my wallet.

Any help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动听の歌 2025-01-26 02:02:35

您可以使用get> string作业

  1. 进行此链接: https:/ /docs.chain.link/docs/any-api-testnet-oracles/
  2. 将Oracle合同地址与您的网络相关。例如:0xCC79157EB46F5624204F47AB42B3906CAA40EAB7用于GOERLI
  3. 使用get> string作业:

7D80A6386EF543A3ABBB52817F6707E3B (请参阅此处的完整示例: https://docs.chain.link.link/docs/ api array-response/

You can test with a GET>STRING JOB

  1. Go to this link: https://docs.chain.link/docs/any-api-testnet-oracles/
  2. Take the Oracle contract address relevant to your network. For instance: 0xCC79157eb46F5624204f47AB42b3906cAA40eaB7 for Goerli
  3. Use a Get>String job: 7d80a6386ef543a3abb52817f6707e3b

Make sure that your callback function expects to receive a string. (see a full example here: https://docs.chain.link/docs/api-array-response/)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文