简体   繁体   English

使用 Hardhat 测试 Chainlink API 调用

[英]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.solChainlinkClient.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 sendChainlinkRequestTosendChainlinkRequestTo函数即可。

Solutions:解决方案:

  1. Use the v0.7 ChainlinkClient and operator.sol (smart contract oracle.sol is renamed as operator.sol in v0.7) for your test.使用 v0.7 ChainlinkClient 和operator.sol (智能合约 oracle.sol 在 v0.7 中更名为 operator.sol)进行测试。 The document can be found here .该文件可以在这里找到。
  2. There is a repo called hardhat-starter-kit in Chainlink official github, there are sample codes you can try. Chainlink 官方 github 中有一个名为hardhat-starter-kit的 repo,里面有示例代码你可以试试。 Or you can directly use starterkit to test AnyApi and any other services.或者您可以直接使用 starterkit 来测试 AnyApi 和任何其他服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有比通过节点单独调用多个 API 更简单的方法从 Chainlink 获取去中心化数据? - Is there a simpler way to get decentralized data from Chainlink than doing separate multiple API calls through nodes? 使用安全帽时华夫饼未定义 - waffle undefined when using hardhat 有没有一种安全的方法可以将 API 密钥传递给 Chainlink 中的 oracle? - Is there a secure way to pass API keys to an oracle in Chainlink? 在使用 HardHat 进行测试时,来自 getSigners 的地址未获得 eth 资助 - Addresses from getSigners not funded with eth while testing with HardHat Hardhat waffle 本地智能合约测试:没有足够的以太币来铸造 NFT - Hardhat waffle local smart contracts testing: Not enough ether to mint NFT 尽管有官方测试文档,但通过 Hardhat 导入以太币失败 - Importing ethers via Hardhat fails despite official testing documentation 使用安全帽编译智能合约时出错 - Errror compiling smart contract using hardhat 使用 Hardhat WITHOUT 脚本部署智能合约 - Deploying a smart contract using Hardhat WITHOUT script ChainLink 任何 API 大响应示例不满足 - ChainLink Any API Large Response Example doesnt fulfill 无法使用 ethers.js 切换签名者以使用 Hardhat 进行合同交互 - Cannot switch signers with ethers.js for a contract interaction using Hardhat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM