akhil
akhil

Reputation: 31

How to add 2 nodes in chainlink smart contract

This is chainlink sample code Here, in this code I added only 1 oracle and 1 job id but the problem is how can I add 2 nodes (2 oracles and 2 job ids) for getting a response from a single URL i.e 2 nodes must verify the URL data before coming into the blockchain.

pragma solidity ^0.6.0;

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

contract APIConsumer is 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;
}

}

Upvotes: 0

Views: 272

Answers (1)

Harry Papacharissiou
Harry Papacharissiou

Reputation: 856

You can add the oracle and jobId parameters to the requestVolumeData function, and then call it twice, each time passing in a different jobId and 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);
}

Then in your fulfill function, you can store the results in an array or some other data structure so that the second response doesn't overwrite the first response. Or have 1 variable for each response, and have logic to check which one to populate (if both null, populate first, else populate second etc). In this function, you can also have a check to see if the minimum amount of responses has been reached and if so to then do validation on the responses

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

Upvotes: 1

Related Questions