示例 APIConsumer 合约未在 Hardhat 中接收到 ChainlinkFulfilled 事件

Sample APIConsumer contract not receiving ChainlinkFulfilled event in Hardhat

我部署了API消费者示例合约(https://docs.chain.link/docs/make-a-http-get-request/) from Remix on Kovan, funded it with LINK, and sent a requestVolumeData transaction from Remix. As can be seen on Etherscan, the ChainlinkFulfilled event was fired: https://kovan.etherscan.io/address/0xde79d39db6b5210f83acf57346d74e5c762ab895#events

然后我使用 Kovan 叉子(使用 Alchemy API 密钥)在 Hardhat 中附加了完全相同的合同,但它从未收到事件。这是我的测试脚本:

const { ethers } = require("hardhat");

describe("API", function() {
  it("Should make request and get result", async function() {
    const ERC20 = await ethers.getContractFactory("ERC20")
    const link = await ERC20.attach("0xa36085F69e2889c224210F603D836748e7dC0088"); //Kovan
    console.log("name=" + await link.name());

    const APIConsumer = await ethers.getContractFactory("APIConsumer");
    const apiConsumer = APIConsumer.attach("0xde79d39db6B5210F83acf57346d74E5C762AB895");

    //Ensure contract has sufficient LINK
    console.log("balanceOf=" + await link.balanceOf(apiConsumer.address));

    //Make request
    const transaction = await apiConsumer.requestVolumeData()
    const tx_receipt = await transaction.wait()
    const requestId = tx_receipt.events[0].topics[1]
    console.log("requestId=%s", requestId)

    //Optionally subscribe to events
    // apiConsumer.on("ChainlinkFulfilled", id => {
    //  console.log("ChainlinkFulfilled: %s", id);
    // })

    //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: %s", result)
  });
});

在 hardhat.config.js 中,blockNumber 被选择在第一个 ChainlinkRequested 事件之前,我们可以从脚本输出中看到合约有足够的资金 LINK.

require("@nomiclabs/hardhat-waffle");

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: "0.8.7",
  networks: {
    hardhat: {
      forking: {
        url: "https://eth-kovan.alchemyapi.io/v2/<API Key>",
        blockNumber: 31301193
      }
    }
  },
};

脚本输出

  API
name=ChainLink Token
balanceOf=1000000000000000000
requestId=0x352988e0ddfe5c4349711ed9787069b1ea55bae562f676a08f6103435a874514
API Consumer Volume: 0

为什么 ChainlinkFulfilled 似乎没有被 Hardhat 测试解雇?

欢迎您,彼得!您的分叉网络上没有 oracle 侦听。要在本地测试预言机,您应该考虑 mocking the oracle (hardhat way).