0

I see that with sendTransaction you can specify maxFeePerGas.

see here

However, if you use signTransaction and then sendSignedTransaction you can only send gas. and here.

How to send a signed transaction and specifying gas? (with max priority fee, akin to metamask) ?

4

2 Answers 2

1
+50

The documentation is not up-to-date, you can use maxPriorityFeePerGas and maxFeePerGas without problem.

gas is the maximum amount of gas you are willing to spend for the tx, and it must be used for every type of transaction: type 0 (legacy), 1 (using access list) and 2 (EIP-1559).

You can find the complete definition of transaction for web3.js in the repo, and below for your convenience:

export interface TransactionConfig {
    from?: string | number;
    to?: string;
    value?: number | string | BN;
    gas?: number | string;
    gasPrice?: number | string | BN;
    maxPriorityFeePerGas?: number | string | BN;
    maxFeePerGas?: number | string | BN;
    data?: string;
    nonce?: number;
    chainId?: number;
    common?: Common;
    chain?: string;
    hardfork?: string;
}

If maxPriorityFeePerGas and maxFeePerGas are set, and the gasPrice parameter is set, they will be both set equal to gasPrice.

See the test cases in the official repo for many examples of use, like the one below. Search for postEip1559Block to see examples that contain maxPriorityFeePerGas and maxFeePerGas.

it("signTransaction must compare to eth_signTransaction", function(done) {
    var provider = new FakeHttpProvider();
    var web3 = new Web3(provider);

    provider.injectResult(
        test.transaction.common.hardfork === 'london' ?
        postEip1559Block:
        preEip1559Block
    );
    provider.injectValidation(function (payload) {
        assert.equal(payload.jsonrpc, '2.0');
        assert.equal(payload.method, 'eth_getBlockByNumber');
        assert.deepEqual(payload.params, ['latest', false]);
    });
    provider.injectResult('0x5022');
    provider.injectValidation(function (payload) {
        assert.equal(payload.jsonrpc, '2.0');
        assert.equal(payload.method, 'eth_gasPrice');
        assert.deepEqual(payload.params, []);
    });

    var ethAccounts = new Accounts(web3);

    var testAccount = ethAccounts.privateKeyToAccount(test.privateKey);
    assert.equal(testAccount.address, test.address);

    testAccount.signTransaction(test.transaction).then(function (tx) {
        assert.equal(tx.messageHash, test.messageHash, "message hash failed");
        assert.equal(tx.transactionHash, test.transactionHash, "tx hash failed");
        assert.equal(tx.rawTransaction, test.rawTransaction, "rawtx failed");
        done();
    })
    .catch(e => {
        console.log(i, e)
        done(e);
    });
});
1
  • Thank you ! Genius, should have looked into the source code instead of relying on documentation
    – Sequoya
    Jun 10, 2022 at 20:22
0

The sendSignedTransaction function does not receive a gasPrice nor gas, there are the parameters -

web3.eth.sendSignedTransaction(signedTransactionData [, callback])
  1. String - Signed transaction data in HEX format
  2. Function - (optional) Optional callback, returns an error object as first parameter and the result as second.

So you can specify maxFeePerGas in signTransaction and send using sendSignedTransaction.

var Tx = require('@ethereumjs/tx').Transaction;
var privateKey = Buffer.from('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex');

var rawTx = {
  nonce: '0x00',
  maxFeePerGas: '0x09184e72a000',
  maxPriorityFeePerGas: '0x1C9C380'
  gasLimit: '0x2710',
  to: '0x0000000000000000000000000000000000000000',
  value: '0x00',
  data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
}

var tx = new Tx(rawTx, {'chain':'ropsten'});
tx.sign(privateKey);

var serializedTx = tx.serialize();

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('receipt', console.log);
3

Your Answer

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

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