Web3 Python – How to Fix ValueError for Unsupported Transaction Type

binance-smart-chainpythonsendrawtransaction

I'm trying to send a signed transaction from an interaction with a smart contract but it all falls apart when sending the raw transaction.

My code is as follows

    import Globalvariables as thg
    bsc='https://bsc-dataseed.binance.org/'
    w3=Web3(Web3.HTTPProvider(bsc))
    w3.middleware_onion.inject(geth_poa_middleware, layer=0)

    abi=json.loads(abi_string)

    contract_object=w3.eth.contract(address=w3.toChecksumAddress(thg.direction_market),abi=abi)

    nonce=w3.eth.getTransactionCount(w3.toChecksumAddress(thg.my_direction))

    #tx={'nonce':nonce,'from':w3.toChecksumAddress(thg.my_direction),'gasPrice':50000000000,'gas':277777}
    tx={'nonce':nonce,'from':w3.toChecksumAddress(thg.my_direction),'maxFeePerGas':277777,'maxPriorityFeePerGas':30000}

    input_data={'addresses':[w3.toChecksumAddress(token['owner']),w3.toChecksumAddress(token['nftContract']),w3.toChecksumAddress(thg.contract_wbnb)],
                'values':[int(token['tokenId']),int(w3.toWei(token['bnb'],'ether')),int(token['saltNonce'])],
                'signature':bytes.fromhex(token['sign'].lstrip('0x'))
                }                               
    purchase=contract_object.functions.matchTransaction(input_data['addresses'],input_data['values'],input_data['signature']).buildTransaction(tx)

    sign_tx=w3.eth.account.sign_transaction(purchase,thg.metamask)
    print(sign_tx,type(sign_tx))
    print(sign_tx.rawTransaction,type(sign_tx.rawTransaction))

    try:raw_final=w3.eth.send_raw_transaction(sign_tx.rawTransaction)
    except:raw_final=w3.eth.send_raw_transaction(sign_tx)

The codes gives back an error in both of the last 2 lines

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

Why am I receiving this?

In the docs I think I followed the code without any mistake. But, looking closer whereas in the docs when you call sign_tx.rawTransaction it returns something like this

>>> signed_txn.rawTransaction
HexBytes('0x02f8b00180843b9aca0084773594008301117094fb6916095ca1df60bb79ce92ce3ea74c37c5d35980b844a9059cbb000000000000000000000000fb6916095ca1df60bb79ce92ce3ea74c37c5d3590000000000000000000000000000000000000000000000000000000000000001c001a0cec4150e52898cf1295cc4020ac0316cbf186071e7cdc5ec44eeb7cdda05afa2a06b0b3a09c7fb0112123c0bef1fd6334853a9dcf3cb5bab3ccd1f5baae926d449')

What I see from my code is this

print(signed_txn.rawTransaction,type(signed_txn.rawTransaction))
b'\x02\xf9\x01\xce8"\x82u0\x83.....', <class 'hexbytes.main.HexBytes'>

Why is my code returning something different?

By the way, in the previous step when signing the transaction I've noticed that in the example they use the private key like this

private_key = b"\xb2\\}\xb3\x1f\xee\xd9\x12''\xbf\t9\xdcv\x9a\x96VK-\xe4\xc4rm\x03[6\xec\xf

Whereas mine is passed as a string. Do I need to do bytes.fromhex(thg.metamask) instead?
Is this the root of the mistake or is it something different all together?

Best Answer

I think your error is not related to hex/binary conversions as this is mostly handled by web3.

You can easily view the hex version in python by using .hex()

h = '935479cfcb8d9b53861f4c6c5e8941ed2787710b289100235dcb6ca1182a2f24'
b = b"\x93Ty\xcf\xcb\x8d\x9bS\x86\x1fLl^\x89A\xed'\x87q\x0b(\x91\x00#]\xcbl\xa1\x18*/$"
print(b.hex())
>>> 935479cfcb8d9b53861f4c6c5e8941ed2787710b289100235dcb6ca1182a2f24
print(b.hex() == h)
>>> True

The actual error comes from poor formatting somewhere in your code.

Possibly from ,'maxFeePerGas':277777,'maxPriorityFeePerGas':30000 as these are mainly for Ethereum and can fail if I include them on other chains.

You should include gas and gasPrice

If you still have issue, change gas to gasLimit

You also appear to be missing the chainId from the transaction which will default to ETH

make sure to add that as well..

chain_id_binance = 56 # 0x38 in hex

tx = {
chainId: w3.utils.toHex(chain_id_binance),
...
}
Related Topic