如何使用Chainlink Oracle读取JSON文件 [英] How to read a JSON file using a chainlink oracle

查看:8
本文介绍了如何使用Chainlink Oracle读取JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将链链接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" } ]

它在部署时没有问题,但是当我调用函数‘questtAthletedata’时,它会处理该函数,但不会返回任何内容。我是不是在什么地方漏了一步?还是代码有问题?

推荐答案

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

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

Chainlink当前不能将字符串写入智能合同,只能将bytes32写入,然后可以将其转换为字符串。另外,您的JSON对象在一个数组中(在第一个索引处),这就是为什么我们需要将&qot;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;
    }
}

这篇关于如何使用Chainlink Oracle读取JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆