request - 测试已完成的 chainlink 预言机请求以太/安全帽的最佳实践是什么?

标签 request ethereum chainlink ethers.js hardhat

我正在 rinkeby 上使用带有以太币的安全帽来测试向本地 chainlink 节点发出 get 请求的智能合约。我可以在节点仪表板上观察到请求已得到满足。

我正在努力编写一个等待第二个履行交易得到确认的测试。

我在 SmartContractKit/chainlink repo tests 中看到类似的测试

 it("logs the data given to it by the oracle", async () => {
  const tx = await oc.connect(roles.oracleNode).fulfillOracleRequest(...convertFufillParams(request, response));
  const receipt = await tx.wait();

  assert.equal(2, receipt?.logs?.length);
  const log = receipt?.logs?.[1];

  assert.equal(log?.topics[2], response);
});

我根本看不到这会等待已完成的交易。在这个函数调用的consumer.sol中有一个事件RequestFulfilled,即emit,但是这个测试似乎没有监听它。

我发现的另一个例子,ocean protocol request test ,通过创建请求 id 的映射、访问器和测试轮询中的 while 循环直到找到请求 id 来实现此目的。

 it("create a request and send to Chainlink", async () => {
  let tx = await ocean.createRequest(jobId, url, path, times);
  request = h.decodeRunRequest(tx.receipt.rawLogs[3]);
  console.log("request has been sent. request id :=" + request.id)

  let data = 0
  let timer = 0
  while(data == 0){
    data = await ocean.getRequestResult(request.id)
    if(data != 0) {
      console.log("Request is fulfilled. data := " + data)
    }
    wait(1000)
    timer = timer + 1
    console.log("waiting for " + timer + " second")
  }

});

这是有道理的,我明白它是如何工作的。但是,当我认为必须有更优化的方法时,我想避免创建映射和访问器。

最佳答案

您想查看hardhat-starter-kit查看使用 Chainlink/oracle API 响应的示例。

对于单元测试,您只需 mock来自 Chainlink 节点的 API 响应。

对于集成测试(例如,在测试网上),您需要添加一些等待参数来返回。在示例 Hardhat-starter-kit 中,它只等待 x 秒,但您也可以编写测试代码来监听事件,以了解预言机何时响应。这确实使用事件来获取 requestId,但是,您实际上不必自己创建事件,因为 Chainlink 核心代码已经有了这个。

    it('Should successfully make an external API request and get a result', async () => {
      const transaction = await apiConsumer.requestVolumeData()
      const tx_receipt = await transaction.wait()
      const requestId = tx_receipt.events[0].topics[1]

      //wait 30 secs for oracle to callback
      await new Promise(resolve => setTimeout(resolve, 30000))

      //Now check the result
      const result = await apiConsumer.volume()
      console.log("API Consumer Volume: ", new web3.utils.BN(result._hex).toString())
      expect(new web3.utils.BN(result._hex)).to.be.a.bignumber.that.is.greaterThan(new web3.utils.BN(0))
    })

关于request - 测试已完成的 chainlink 预言机请求以太/安全帽的最佳实践是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68883111/

相关文章:

chainlink - 创建应用程序 : failed to initialize ORM

node.js - 解码特殊西里尔字母

无法使用 lwIP 原始 TCP 连接接收对 HTTP 请求的多个响应

amazon-web-services - 如何连接到使用 AWS 区 block 链模板创建的以太坊 URL

cryptography - 在智能合约中隐藏部分结果

solidity - 链环 VRF : Error encoding arguments: Error: invalid BigNumber string

model - Chainlink新手疑惑

php - 如何从 php 到 Android 通信

javascript - JS、多个JSON请求和回调函数

java - Web3j 读取单个事务的所有发出事件