【发布时间】:2022-08-10 17:12:51
【问题描述】:
我一直在学习 chainlink API 并尝试从 Chainlin 的文档中修改 example 以从 API 中获取 byets32 值。该示例的原始代码运行良好,但由于我使用的 API 返回一个 byets32 foem,因此需要配置 Chainlink 作业以处理这种类型的返回。该节点通过 Kovan 测试网获得 here。这是我的代码
pragma solidity ^0.8.7;
import \"@chainlink/contracts/src/v0.8/ChainlinkClient.sol\";
/**
* Request testnet LINK and ETH here: https://faucets.chain.link/
* Find information on LINK Token Contracts and get the latest ETH and LINK faucets here: https://docs.chain.link/docs/link-token-contracts/
*/
/**
* THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY.
* PLEASE DO NOT USE THIS CODE IN PRODUCTION.
*/
contract APIConsumer is ChainlinkClient {
using Chainlink for Chainlink.Request;
//Result of the api
bytes32 public martket;
address private oracle;
bytes32 private jobId;
uint256 private fee;
/**
* Get -> bytes32
* Network: Kovan
* Oracle: 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8 (Chainlink Devrel
* Node)
* Job ID: 7401f318127148a894c00c292e486ffd
* Fee: 0.1 LINK
*/
constructor() {
setPublicChainlinkToken();
// Get -> bytes32 node taken from documentation
oracle = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8;
jobId = \"7401f318127148a894c00c292e486ffd\";
fee = 0.1 * 10 ** 18; // (Varies by network and job)
}
/**
* Create a Chainlink request to retrieve API response, find the target
* data, then multiply by 1000000000000000000 (to remove decimal places from data).
*/
function requestVolumeData() 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://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD\");
// Set the path to find the desired data in the API response, where the response format is:
// {\"RAW\":
// {\"ETH\":
// {\"USD\":
// {
// \"MARKET\": \"CCCAGG\",
// }
// }
// }
// }
//Get the MARKET field of API
request.add(\"path\", \"RAW,ETH,USD,MARKET\"); // Chainlink nodes 1.0.0 and later support this format
// Sends the request
return sendChainlinkRequestTo(oracle, request, fee);
}
/**
* Receive the response in the form of bytes32
*/
function fulfill(bytes32 _requestId, bytes32 _market) public recordChainlinkFulfillment(_requestId)
{
martket = _market;
}
// function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract
}
market 的值应该是一个 byets32 代表“CCCAGG”,如 API 中所示。但是我得到的只是0x0...00,这意味着market还没有被修改过。我已经检查了各种方法,发现fulfill 函数永远不会运行。然后当我将jobId和oracle更改为处理get-> int256、get -> bool时也会发生同样的事情(当然我确实更改了变量的返回类型,使其与API的返回形式一致) .我还注意到只有工作 get -> uint256 运作良好(文档中的示例也使用了这个工作)。有人知道为什么吗?是我的代码错误还是问题来自节点/作业?由于我能够正确地获得示例,因此我认为问题不是来自我的钱包。
任何帮助,将不胜感激!
-
目前正在进行一些关于新 TOML 作业格式的 get-bytes32 作业的内部讨论。当我更清楚时,我会给出正确的答案。但与此同时,作为一种解决方法,您可以使用“大响应”解决方案来返回任意长度的字节数据docs.chain.link/docs/large-responses
-
1. 看起来你的jobid 可能已经不存在了。要找一个活的工作,试试market.link 2. \"CCCAGG\" 字符串可能不会转换为 bytes32,因为它的长度不等于 32。