我一直在学习链接API,并试图修改Chainlin文档中的示例,以从API获得byets32
值。示例的原始代码运行良好,但由于我使用的API返回byets32
foem,因此需要配置Chainlink作业来处理这种类型的返回。这里给出了Kovan测试网的节点。这是我的代码
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
}
CCD_ 3的值应该是CCD_;CCCAGG";如API中所示。但我一直得到的只是0x0...00
,这意味着market
还没有被修改。我已经通过各种方式对此进行了检查,发现fulfill
函数永远不会出错。然后,当我更改jobId
和oracle
以处理get-> int256
、get -> bool
时,也会发生同样的事情(当然,我确实更改了变量的返回类型,使其与API的返回形式一致(。我还注意到只有作业get -> uint256
运行良好(文档中的示例也使用了此作业(。有人知道为什么吗?是我的代码错了,还是问题来自节点/作业?既然我能把这个例子做对,我不认为问题出在我的钱包里。
任何帮助都将不胜感激!
您可以使用GET进行测试>字符串作业
- 转到此链接:https://docs.chain.link/docs/any-api-testnet-oracles/
- 获取与您的网络相关的Oracle合同地址。例如:Goerli的0xCC79157eb46F5624204f47AB42b3906cAA40eaB7
- 使用Get>字符串作业:7d80a6386ef543a3abb52817f6707e3b
确保回调函数期望接收一个字符串。(请参阅此处的完整示例:https://docs.chain.link/docs/api-array-response/)