如何使用 Chainlink api 调用通过 api 从 json 返回中检索字符串值
Posted
技术标签:
【中文标题】如何使用 Chainlink api 调用通过 api 从 json 返回中检索字符串值【英文标题】:How do I use a Chainlink api call to retrieve a string value from json returne by api 【发布时间】:2021-12-28 21:30:21 【问题描述】:我正在尝试使用 chainlink 请求进行 api 调用,然后使用 api 调用的结果更新 volume 变量。
api 调用应该检索一个字符串。在部署和资助智能合约之后,我似乎能够成功地进行 api 调用(尽管我对此不确定)。问题是音量变量没有更新。
我从chainlink tutorial 改编了代码。原始教程代码有效,在我看来,我所做的更改也应该有效。有人可以帮忙吗?
// SPDX-License-Identifier: MIT
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;
string public volume;
address private oracle;
bytes32 private jobId;
uint256 private fee;
/**
* Network: Kovan
* Oracle: 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8 (Chainlink Devrel
* Node)
* Job ID: d5270d1c311941d0b08bead21fea7747
* Fee: 0.1 LINK
*/
constructor()
setPublicChainlinkToken();
oracle = 0xF405B99ACa8578B9eb989ee2b69D518aaDb90c1F;
jobId = "c51694e71fa94217b0f4a71b2a6b565a";
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":
//
// "VOLUME24HOUR": xxx.xxx,
//
//
//
//
request.add("path", "RAW.ETH.USD.MARKET");
// // Multiply the result by 1000000000000000000 to remove decimals
// int timesAmount = 10**18;
// request.addInt("times", timesAmount);
// Sends the request
return sendChainlinkRequestTo(oracle, request, fee);
/**
* Receive the response in the form of uint256
*/
function fulfill(bytes32 _requestId, bytes32 _volume) public recordChainlinkFulfillment(_requestId)
volume = bytes32ToString(_volume);
function bytes32ToString(bytes32 _bytes32) public pure returns (string memory)
uint8 i = 0;
while(i < 32 && _bytes32[i] != 0)
i++;
bytes memory bytesArray = new bytes(i);
for (i = 0; i < 32 && _bytes32[i] != 0; i++)
bytesArray[i] = _bytes32[i];
return string(bytesArray);
// function withdrawLink() external - Implement a withdraw function to avoid locking your LINK in the contract
【问题讨论】:
【参考方案1】:要返回一个字符串,你实际上必须返回一个bytes32
并将其转换为链上的字符串。
你可以使用这样的东西,将 bytes32 转换为字符串:
function bytes32ToString(bytes32 _bytes32) public pure returns (string memory)
uint8 i = 0;
while(i < 32 && _bytes32[i] != 0)
i++;
bytes memory bytesArray = new bytes(i);
for (i = 0; i < 32 && _bytes32[i] != 0; i++)
bytesArray[i] = _bytes32[i];
return string(bytesArray);
【讨论】:
以上是关于如何使用 Chainlink api 调用通过 api 从 json 返回中检索字符串值的主要内容,如果未能解决你的问题,请参考以下文章
区块链预言机架构原理:以 Oraclize 与 Chainlink 为例(上)