1

I need to introduce a uint in my Ethereum contract. I am testing this code in Kovan in base to this example: https://docs.chain.link/docs/make-a-http-get-request/

pragma solidity ^0.6.0;

import "https://raw.githubusercontent.com/smartcontractkit/chainlink/develop/evm-contracts/src/v0.6/ChainlinkClient.sol";

// SPDX-License-Identifier: UNLICENSED

contract ChainlinkExample is ChainlinkClient {
  
    uint256 public price;
    
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;
    
    constructor() public {
        setPublicChainlinkToken();
        oracle = 0x2f90A6D021db21e1B2A077c5a37B3C7E75D15b7e;
        jobId = "29fa9aa13bf1468788b7cc4a500a45b8";
        fee = 0.1 * 10 ** 18; // 0.1 LINK
    }

    function requestEthereumPrice() public returns (bytes32 requestId){
        Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
            req.add("get", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD");//{"USD":1797.12}
            req.add("path", "USD");
            req.addInt("times", 100);
        return sendChainlinkRequestTo(oracle, request, fee);
    }

    function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId){
        price = _price;
    }

}

All works correctly, but "requestEthereumPrice()" function takes a few seconds to save the variable "price".

How could I call a request, wait the answer and continue with my next code claculations?

For example, how can I request the API price and make an operation and return the result in the same function call?

1
  • Please don't add another question to the same question in stackoverflow. Can you please create a new question titled "How to return a Chainlink API Call to another contract"? Jun 4 '21 at 15:10
2

You'd want to place all your logic in the callback function, in this case fulfill.

For example, if you wanted to mint some token based on the return of an API call, you'd do something like:

function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId){
        price = _price;
        mint(price);
        // any other logic here
    }

Chainlink API Calls are asynchronous by nature, so you don't make an API call and get a response in the same block.

3
  • I need to call the "requestEthereumPrice()" function from other contract. How can I return the "price" to the parent contract?
    – JTConsulta
    Jun 3 '21 at 22:39
  • You'd want the callback address to be the other contract. buildChainlinkRequest(jobId, address(this), this.fulfill.selector); swap out address(this) for whatever address you want it to return to. Jun 3 '21 at 23:14
  • Hi Patrick, I am trying but I can't get it to work in a simple way. Could you guide me? Thanks. (Look the EDIT section)
    – JTConsulta
    Jun 4 '21 at 9:59

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.