1

There are so many gas paramaters in web3.js. How can I send a transaction with a custom gas fee in gwei?

1 Answer 1

0

The main parameters in EIP-1559 are maxFeePerGas and maxPriorityFeePerGas. Explaining gas and EIP-1559 is not in scope for this question but for example see Max Priority Fee and Max Gas Fee difference and definition

In web3.js sendTransaction you would specify type: 0x2 (an EIP-1559 transaction).

The base unit in the Ethereum protocol is wei (not Ether or gwei), so units in web3.js will need the correct number of zeros. Use web3.utils.toWei(number [, unit]) to help you.

For example, web3.utils.toWei('1.5', 'gwei').

Here is a web3.js v1.8 test for sendTransaction:

        var receipt = await web3.eth.sendTransaction({
            to: accounts[1],
            from: accounts[0],
            nonce: nonceVal,
            value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')),
            gas: web3.utils.toHex(21000),
            maxFeePerGas: '0x59682F00', // 1.5 Gwei
            maxPriorityFeePerGas: '0x1DCD6500', // .5 Gwei
            type: '0x2'
        })

The permalink of the last 3 lines https://github.com/web3/web3.js/blob/59b657b8c02af05575dc2d0ed321fa7d3aa8eaf6/test/e2e.method.send.js#L53-L55 shows:

            maxFeePerGas: '0x59682F00', // 1.5 Gwei
            maxPriorityFeePerGas: '0x1DCD6500', // .5 Gwei
            type: '0x2'

The test uses hardcoded hex values; you'll probably want to use web3.utils.toWei. It may also be desirable to convert the decimal wei, to hexadecimal using web3.utils.toHex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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