Ramit Agarwal
Ramit Agarwal

Reputation: 11

Problem in fetching data from external API on Matic Mumbai Testnet

This is the documentation of my used API.

The price is still showing 0 as you can see in this image reference.

Here is my code:

pragma solidity ^0.6.0;

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

contract APIConsumer is ChainlinkClient {



event below20(uint _price);
event below30(uint _price);
event below40(uint _price);
event below50(uint _price);

uint256 public Price;

address private oracle;
bytes32 private jobId;
uint256 private fee;


constructor() public {
    setChainlinkToken(0x326C977E6efc84E512bB9C30f76E30c160eD06FB);
    oracle =  0xb33D8A4e62236eA91F3a8fD7ab15A95B9B7eEc7D;
    jobId = "da20aae0e4c843f6949e5cb3f7cfe8c";
    fee = 10 ** 16; // 0.01 LINK
}

 
 
function requestBTCCNYPrice() 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://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=BTC&to_currency=CNY&apikey=demo");
    
    string[] memory path = new string[](2);
    path[0] = "Realtime Currency Exchange Rate";
    path[1] = "5. Exchange Rate";
    request.addStringArray("path", path);


    request.addInt("times", 10000000000);
    
    // Sends the request
    return sendChainlinkRequestTo(oracle, request, fee);
}

/**
 * Receive the response in the form of uint256
 */ 
function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId)
{
    Price = _price;
}

Upvotes: 1

Views: 218

Answers (1)

Zak Ayesh
Zak Ayesh

Reputation: 631

That node is down. Use the following node and JobID in this code:

pragma solidity ^0.6.0;

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

contract APIConsumer is ChainlinkClient {



event below20(uint _price);
event below30(uint _price);
event below40(uint _price);
event below50(uint _price);

uint256 public Price;

address private oracle;
bytes32 private jobId;
uint256 private fee;


constructor() public {
    setChainlinkToken(0x326C977E6efc84E512bB9C30f76E30c160eD06FB);
    oracle =  0xc8D925525CA8759812d0c299B90247917d4d4b7C;
    jobId = "bbf0badad29d49dc887504bacfbb905b";
    fee = 10 ** 16; // 0.01 LINK
}

 
 
function requestBTCCNYPrice() 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://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=BTC&to_currency=CNY&apikey=demo");
    
    string[] memory path = new string[](2);
    path[0] = "Realtime Currency Exchange Rate";
    path[1] = "5. Exchange Rate";
    request.addStringArray("path", path);


    request.addInt("times", 10000000000);
    
    // Sends the request
    return sendChainlinkRequestTo(oracle, request, fee);
}

/**
 * Receive the response in the form of uint256
 */ 
function fulfill(bytes32 _requestId, uint256 _price) public recordChainlinkFulfillment(_requestId)
{
    Price = _price;
}
}

To check to see if a node is running 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 quite a long time.

To find nodes and jobs that are currently working, you should check market.link.

Upvotes: 1

Related Questions