Web3.js: Web3 options are not set in the submodules on initiating of the library.

Created on 26 Mar 2019  路  20Comments  路  Source: ChainSafe/web3.js

Description

When I am deploying smart contract using web3 function deploy, it doesn't trigger the event .on('receipt', (receipt)).

Expected behavior

```
myContract.deploy({
data: '0x' + bytecode,
arguments: []
}).send({
from: walletAddress,
gas: 21000
}).on('receipt', (receipt) => {
console.log(receipt.contractAddress)
};

Should return contract address.


#### Actual behavior
Calling the above function doesn't return contractAddress and doesn't compile .on('receipt). It got stuck.

#### Steps to reproduce the behavior

1. Import web3 in nodejs code

1. Contract deploy using web3 (myContract.deploy())

1. Add an event .on('receipt') with web3.deploy.

#### Code Block:
   ```
let myContract = new web3.eth.Contract(abi);

   myContract.deploy({
         data: '0x' + bytecode,
         arguments: []
    }).send({
          from: walletAddress,
          gas: 21000
    }).on('receipt', (receipt) => {
          console.log(receipt.contractAddress)
    };

Versions

  • web3.js: 1.0.0-beta.50
  • nodejs: 10.13.0
  • browser: Chrome, Firefox
  • ethereum node: 1.8.23-stable
documentation

Most helpful comment

@princesinha19 @szerintedmi The missing emitted receipt event got fixed with #2616 and will be released this week.

All 20 comments

Did you configure the transactionConfirmationsBlock option of the Eth module?

@nivida Thanks for your response.
Yes, I have configured in options. I am running a private geth node with no peers.

const options = {
    transactionConfirmationBlocks: 0,
    transactionPollingTimeout: 5
}

if (typeof web3 !== "undefined") {
    web3 = new Web3(web3.currentProvider, options);
} else {
    // set the provider you want from Web3.providers

    web3 = new Web3(
        new Web3.providers.WebsocketProvider("http://127.0.0.1:8546", options)
    );
}

If I check the transaction receipt using geth command eth.getTransactionReceipt('0x5ac7d8eec4bd18fcb85c68ffa33dc8f1731c043f361754d68911df61928b19e8'), Then I get a receipt.
geth log

The correct configuration would be:

const options = {
    transactionConfirmationBlocks: 1,
    transactionBlockTimeout: 5
}

The transactionConfirmationBlock property has to be one because it can't find the transaction receipt if no block got mined. If you use the MetamaskInpageProvider then you should define the transactionBlockTimeout property because it's a socket based provider. The transactionPollingTimeout is for legacy providers and the internal HttpProvider of Web3.js.

@nivida I did the configuration suggested by you but it doesn't work for me.

The correct configuration would be:

const options = {
transactionConfirmationBlocks: 1,
transactionBlockTimeout: 5
}

do I need to do any configuration in my etherum (geth) node too?

const options = {
    transactionConfirmationBlocks: 1
    // transactionBlockTimeout: 5
}

if (typeof web3 !== "undefined") {
    web3 = new Web3(web3.currentProvider, options);
} else {
    // set the provider you want from Web3.providers
    web3 = new Web3(
        new Web3.providers.WebsocketProvider("http://127.0.0.1:8546", options)
    );
}

Although., The options is not working for me as in websocket provider:
options

web3.transactionConfirmationBlocks = 1;
This worked for me. you can see the log.

options log

Thanks for additional information! I'll fix and release it asap.

I've just seen that the documentation is inconsistent for the construction of a Web3 module. I will update the documentation to the following format: new Web3(provider, net?, options?);

I've checked the code from the old architecture and it was not possible to pass options over the constructor because the second parameter was the net.Socket dependency.

I apologize for the confusion and the stolen time.

@nivida okay no problem. Thanks for your response.
I don't know what's the problem with receipt generation at the time of contract deployment, when calling .on('receipt') event.
Although I am able to set the options using web3.transactionConfirmationBlocks = 1
That also need to be researched.

You're able to set the options just change your code to:

new Web3(web3.currentProvider, null, options);

Thanks, @nivida.
It worked for setting options. But still not getting the receipt at the time of contract deployment.

@nivida Now, .on('receipt') is working. But I am only getting the tree structure on calling .on('receipt) event, I doesn't get actual transaction receipt which consists of contractAddress, transactionHash, etc.

Code Block

myContract.deploy({ data: '0x' + bytecode, arguments: [] }).send({ from: walletAddress, gas: 21000 }).on('receipt', (receipt) => { console.log(receipt.contractAddress) };

Log of transaction receipt

receipt log

I'm struggling with tx events using web3js beta 51 too.
Using local ganache v6.4.1 (ganache-core: 2.5.3).

  • When I connect via websocket I receive transactionHash and confirmation events but never receipt and the tx.send() promise never resolves.

  • When I connect via http then I don't even receive the confirmation events.

Both cases the transactionConfirmationBlocks set to 1.
These all work (almost) fine with beta 36.

It might be related: the confirmation events were not received in beta36 with websocket on local ganache only when I issued an "empty" txs to ganache: web3.eth.sendTransaction({from: web3.eth.accounts[0] }). Each blank tx generated one confirmation event.
With http connection the confirmation events were triggering fine with beta36.

@szerintedmi Did you configured the transactionConfirmationBlocks option?
As it is working for me with Ganache in version 1.0.0-beta.50. But doesn't with Geth.

@szerintedmi Did you configured the transactionConfirmationsBlock option?
As it is working for me with Ganache in version 1.0.0-beta.50. But doesn't with Geth.

Yes, in provider options. I also crossed checked with console.log(web3.transactionConfirmationBlocks)
(I assume you meant transactionConfirmationBlocks and not transactionConfirmationsBlock )

I'm writing a quick test to reproduce the issue, will keep it posted

@princesinha19 @szerintedmi The missing emitted receipt event got fixed with #2616 and will be released this week.

thanks, @nivida.

@nivida : It seems beta52 didn't resolve the issue, at least mine. It's still not resolving on ganache. I created a separate ticket because I'm unsure if it's related to this one: #2652

Thanks, @nivida. Yes, I can see the updated docs.
Also, at the time of deployment, I am getting the receipt when using .on('receipt) event. So, that is also fixed.
Thanks again.

Was this page helpful?
0 / 5 - 0 ratings