Search code examples
solidityweb3jshardhatchainlink

Requesting data with Chainlink, response not what expected


I've been trying to integrate Chainlink into my contract, managed to get the random number thingy working, but the API call doesn't work for me. Here's what I got:

contract ValorantCards is Ownable, ERC1155, VRFConsumerBase, ChainlinkClient {
using Chainlink for Chainlink.Request;

address private linkToken;

// Chainlink VRF
bytes32 private keyHash;
uint256 private vrfFee;

uint256 public randomResult;

// Chainlink API calls
address private oracle;
bytes32 private jobId;
uint256 private oracleFee;

uint256 public playerLevel;

constructor(
    address _vrfCoordinator,
    address _linkToken,
    bytes32 _keyHash,
    address _oracle,
    bytes32 _jobId,
    uint256 _oracleFee
) ERC1155("") VRFConsumerBase(_vrfCoordinator, _linkToken) {
    setPublicChainlinkToken();

    linkToken = _linkToken;
    keyHash = _keyHash;
    vrfFee = 0.1 * 10**18;

    oracle = _oracle;
    jobId = _jobId;
    oracleFee = _oracleFee;
}

function requestUserLevel() public returns (bytes32 requestId) {
    Chainlink.Request memory request = buildChainlinkRequest(
        jobId,
        address(this),
        this.fulfill.selector
    );

    request.add(
        "get",
        "https://api.henrikdev.xyz/valorant/v1/account/draven/2023"
    );
    request.add("path", "data.account_level");

    return sendChainlinkRequestTo(oracle, request, oracleFee);
}

function fulfill(bytes32 _requestId, uint256 _level)
    public
    recordChainlinkFulfillment(_requestId)
{
    playerLevel = _level;
}

I'm deploying from hardhat, with the following parameters (ignoring the ones for VRF since that's working):

Oracle: 0x9C0383DE842A3A0f403b0021F6F85756524d5599

JobId: 0x3766623533366265383635623433333662323766633130313437633139336337

OracleFee: 0.1 * 10**18

The function runs fine, the transaction doesn't revert or anything, but when I check "playerLevel", it's always just 0


Solution

  • Looking at the Etherscan activity, it looks like the node you are using may be inactive. Try this node and jobId:

    Oracle = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8; 
    JobId = "d5270d1c311941d0b08bead21fea7747";
    

    These were taken from the Chainlink Official Docs.

    To check to see if a node may be inactive or not, check out the oracle address in a block explorer. You can see here that the original node you tried to use hasn't posted a transaction in awhile.

    If a node is inactive you will need to find a new one or host one yourself. To find more nodes and jobs, you can check market.link or use the one found in the docs as mentioned earlier.