0

I am trying to run a method and the return is stuck on promise pending, if i have to do await it still stuck waiting for return. I have no idea why this happen, i log the value of localWeb3 and it returns an object without a problem

There is the snippet:

txChecker = async () => {
const localWeb3 = new Web3(
      ganache.provider({ fork: "wss://eth-mainnet.ws.alchemyapi.io/v2/apikey" })
    )

console.log(localWeb3) // image attached

console.log("latest_block forked 1: ", localWeb3.eth.getBlockNumber()) // promise state pending, value undefined

console.log("latest_block forked 2: ", await localWeb3.eth.getBlockNumber()) // black
}

Why is this happening and how do i resolve this issue?enter image description here

1

I have tried your code as described below (using Infura) and it worked:

const txChecker = async () => {

    const localWeb3 = new Web3(new HDWalletProvider(mnemonic, `wss://rinkeby.infura.io/ws/v3/${infuraKey}`));

    console.log(localWeb3);

    console.log("latest_block forked 1: ", await localWeb3.eth.getBlockNumber()); // Should work 

    console.log("latest_block forked 2: ",  localWeb3.eth.getBlockNumber()); // Shouldn't work
}

txChecker();

Output:

enter image description here

You certainly need the await when you get the block asynchronously; otherwise, you will be retrieving the block number before the promise is resolved.

If you are not getting the block number, there might be an issue with the provider you are using, so perhaps you can try Infura to double check.

UPDATE

Also tested with Ganache provider using the fork parameter and worked correctly with same results:

const Web3 = require('web3');
const ganache = require('ganache-cli');

const txChecker = async () => {

    const localWeb3 = new Web3(ganache.provider({fork: `wss://rinkeby.infura.io/ws/v3/${infuraKey}`}));

    console.log(localWeb3);

    console.log("latest_block forked 1: ", await localWeb3.eth.getBlockNumber()); // Should work 

    console.log("latest_block forked 2: ",  localWeb3.eth.getBlockNumber()); // Shouldn't work
}

txChecker();
4
  • this only happen when using ganache core fork, it is fine using regular one. Can you try the wtih ganache.provider({ fork: "wss://eth-mainnet.ws.alchemyapi.io/v2/apikey" }) or with infura key as fork value.
    – Marr
    Jan 17 at 6:18
  • I have also tried using Ganache provider and fork parameter with Infura and it's all OK, so looks like something related to the alchemy provider you are using. Updated my answer with the additional code. Jan 17 at 6:58
  • it seems to really work when used on node script, this snippet is originally inside a react dapp project. I am not sure if ganache core support dapp?
    – Marr
    Jan 17 at 12:07
  • Unless you manage the security of the wallet with a react app, it's easier to just use web3 = new Web3(window.ethereum) and await window.ethereum.enable() in order to connect to Ethereum through Metamask without the need to define any provider programatically. Jan 17 at 14:57

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.