1

我正在尝试在 kovan fork 网络上使用安全帽按照文档步骤测试 Chainlink 警报预言机。

https://docs.chain.link/docs/chainlink-alarm-clock/#:~:text=ala

当我用它测试合同时,npx hardhat test它看起来像向 oracle 发送请求函数,但从未回调过fulfill_alarm()。

我想知道这是否可能是 fork 网络或我的测试代码的问题。

我正在使用 Alchemy api 作为 kovan 上 fork 网络的 url。

另外,我正在使用 mocha 超时,因此它可以等待回调执行,但永远不会命中。

第一个 it 语句正确执行,但是在第二个它应该在彩票完成后增加 lotteryId + 1 (调用 fulfill_alarm() )没有通过。看起来该函数永远不会被chainlink oracle执行。

甲骨文:0xAA1DC356dc4B18f30C347798FD5379F3D77ABC5b

工作编号:'982105d690504c5d9ce374d040c08654'

坚固性代码:

    //Starting Oracle Alarm
  function start_new_lottery(uint256 duration) public {
      require(lottery_state == LOTTERY_STATE.CLOSED, "can't start a n
      lottery_state = LOTTERY_STATE.OPEN;
      Chainlink.Request memory req = buildChainlinkRequest(jobId, add
      req.addUint("until", block.timestamp + duration);
      sendChainlinkRequestTo(oracle, req, oraclePayment);
       console.log('LINK REQ SEND');
   }

     //Callback Function after Oracle Alarm is Fulfilled
   function fulfill_alarm(bytes32 _requestId) public recordChainlinkFu
       require(lottery_state == LOTTERY_STATE.OPEN, "The lottery hasn'
       lotteryId = lotteryId + 1;
       console.log('ALARM_FULFILLED');
       pickWinner();
  }

测试代码:

const {expect} = require("chai");

const { time } = require("@openzeppelin/test-helpers");

describe("彩票合约", function() {

   //Defining global variables for testing.
   const price_lottery = ethers.BigNumber.from(1);
   let owner;
   let Lottery;
   let lottery;

  before(async function () {
      Lottery = await ethers.getContractFactory("Lottery");
      [owner] = await ethers.getSigners();
      lottery = await Lottery.deploy(
          price_lottery,
      );

      //Starting new Lottery
      await lottery.start_new_lottery(60);

  });

   it("should start a chainlink alarm to init new lottery", async func

       let lottery_state = await lottery.lottery_state();

       expect(lottery_state).to.equal(0);

       //Initial value
       let lotteryId = await lottery.lotteryId();
      expect(lotteryId).to.equal(1);
  })

  it("increments lotteryID + 1, when chainlink alarm is fulfilled aft

      //Wait until alarm is fulfilled
      function timeout(ms) {
           return new Promise(resolve => setTimeout(resolve, ms));
       }

       await timeout(120000);

       //Expected value
       let lotteryId = await lottery.lotteryId();
       expect(lotteryId).to.equal(2);
   })

})

4

0 回答 0