微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在chainlink智能合约中添加2个节点

如何解决如何在chainlink智能合约中添加2个节点

这是链链接示例代码在这里在这代码中,我只添加了 1 个 oracle 和 1 个作业 ID,但问题是如何添加 2 个节点(2 个 oracle 和 2 个作业 ID)以从单个 URL 获取响应,即 2节点在进入区块链之前必须验证 URL 数据。

pragma solidity ^0.6.0;

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

合约 APIConsumer 是 ChainlinkClient { uint256 公开卷;

address private oracle;
bytes32 private jobId;
uint256 private fee;

/**
 * Network: Kovan
 * Chainlink - 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e
 * Chainlink - 29fa9aa13bf1468788b7cc4a500a45b8
 * Fee: 0.1 LINK
 */
constructor() public {
    setPublicChainlinkToken();
    oracle = 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e;
    jobId = "29fa9aa13bf1468788b7cc4a500a45b8";
    fee = 0.1 * 10**18; // 0.1 LINK
}

/**
 * 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:
    request.add("path","RAW.ETH.USD.VOLUME24HOUR");

    // Multiply the result by 1000000000000000000 to remove decimals
    int256 timesAmount = 10**18;
    request.addInt("times",timesAmount);

    // Sends the request
    return sendChainlinkRequestTo(oracle,request,fee);
}

/**
 * Receive the response in the form of uint256
 */

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

}

解决方法

可以在requestVolumeData函数中添加oracle和jobId参数,然后调用两次,每次传入不同的jobId和oracle。

uint[2] storage responses;   

function requestVolumeData(bytes32 _jobId,address _oracle) 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:
    request.add("path","RAW.ETH.USD.VOLUME24HOUR");

    // Multiply the result by 1000000000000000000 to remove decimals
    int256 timesAmount = 10**18;
    request.addInt("times",timesAmount);

    // Sends the request
    return sendChainlinkRequestTo(_oracle,request,fee);
}

然后在您的执行函数中,您可以将结果存储在数组或其他一些数据结构中,以便第二个响应不会覆盖第一个响应。或者每个响应有 1 个变量,并且有逻辑来检查要填充哪个变量(如果两者都为空,则首先填充,否则填充第二个等等)。在此功能中,您还可以检查是否已达到最小响应数量,如果已达到,则对响应进行验证

function fulfill(bytes32 _requestId,uint256 _volume)
public recordChainlinkFulfillment(_requestId)
{
    responses.push(_volume)
    if responses.length > some number {
       //do something with the responses
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。