【问题标题】:Problem in fetching data from external API on Matic Mumbai Testnet从 Matic Mumbai 测试网上的外部 API 获取数据的问题
【发布时间】:2021-08-03 21:37:32
【问题描述】:

这是我使用的 API 的documentation

正如您在 image reference 中看到的那样,价格仍然显示为 0。

这是我的代码:

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;
}

【问题讨论】:

  • 您是否通过链接查看docs.chain.link/docs/link-token-contracts 为您的合同提供资金?其他想法是您是否等待一段时间才能在街区上实现?
  • 您能否在此处添加您的 API 响应,这样人们就不必单击该链接?所有信息都应在问题内的 stackoverflow 问题中提供。

标签: chainlink


【解决方案1】:

该节点已关闭。在此代码中使用以下节点和 JobID:

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;
}
}

要检查节点是否正在运行,请在区块浏览器中查看预言机地址。 You can see here,您尝试使用的原始节点已经很长时间没有发布交易了。

要查找当前正在工作的节点和作业,您应该检查market.link

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 2017-07-06
    • 1970-01-01
    • 2011-12-21
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多