如何使用链链接Oracle读取JSON文件的多个字段
链条 我已经更改了链链接大响应示例,以读取一个我希望带入并存储在智能合约中的json文件
// spdx-license-indistifier:mit Pragma固体 ^0.8.7;
导入'@chainlink/contracts/src/v0.8/chainlinkclient.sol'; 导入'@chainlink/contracts/src/v0.8/conjenteredowner.sol';
/**
- 请求testnet链接和eth在此处: https://faucets.chain.link.link/
- 链接令牌合同,并在此处获取最新的ETH和链接水龙头: https://docs.chain.link/link/docs/link-token-contracts/ */
Contract generlargeresponse是链链接,确认已有{ 使用链链接进行链链接。
// variable bytes(arbitrary-length raw byte data) returned in a single oracle
response
bytes public data;
bytes public imgdata;
string public image;
string public name;
bytes32 private jobId;
uint256 private fee;
/**
* @notice Initialize the link token and target oracle
* @dev The oracle address must be an Operator contract for multiword response
*
*
* Rinkeby Testnet details:
* Link Token: 0x01BE23585060835E02B77ef475b0Cc51aA1e0709
* Oracle: 0xf3FBB7f3391F62C8fe53f89B41dFC8159EE9653f (Chainlink DevRel)
* jobId: 7da2702f37fd48e5b1b9a5715e3509b6
*
*/
constructor() ConfirmedOwner(msg.sender) {
setChainlinkToken(0x01BE23585060835E02B77ef475b0Cc51aA1e0709);
setChainlinkOracle(0xf3FBB7f3391F62C8fe53f89B41dFC8159EE9653f);
jobId = '7da2702f37fd48e5b1b9a5715e3509b6';
fee = (1 * LINK_DIVISIBILITY) / 10; // 0,1 * 10**18 (Varies by network and job)
}
/**
* @notice Request variable bytes from the oracle
*/
function requestBytes() public {
Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this),
this.fulfillBytes.selector);
req.add(
'get',
'https://ipfs.io/ipfs/QmWBxuuuqDxjiSvHsf52bQVkQ7Sqv1dLx4DJPFZ4KdsHGZ/1.json'
);
req.add('path', 'name');
req.add('path', 'image');
sendChainlinkRequest(req, fee);
}
event RequestFulfilled(bytes32 indexed requestId, bytes indexed data, bytes indexed
imgdata);
/**
* @notice Fulfillment function for variable bytes
* @dev This is called by the oracle. recordChainlinkFulfillment must be used.
*/
function fulfillBytes(bytes32 requestId, bytes memory bytesData, bytes memory
imgdata_) public recordChainlinkFulfillment(requestId) {
emit RequestFulfilled(requestId, bytesData, imgdata_);
data = bytesData;
name = string(data);
imgdata = imgdata_;
image = string(imgdata);
}
/**
* Allow withdraw of Link tokens from the contract
*/
function withdrawLink() public onlyOwner {
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
require(link.transfer(msg.sender, link.balanceOf(address(this))), 'Unable to
transfer');
}
}
这是它试图读取的数据: {“名称”:“令牌#1”,“描述”:“ FUL Player nft#1”,“ image”:“ IPFS // QMETFE2HRTFMKZ5WAMY8WAMY8AW2HCDF7BJJJXQPWV3ET1BVQTER1BVQTER/1.JPG”}
chainlink
I've altered the chainlink large Response example to read a JSON file that contains multiple data that I wish to bring into and store in the smart contract
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import '@chainlink/contracts/src/v0.8/ChainlinkClient.sol';
import '@chainlink/contracts/src/v0.8/ConfirmedOwner.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/
*/
contract GenericLargeResponse is ChainlinkClient, ConfirmedOwner {
using Chainlink for Chainlink.Request;
// variable bytes(arbitrary-length raw byte data) returned in a single oracle
response
bytes public data;
bytes public imgdata;
string public image;
string public name;
bytes32 private jobId;
uint256 private fee;
/**
* @notice Initialize the link token and target oracle
* @dev The oracle address must be an Operator contract for multiword response
*
*
* Rinkeby Testnet details:
* Link Token: 0x01BE23585060835E02B77ef475b0Cc51aA1e0709
* Oracle: 0xf3FBB7f3391F62C8fe53f89B41dFC8159EE9653f (Chainlink DevRel)
* jobId: 7da2702f37fd48e5b1b9a5715e3509b6
*
*/
constructor() ConfirmedOwner(msg.sender) {
setChainlinkToken(0x01BE23585060835E02B77ef475b0Cc51aA1e0709);
setChainlinkOracle(0xf3FBB7f3391F62C8fe53f89B41dFC8159EE9653f);
jobId = '7da2702f37fd48e5b1b9a5715e3509b6';
fee = (1 * LINK_DIVISIBILITY) / 10; // 0,1 * 10**18 (Varies by network and job)
}
/**
* @notice Request variable bytes from the oracle
*/
function requestBytes() public {
Chainlink.Request memory req = buildChainlinkRequest(jobId, address(this),
this.fulfillBytes.selector);
req.add(
'get',
'https://ipfs.io/ipfs/QmWBxuuuqDxjiSvHsf52bQVkQ7Sqv1dLx4DJPFZ4KdsHGZ/1.json'
);
req.add('path', 'name');
req.add('path', 'image');
sendChainlinkRequest(req, fee);
}
event RequestFulfilled(bytes32 indexed requestId, bytes indexed data, bytes indexed
imgdata);
/**
* @notice Fulfillment function for variable bytes
* @dev This is called by the oracle. recordChainlinkFulfillment must be used.
*/
function fulfillBytes(bytes32 requestId, bytes memory bytesData, bytes memory
imgdata_) public recordChainlinkFulfillment(requestId) {
emit RequestFulfilled(requestId, bytesData, imgdata_);
data = bytesData;
name = string(data);
imgdata = imgdata_;
image = string(imgdata);
}
/**
* Allow withdraw of Link tokens from the contract
*/
function withdrawLink() public onlyOwner {
LinkTokenInterface link = LinkTokenInterface(chainlinkTokenAddress());
require(link.transfer(msg.sender, link.balanceOf(address(this))), 'Unable to
transfer');
}
}
This is the data its trying to read:
{"name":"Token #1","description":"Ful player NFT #1","image":"ipfs//QmetFE2HRTFMkz5WamY8aw2hCDf7BJjXQPWv3et1bVqtER/1.jpg"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,您在合同中的方法已经足够了,但是我会做出小的更改以匹配一个节点操作员需要添加到其节点以服务您的请求。
通常,如果您有两种不同的路径,但是将它们分配给同一变量,则请求将出错。
最后,您的请求类型
(get> x2字节)
是一个非标准的作业请求。您必须找到一个节点运算符,以托管相关的作业规格在其节点/甲骨文上,以使您的智能合约按预期工作。如果您愿意,如果您愿意,我可以在我们的节点上托管此作业,可以在我们的Discord服务器上给我发消息: https:// https:// discord.gg/pgxrvrdum7
In general your approach in your contract is adequate, but I would make the small change to match a job-spec a node operator would need to add to their node to service your requests.
In general, if you have two different paths, but have them assigned to the same variable, the request will error out.
Finally, your request type
(get > x2 bytes)
is a non-standard job request. You would have to find a node operator to host the associated job-spec on their node/oracle for your smart contract to work as intended.If you want I can host this job on our node if you like, you can message me on our discord server: https://discord.gg/PgxRVrDUm7