0

これはチェーンリンクのサンプル コードです。このコードでは、1 つのオラクルと 1 つのジョブ ID のみを追加しましたが、問題は、単一の URL から応答を取得するために 2 つのノード (2 つのオラクルと 2 つのジョブ ID) を追加する方法です。つまり、2 つのノードを検証する必要があります。ブロックチェーンに入る前の URL データ。

プラグマ固体性 ^0.6.0;

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

コントラクト APIConsumer は ChainlinkClient { uint256 public volume;

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;
}

}

4

1 に答える 1

1

oracle パラメーターと jobId パラメーターを requestVolumeData 関数に追加し、それを 2 回呼び出して、毎回異なる 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);
}

次に、フルフィルメント関数で、結果を配列またはその他のデータ構造に保存して、2 番目の応答が最初の応答を上書きしないようにすることができます。または、応答ごとに 1 つの変数を用意し、どちらを入力するかをチェックするロジックを用意します (両方が null の場合は最初に入力し、そうでない場合は 2 番目に入力するなど)。この関数では、応答の最小量に達したかどうかを確認し、到達した場合は応答の検証を行うこともできます。

function fulfill(bytes32 _requestId, uint256 _volume)
public recordChainlinkFulfillment(_requestId)
{
    responses.push(_volume)
    if responses.length > some number {
       //do something with the responses
    }
}
于 2021-06-21T01:30:10.970 に答える