ethereum / web3.py

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

ValueError: {'code': -32000, 'message': 'transaction type not supported'} #2222

Closed mzaprudin closed 2 years ago

mzaprudin commented 2 years ago

What was wrong?

I'm trying to send some BNB from one account to another in Binanse Smart Chain but I get 'transaction type not supported' error message.

from web3 import Web3

REF_ADDRESS = '0x5...'
MY_ADDRESS = '0xe...'
MY_PKEY = 'd...'
MAX_GAS_ETHER = 0.0005

web3 = Web3(Web3.HTTPProvider('https://bsc-dataseed.binance.org/'))

balance = web3.eth.get_balance(MY_ADDRESS)
gas_price = float(web3.fromWei(web3.eth.gas_price, 'ether'))
allowed_gas = int(MAX_GAS_ETHER/gas_price)
print(web3.fromWei(balance, 'ether'))

tx = {
    'from': MY_ADDRESS,
    'to': REF_ADDRESS,
    'value': web3.toWei(0.0001, 'ether'),
    'gas': allowed_gas,
    'type': 2,
    'chainId': web3.eth.chain_id,
    'maxFeePerGas': web3.toWei(250, 'gwei'),
    'maxPriorityFeePerGas': web3.toWei(2, 'gwei'),
    'nonce': web3.eth.get_transaction_count(MY_ADDRESS),
    }

tx_signed = web3.eth.account.sign_transaction(tx, MY_PKEY)
tx_hash = web3.eth.send_raw_transaction(tx_signed.rawTransaction)
tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)

~/miniconda3/lib/python3.9/site-packages/web3/eth.py in send_raw_transaction(self, transaction) 720 721 def send_raw_transaction(self, transaction: Union[HexStr, bytes]) -> HexBytes: --> 722 return self._send_raw_transaction(transaction) 723 724 def sign_munger(

~/miniconda3/lib/python3.9/site-packages/web3/module.py in caller(*args, **kwargs) 55 return LogFilter(eth_module=module, filter_id=err.filter_id) 56 result_formatters, error_formatters, null_result_formatters = response_formatters ---> 57 result = w3.manager.request_blocking(method_str, 58 params, 59 error_formatters,

~/miniconda3/lib/python3.9/site-packages/web3/manager.py in request_blocking(self, method, params, error_formatters, null_result_formatters) 196 """ 197 response = self._make_request(method, params) --> 198 return self.formatted_response(response, 199 params, 200 error_formatters,

~/miniconda3/lib/python3.9/site-packages/web3/manager.py in formatted_response(self, response, params, error_formatters, null_result_formatters) 169 if "error" in response: 170 apply_error_formatters(error_formatters, response) --> 171 raise ValueError(response["error"]) 172 # NULL_RESPONSES includes None, so return False here as the default 173 # so we don't apply the null_result_formatters if there is no 'result' key

ValueError: {'code': -32000, 'message': 'transaction type not supported'}


* I connect HTTP Node.
mzaprudin commented 2 years ago

Well, I solved the issue by changing transaction values from

tx = {
    'from': MY_ADDRESS,
    'to': REF_ADDRESS,
    'value': web3.toWei(0.0001, 'ether'),
    'gas': allowed_gas,
    'type': 2,
    'chainId': web3.eth.chain_id,
    'maxFeePerGas': web3.toWei(250, 'gwei'),
    'maxPriorityFeePerGas': web3.toWei(2, 'gwei'),
    'nonce': web3.eth.get_transaction_count(MY_ADDRESS),
    }

to

tx = {
    'nonce': web3.eth.get_transaction_count(MY_ADDRESS),
    'to': REF_ADDRESS,
    'value': web3.toWei(0.001, 'ether'),
    'gas': allowed_gas,
    'gasPrice': web3.eth.gas_price
}

the documentation says

gasPrice: integer - Integer of the gasPrice used for each paid gas LEGACY - unless you have a good reason to use gasPrice, use maxFeePerGas and maxPriorityFeePerGas instead

and the provided example doesn't use gasPrice value

>>> signed_txn = w3.eth.account.sign_transaction(dict(
    nonce=w3.eth.get_transaction_count(public_address_of_senders_account),
    maxFeePerGas=3000000000,
    maxPriorityFeePerGas=2000000000,
    gas=100000,
    to='0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
    value=12345,
    data=b'',
    type=2,  # (optional) the type is now implicitly set based on appropriate transaction params
    chainId=1,
  ),
  private_key_for_senders_account,
)
>>> w3.eth.send_raw_transaction(signed_txn.rawTransaction)
HexBytes('0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331')

But it doesn't work in this manner (

fselmo commented 2 years ago

@mzaprudin Thanks for reaching out. It's important to note that the module these methods exist within is the eth module. This is going to be solely driven by and expected to support Ethereum clients. Ethereum recently introduced the London hard fork and EIP-1559 was one of these changes. EIP-1559 introduced support for a type=2 transaction where the max fee fields replace the gas price field. Not all chains will have this support. I suspect most won't.

It looks like you are connecting to BSC which does not support a type=2 transaction that I am aware of. So using gasPrice instead of the EIP-1559 max fees is the way to go here. I'm glad you were able to resolve this. I'm going to close this ticket but if you find there is still a relevant error here please feel free to re-open.

061official commented 2 years ago

@mzaprudin Thanks for reaching out. It's important to note that the module these methods exist within is the eth module. This is going to be solely driven by and expected to support Ethereum clients. Ethereum recently introduced the London hard fork and EIP-1559 was one of these changes. EIP-1559 introduced support for a type=2 transaction where the max fee fields replace the gas price field. Not all chains will have this support. I suspect most won't.

It looks like you are connecting to BSC which does not support a type=2 transaction that I am aware of. So using gasPrice instead of the EIP-1559 max fees is the way to go here. I'm glad you were able to resolve this. I'm going to close this ticket but if you find there is still a relevant error here please feel free to re-open.

@mzaprudin @fselmo first of all thanks for this thread. I have faced the same issue as mzaprudin for BSC testenet and his solution worked for me as well.

@fselmo may i propose to include your response as a note at https://web3py.readthedocs.io/en/stable/web3.eth.html#web3.eth.Eth.send_raw_transaction as this is highly useful information.

fselmo commented 2 years ago

Hey @061official. This is good feedback.I think it's pretty explicit that the eth module should not support bsc. We are working on making it easier to import external modules into the web3 library so I think this can pave the way for others creating their own bsc module to be imported. But this repo does live within the Ethereum repositories as well.

We made a note to add a bit better messaging in general to the docs, rather than to the eth_sendRawTransaction method docs, so that we can make this a bit more clear to users of the library. Thanks for the feedback.