ethereum / web3.py

A python interface for interacting with the Ethereum blockchain and ecosystem.
http://web3py.readthedocs.io
MIT License
4.95k stars 1.69k forks source link

Web3.eth.signTransaction error missing gasPrice #2091

Closed ebrahimkarimi closed 3 years ago

ebrahimkarimi commented 3 years ago

i'm trying to send a transaction to Rinkeby Test Network with infura's nodes api i have used legacy transaction type with gasPrice but transaction didn't completed then i decided to use EIP-1559 and i saw the documentation and tried it but i got this error.

        web = web3.Web3(web3.HTTPProvider('https://rinkeby.infura.io/v3/MY_API_KEY'))  # Rinkeby Test Network
        nonce = web.eth.get_transaction_count(hdwallet.p2pkh_address()) + 1
        weiPrice = web.toWei(0.5, 'ether')
        toAddress = '0xAf175F66058cd85e65f43F1284416fF747587348'
        gas = 30000
        gasPrice = web.eth.gasPrice
        maxFeePerGas = web.toWei(250, 'gwei')
        maxPriorityFeePerGas = web.toWei(2, 'gwei')
        transaction_params = {
            'to': toAddress,
            'value': weiPrice,
            'gas': gas,
            'maxFeePerGas': maxFeePerGas,
            'maxPriorityFeePerGas': maxPriorityFeePerGas,
            'nonce': nonce
        }
        transaction = web.eth.account.signTransaction(transaction_params, private_key=hdwallet.private_key())

in the last line i get following error

Traceback (most recent call last): File " web3test/main.py", line 56, in transaction = web.eth.account.signTransaction(transaction_params, private_key=private_key) File " $env_address$/lib/python3.8/site-packages/eth_utils/decorators.py", line 18, in _wrapper return self.method(obj, *args, *kwargs) File " $env_address$/lib/python3.8/site-packages/eth_account/account.py", line 603, in signTransaction return self.sign_transaction(transaction_dict, private_key) File " $env_address$/lib/python3.8/site-packages/eth_utils/decorators.py", line 18, in _wrapper return self.method(obj, args, **kwargs) File " $env_address$/lib/python3.8/site-packages/eth_account/account.py", line 667, in sign_transaction ) = sign_transaction_dict(account._key_obj, sanitized_transaction) File " $env_address$/lib/python3.8/site-packages/eth_account/_utils/signing.py", line 28, in sign_transaction_dict unsigned_transaction = serializable_unsigned_transaction_from_dict(transaction_dict) File " $env_address$/lib/python3.8/site-packages/eth_account/_utils/transactions.py", line 43, in serializable_unsigned_transaction_from_dict assert_valid_fields(transaction_dict) File " $env_address$/lib/python3.8/site-packages/eth_account/_utils/transactions.py", line 135, in assert_valid_fields raise TypeError("Transaction must include these fields: %r" % missing_keys) TypeError: Transaction must include these fields: {'gasPrice'}

fselmo commented 3 years ago

hey @ebrahimkarimi, thanks for reaching out. This has been addressed in PR #2065. This has been merged and will be out in a release early next week. In the meantime you can install web3py from the latest master branch with pip install https://github.com/ethereum/web3.py/archive/master.zip if that helps you get past this hump for now.

Keep in mind if you do install from the current master branch, you will need to pass in a type with value '0x2' explicitly in the transaction params to sign_transaction, indicating that you would like to sign using EIP-1559 transaction type.

I'm going to close this since this has already been addressed but thanks for the heads up!

fselmo commented 3 years ago

@ebrahimkarimi I updated my comment above with an easier way of installing from master. Hope this helps.

fselmo commented 3 years ago

@ebrahimkarimi just letting you know that web3.py version 5.22.0 was released this morning with support for signing transactions with EIP-1559 transaction params

maannajjar commented 3 years ago

Hi @fselmo, I'm testing EIP-1559 transaction using version 5.22.0. But after signing the transaction, when I send it using send_transaction, I get the following error: ValueError: {'code': -32000, 'message': 'invalid sender'}

However, if I try to sign and send as legacy transaction (by adding gasPrice and removing maxFeePerGas & maxPriorityFeePerGas), I don't get this error and the transaction is sent successfully. I'm using Ropsten infura node.

Any idea what could be the reason ?

fselmo commented 3 years ago

@maannajjar if you are manually signing a transaction using your private key you will need to send it via send_raw_transaction(), not send_transaction().

Please try to use send_raw_transaction() as in the example in the link above and update here if the problem still persists.

maannajjar commented 3 years ago

@fselmo my bad, I meant to say I'm using send_raw_transaction with my private key. I'm not using send_transaction. It works with signed legacy transaction. But when I do just that one change: replace gasPrice with maxFeePerGas & maxPriorityFeePerGasin sign_transaction (I'm adding type: 2 also). I get that error when calling send_raw_transaction.

fselmo commented 3 years ago

@maannajjar hmm out of curiosity are you providing the chainId when signing as well?

edit: I just gave it a shot without the chainId and got the same message. I'm going to make sure this gets updated in the docs to make it more clear. Please test it out with the appropriate chainId and let us know if that solves your issue.

ebrahimkarimi commented 3 years ago

@maannajjar hmm out of curiosity are you providing the chainId when signing as well?

edit: I just gave it a shot without the chainId and got the same message. I'm going to make sure this gets updated in the docs to make it more clear. Please test it out with the appropriate chainId and let us know if that solves your issue.

Actually missing chainId was my problem as well, it wasn't documented

maannajjar commented 3 years ago

@fselmo you were right, I had the chainId missing. I just added it and it worked. Thanks for the help!