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()
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
andgasPrice
If you still have issue, change
gas
togasLimit
You also appear to be missing the
chainId
from the transaction which will default toETH
make sure to add that as well..