0

I'm new to Javascript and Etherjs. I'm trying to interact with a specific contract in the following way. The contract has two functions, hi and bye. I want to first run contract.hi();, wait N blocks while printing the number of each block to the console, then afterwards run contract.bye();. I'm using AlchemyAPI to connect to the testnet. I'm guessing something like:

it("Greeting", async function() {
   await contract.hi();
   
   for(let i = 0; i < N; i++){
      \\What to put here?
      console.log(await ethers.provider.getBlockNumber());
   }

   await contract.bye();
});
0

I'd create a promise that subscribe to the 'block' events and waits for the correct number of blocks. Read the documentation Event Emitter Methods.

A pseudo code would be like:

await new Promise((resolve, reject) => {
  provider.on("block", (blockNumber) => {
    if (blockNumber == desiredBlcok) {
      resolve();
    }
  })
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.