DePayFi / web3-mock

🤡 JavaScript library to mock web3 responses either by emulating web3 wallets or web3 RPC requests.
https://depay.com
MIT License
87 stars 20 forks source link

wait transaction emulation resolve with increaseBlock #16

Closed pabloes closed 2 years ago

pabloes commented 2 years ago

Is there a way that when calling increaseBlock, or any other method, emulates that the block is mined and transaction.wait(1) is immediatelly resolved?

10xSebastian commented 2 years ago

If you use confirm right after mocking the transaction and before interacting with your app/library, it should resolve "immediately":

https://github.com/DePayFi/web3-mock#mock-transaction-confirmations

import { mock, confirm } from '@depay/web3-mock'

let mockedTransaction = mock({
  blockchain: 'ethereum',
  transaction: {
    to: "0x5Af489c8786A018EC4814194dC8048be1007e390",
    value: '1000000000000000000'
  }
})

confirm(mockedTransaction)

await sentTransaction.wait(1).then(function(receipt){
  //... will be executed once confirm is called
})

But some EVM clients like ethers or web3 never "immediately" resolve this due to the async nature of wait().

pabloes commented 2 years ago

I see with wait(1) it resolves immediatelly, but not with any number >1, sorry for not being specific.

In following example, taken from confirmations.spec.js, it takes like 4 seconds to resolve, I need to resolve it immediatelly. So I guess I would need to find a way to emulate the nature of this wait maybe by wrapping it with custom function.

        increaseBlock(12)
        let waitedFor12Confirmations;
        console.time();
        await sentTransaction.wait(12).then(function(receipt){
          transactionReceipt = receipt
          waitedFor12Confirmations = true
        })
        console.timeEnd();
10xSebastian commented 2 years ago

ethersjs does internally try to precompute the approximate time it will take before requesting a block update: https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/base-provider.ts#L1203

Hence you can't speed it up.

10xSebastian commented 2 years ago

Here are the 4 seconds you're waiting for ;)

https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/base-provider.ts#L794

Try to set the provider.pollingInterval of your etherjs: https://github.com/ethers-io/ethers.js/issues/186#issuecomment-393418416

pabloes commented 2 years ago

amazing , thx a lot, provider.pollingInterval = 1; did the trick;