0

I was going through the Chainlink tutorial on APIs and had some questions about the fulfill function in the contract below. From how I understand it, the third argument of request object inside the requestVolumeData() function tells the Chainlink node what method to call when it returns the data. In this case, it is the fulfill() function. Does this mean that if I were to make a request to an oracle node, the oracle node would automatically know to call fulfill() once it's got the requested data? In other words, I don't need to enter any parameters into fulfill() manually to get back data?

In addition, in Patrick's video at 09:57 (these videos are so awesome! Thank you Patrick!), he clicks on the volume variable in remix to get the number, which is not happening for me. I'm getting only zeros even when clicking on volume for a second time like Patrick does. Some clarification on this step would help a lot. Thank you so much!

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

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

/**
 * Request testnet LINK and ETH here: https://faucets.chain.link/
 * Find information on LINK Token Contracts and get the latest ETH and LINK faucets here: https://docs.chain.link/docs/link-token-contracts/
 */

/**
 * THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY.
 * PLEASE DO NOT USE THIS CODE IN PRODUCTION.
 */
contract APIConsumer is ChainlinkClient {
    using Chainlink for Chainlink.Request;
  
    uint256 public volume;
    
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;
    
    /**
     * Network: Kovan
     * Oracle: 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8 (Chainlink Devrel   
     * Node)
     * Job ID: d5270d1c311941d0b08bead21fea7747
     * Fee: 0.1 LINK
     */
    constructor() {
        setPublicChainlinkToken();
        oracle = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8;
        jobId = "d5270d1c311941d0b08bead21fea7747";
        fee = 0.1 * 10 ** 18; // (Varies by network and job)
    }
    
    /**
     * 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:
        // {"RAW":
        //   {"ETH":
        //    {"USD":
        //     {
        //      "VOLUME24HOUR": xxx.xxx,
        //     }
        //    }
        //   }
        //  }
        request.add("path", "RAW.ETH.USD.VOLUME24HOUR");
        
        // Multiply the result by 1000000000000000000 to remove decimals
        int 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;
    }

    // function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract
}
2
  • The volume variable Did you fund your deployed smart contract with LINK? Mar 26 at 4:47
  • Hi @SolangeGueiros! Yes~ My contract was funded with LINK.
    – kay_m43
    Mar 26 at 19:28

1 Answer 1

0

Yes! The third argument of request object inside the requestVolumeData() function tells the Chainlink node what method to call when it returns the data. In this case, it is the fulfill() function, defined in line 72

Yes! This mean that if I were to make a request to an oracle node, the oracle node would automatically know to call fulfill() once it's got the requested data. And I don't need to enter any parameters into fulfill() manually to get back data.

Important notice that for all Chainlink API calls, the fulfil method only takes 2 arguments.

  • bytes32 _requestId The requestId of the Chainlink API call.
  • <type> _data The _data that is being returned by the Chainlink API call.
1
  • Thank you so much for the clarification! I was able to troubleshoot the issue. The path adapters in Chainlink node version 1 used dot-delimited strings. The newest version uses comma-delimited strings. So,"RAW.ETH.USD.VOLUME24HOUR" should be "RAW,ETH,USD,VOLUME24HOUR"
    – kay_m43
    Mar 26 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.