Solidity

Chainlink “任何 API” 返回 0

  • September 15, 2021

我想使用 chainlink API 功能,但我無法獲取數據。當我嘗試執行 requestEthereumPrice 函式時,我得到“uint256:0”我正在使用 Kovan 測試網和元遮罩。

pragma solidity ^0.6.0;

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

// MyContract inherits the ChainlinkClient contract to gain the
// functionality of creating Chainlink requests
contract ChainlinkExample is ChainlinkClient  {
   using Chainlink for Chainlink.Request;
 // Stores the answer from the Chainlink oracle
 uint256 public currentPrice;
 address public owner;
 
   // The address of an oracle - you can find node addresses on https://market.link/search/nodes 
 address ORACLE_ADDRESS = 0xe0090e7AB96123FDE1D3CC8b3f3b342B4aA60a2E;
 
 // The address of the http get job - you can find job IDs on https://market.link/search/jobs
 string constant JOBID = "390abeb54c8b4bd0ae079699ba883bf2";
 
 // 17 0s = 0.1 LINK 
 // 18 0s = 1 LINK 
 uint256 constant private ORACLE_PAYMENT = 100000000000000000;

 constructor() public {
   // Set the address for the LINK token for the network
   setPublicChainlinkToken();
   owner = msg.sender;
 }

 // Creates a Chainlink request with the uint256 multiplier job
 // Ideally, you'd want to pass the oracle payment, address, and jobID as 
 function requestEthereumPrice() 
   public
   onlyOwner
 {
   // newRequest takes a JobID, a callback address, and callback function as input
   Chainlink.Request memory req = buildChainlinkRequest(stringToBytes32(JOBID), address(this), this.fulfill.selector);
   // Adds a URL with the key "get" to the request parameters
   req.add("get", "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD");
   // Uses input param (dot-delimited string) as the "path" in the request parameters
   req.add("path", "USD");
   // Adds an integer with the key "times" to the request parameters
   req.addInt("times", 100);
   // Sends the request with the amount of payment specified to the oracle
   sendChainlinkRequestTo(ORACLE_ADDRESS, req, ORACLE_PAYMENT);
 }

 // fulfill receives a uint256 data type
 function fulfill(bytes32 _requestId, uint256 _price)
   public
   // Use recordChainlinkFulfillment to ensure only the requesting oracle can fulfill
   recordChainlinkFulfillment(_requestId)
 {
   currentPrice = _price;
 }
 
 // cancelRequest allows the owner to cancel an unfulfilled request
 function cancelRequest(
   bytes32 _requestId,
   uint256 _payment,
   bytes4 _callbackFunctionId,
   uint256 _expiration
 )
   public
   onlyOwner
 {
   cancelChainlinkRequest(_requestId, _payment, _callbackFunctionId, _expiration);
 }

 
 // withdrawLink allows the owner to withdraw any extra LINK on the contract
 function withdrawLink()
   public
   onlyOwner
 {
   LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
   require(link.transfer(msg.sender, link.balanceOf(address(this))), "Unable to transfer");
 }
 
 modifier onlyOwner() {
   require(msg.sender == owner);
   _;
 }
 
  // A helper funciton to make the string a bytes32
 function stringToBytes32(string memory source) private pure returns (bytes32 result) {
   bytes memory tempEmptyStringTest = bytes(source);
   if (tempEmptyStringTest.length == 0) {
     return 0x0;
   }
   assembly { // solhint-disable-line no-inline-assembly
     result := mload(add(source, 32))
   }
 }
}

那個 oracle 地址和 jobId 看起來像是處於非活動狀態。這些應該工作:

// The address of an oracle - you can find node addresses on https://market.link/search/nodes 
 address ORACLE_ADDRESS = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8;
 
// The address of the http get job - you can find job IDs on https://market.link/search/jobs
 string constant JOBID = "d5270d1c311941d0b08bead21fea7747";

您可以通過etherscan 並輸入 oracle 地址來檢查節點的狀態,以查看它是否響應您的請求。如果它們處於非活動狀態,您可以嘗試在market.link上搜尋新節點或參考Chainlink 官方文件中的預設節點。

引用自:https://ethereum.stackexchange.com/questions/108176