soliditysmartcontractschainlinkavalanche

Chainlink: Oracle Get external API data on Avalanche Testnet Fuji


I'm building a smartcontract that interacts with chainlink oracle to get external data from an api call. I did that on ethereum kovan testnet and it works with the following oracle address & job ids but it didn't work for Avalanche tesnet

And

Below you can find the solidity code that I deployed on ethereum kovan testnet & avalanche fuji testnet using Remix. (inside of the constructor you can find commented the script for Avalanche fuji testnet.)

Here are the two contract deployed:

Ethereum Kovan: https://kovan.etherscan.io/address/0xD20dd5ee7844E27Fa2DD437744f986C121aDDE0f

Avalanche Fuji: https://testnet.snowtrace.io/address/0xfb0368e7a97b2ea12980c3900e9c52806e68e8a6

I noticed that on the "events" tab, on Kovan I have two events:

chainlinkRequested & chainlinkFulfilled
enter image description here

On Fuji I have only one event ...:

only chainlinkRequested so it seems that the oracle doesn't return the data to emit the event: chainlinkFulfilled ...
enter image description here

Could you please help me go through this and get back the api data to the smartcontract? Otherwise do you have any documentation that help me build my own oracle? (I already have the cryptozombies.io doc so please share other doc if you have.)

    // 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 APIPlayerScore is ChainlinkClient {
    using Chainlink for Chainlink.Request;
  
    uint256 public playerScore;
    
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;
    
    constructor() {
        //Ethereum Kovan
        setPublicChainlinkToken();
        oracle = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8;
        jobId = "d5270d1c311941d0b08bead21fea7747";
        fee = 0.1 * 10 ** 18; // (Varies by network and job)

        //Avalanche Fuji
        //setChainlinkToken(0x0b9d5D9136855f6FEc3c0993feE6E9CE8a297846);
        //oracle = 0xCC80934EAf22b2C8dBf7A69e8E0D356a7CAc5754;
        //jobId = "5ca4fa9b2d64462290abfbda84e38cf4";
        //fee = 0.1 * 10 ** 18;
    }
    
    /**
     * Create a Chainlink request to retrieve API response, find the target
     * data, then multiply by 1000000000000000000 (to remove decimal places from data).
     */
    function requestPlayerScoreData() 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://****database.app/data.json");
        
        // Set the path to find the desired data in the API response, where the response format is:
        // {"player": {
        //            "id": "4291820",
        //            "score": 560
        //        }
        //  }
        request.add("path", "player.score");
        
        
        // Sends the request
        return sendChainlinkRequestTo(oracle, request, fee);
    }
    
    /**
     * Receive the response in the form of uint256
     */ 
    function fulfill(bytes32 _requestId, uint256 _score) public recordChainlinkFulfillment(_requestId)
    {
        playerScore = _score;
    }

    // function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract
}

Solution

  • I succeed to implement an external API call from a smart-contract by using Frensprotocol oracle. The documentation helped. And to get the tokens to make the Oracle work you can contact their team on Discord or Twitter.