1

这是我使用的 API 的文档

正如您在此图像参考中看到的那样,价格仍显示为 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;
}
4

1 回答 1

0

该节点已关闭。在此代码中使用以下节点和 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;
}
}

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

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

于 2021-08-13T16:05:53.390 回答