[英]Testing Chainlink API calls using Hardhat
I am having trouble testing Chainlink API calls locally.我在本地测试 Chainlink API 调用时遇到问题。 I have a contract which calls my API consumer contract to make an API call.我有一个调用我的 API 消费者合约来进行 API 调用的合约。 However, when I try to do that, the MockOracle (mocked oracle for local testing, from chainlink) contract reverts (VM exception) saying "Must use whitelisted functions".但是,当我尝试这样做时,MockOracle(用于本地测试的模拟 oracle,来自 chainlink)合同恢复(VM 异常)说“必须使用列入白名单的函数”。 I am making an API call for a bytes32 get request.我正在为 bytes32 get 请求进行 API 调用。
This is the function modifier (inside of LinkTokenReceiver) where the MockOracle reverts (it gets called by the MockOracle when receiving LINK tokens):这是 MockOracle 恢复的函数修饰符(在 LinkTokenReceiver 内部)(它在接收 LINK 令牌时被 MockOracle 调用):
/**
* @dev Reverts if the given data does not begin with the `oracleRequest` function selector
* @param _data The data payload of the request
*/
modifier permittedFunctionsForLINK(bytes memory _data) {
bytes4 funcSelector;
assembly {
// solhint-disable-next-line avoid-low-level-calls
funcSelector := mload(add(_data, 32))
}
require(funcSelector == ORACLE_REQUEST_SELECTOR, "Must use whitelisted functions");
_;
}
The ApiConsumer is properly funded, and the LINK token is a local deployment of the chainlink Token. ApiConsumer 资金充足,LINK 代币是 chainlink 代币的本地部署。 So, what am I doing wrong?那么,我做错了什么? Or what is the best way to test Chainlink API calls (bytes32 get request) locally using hardhat?或者使用安全帽在本地测试 Chainlink API 调用(bytes32 获取请求)的最佳方法是什么?
In Chainlink oracle workflow, consumers are basically supposed to send a request to the off-chain oracle node so that the oracle node can fetch data from the API specified by consumers.在 Chainlink 预言机工作流程中,消费者基本上应该向链下预言机节点发送请求,以便预言机节点可以从消费者指定的 API 中获取数据。 In the workflow, request sent by user must be caught by off-chain oracle node.在工作流程中,用户发送的请求必须被链下预言机节点捕获。
What you are doing here is to send your request to smart contract oracle
and the oracle
save your request into event log oracleRequest
so that it could be caught by off-chain oracle.你在这里做的是将你的请求发送到智能合约oracle
,然后oracle
将你的请求保存到事件日志oracleRequest
中,以便它可以被链下 oracle 捕获。
Your problem here is as suggested by modifier permittedFunctionForLINK
, the function selector is not oracleRequest
so that off-chain oracle cannot recognize the request.您的问题正如修饰符permittedFunctionForLINK
所建议的那样,函数选择器不是oracleRequest
,因此链下 oracle 无法识别该请求。
I am guessing you are currently using the oracle.sol
and ChainlinkClient.sol
in version v0.6.我猜您目前使用的是 v0.6 版的oracle.sol
和ChainlinkClient.sol
。 In version v0.6, AFAIK, ChainlinkClient doesn't not help users to add function selector oracleRequest
in the users' Chainlink request, which means you will get the error if you doesn't manually add the function selector in your request.在 v0.6 版本中,据我所知,ChainlinkClient 不会帮助用户在用户的 Chainlink 请求中添加函数选择器oracleRequest
,这意味着如果您不在请求中手动添加函数选择器,将会出现错误。 In v0.7, ChainlinkClient helps you add the selector in the request.在 v0.7 中,ChainlinkClient 帮助您在请求中添加选择器。 Just check the function sendChainlinkRequestTo
in ChainlinkClient v0.6 and ChainlinkClient v0.7 by yourself.自己查看ChainlinkClient v0.6和ChainlinkClient v0.7 sendChainlinkRequestTo
的sendChainlinkRequestTo函数即可。
Solutions:解决方案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.