【问题标题】:How to read a JSON file using a chainlink oracle如何使用 chainlink oracle 读取 JSON 文件
【发布时间】:2021-07-20 00:18:25
【问题描述】:

我已更改链链接 APIconsumer example 以读取 JSON 文件,其中包含我希望带入并存储在智能合约中的数据

pragma solidity ^0.6.0;

import "https://raw.githubusercontent.com/smartcontractkit/chainlink/develop/evm-contracts/src/v0.6/ChainlinkClient.sol";

contract APIConsumer is ChainlinkClient {
  
    string public Name;
    
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;
    
    /**
     * Network: Kovan
     * Oracle: Chainlink - 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e
     * Job ID: Chainlink - 50fc4215f89443d185b061e5d7af9490
     * Fee: 0.1 LINK
     */
    constructor() public {
        setPublicChainlinkToken();
        oracle = 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e;
        jobId = "50fc4215f89443d185b061e5d7af9490";
        fee = 0.1 * 10 ** 18; // 0.1 LINK
    }
    
    /**
     * Create a Chainlink request to retrieve API response, find the target price
     * data, then multiply by 100 (to remove decimal places from price).
     */
    function requestAthleteData() 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://1e68b62e-578e-4390-bf43-6b70a92a23b6.mock.pstmn.io/get");
        
        // Set the path to find the desired data in the API response, where the response format is:
        // {"USD":243.33}
        request.add("path", "Name");
        
        // Multiply the result by 100 to remove decimals
       // request.addInt("times", 100);
        
        // Sends the request
        return sendChainlinkRequestTo(oracle, request, fee);
    }
    
    /**
     * Receive the response in the form of uint256
     */ 
    function fulfill(bytes32 _requestId, string memory _name) public recordChainlinkFulfillment(_requestId)
    {
        Name = _name;
    }
}

这是它试图读取的数据: https://1e68b62e-578e-4390-bf43-6b70a92a23b6.mock.pstmn.io/get

[ { "Name": "Tom", "Birthday": "2021-07-01", "Nationality": "SA", "Address": "123 st st" } ]

部署没有问题,但是当我调用函数“requesttAthletedata”时,它会处理它但不返回任何内容。我在某处错过了一步吗?还是代码有问题?

【问题讨论】:

    标签: json solidity chainlink


    【解决方案1】:

    首先,将fulfill()中的_name参数改为bytes32。

    其次,将您的请求路径更改为: request.add("path", "0.Name");

    Chainlink 目前无法将字符串写入智能合约,只能将字节 32 转换为字符串。此外,您的 JSON 对象位于数组中(在第一个索引处),这就是我们需要将“0.Name”指定为 JSON 路径的原因。

    第三,如果您想在智能合约中将 bytes32 转换为字符串,则需要在 fulfill() 方法中进行。 您的最终代码应如下所示:

    pragma solidity ^0.6.0;
    
    import "@chainlink/contracts/src/v0.6/ChainlinkClient.sol";
    
    contract test is ChainlinkClient {
      
        string public Name;
        
        address private oracle;
        bytes32 private jobId;
        uint256 private fee;
        
        /**
         * Network: Kovan
         * Oracle: Chainlink - 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e
         * Job ID: Chainlink - 50fc4215f89443d185b061e5d7af9490
         * Fee: 0.1 LINK
         */
        constructor() public {
            setPublicChainlinkToken();
            oracle = 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e;
            jobId = "50fc4215f89443d185b061e5d7af9490";
            fee = 0.1 * 10 ** 18; // 0.1 LINK
        }
        
        /**
         * Create a Chainlink request to retrieve API response, find the target price
         * data, then multiply by 100 (to remove decimal places from price).
         */
        function requestAthleteData() 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://1e68b62e-578e-4390-bf43-6b70a92a23b6.mock.pstmn.io/get");
            
            // Set the path to find the desired data in the API response, where the response format is:
            // {"USD":243.33}
            request.add("path", "0.Name");
            
            // Multiply the result by 100 to remove decimals
           // request.addInt("times", 100);
            
            // 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);
        }
         
        function fulfill(bytes32 _requestId, bytes32  _name) public recordChainlinkFulfillment(_requestId)
        {
           string memory stringName = bytes32ToString(_name);
            Name = stringName;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 2017-12-23
      • 1970-01-01
      • 2022-12-12
      • 2017-10-03
      • 1970-01-01
      • 2018-10-20
      相关资源
      最近更新 更多