1

I fetched a mempool tx

AttributeDict({'blockHash': HexBytes('0x92911a95b4d4b07bb13552bfb29cc43bf43eb7d63af7c968ddaa3df9a6f22988'), 'blockNumber': 17964189, 'from': '0xfd00479c712370C84B398CB8951aebC8039c3024', 'gas': 64744, 'gasPrice': 74341019612, 'maxFeePerGas': 74341019612, 'maxPriorityFeePerGas': 74341019612, 'hash': HexBytes('0x6292090633c01105ec72f4ec9cceb6d7f96321074b4faca9ad394c3a35add749'), 'input': HexBytes('0xa9059cbb00000000000000000000000061b7b515c1ec603cf21463bcac992b60fd610ca900000000000000000000000000000000000000000000002dbf877cf6ec677800'), 'nonce': 38, 'to': '0xaea46A60368A7bD060eec7DF8CBa43b7EF41Ad85', 'transactionIndex': 0, 'value': 0, 'type': 2, 'accessList': [], 'chainId': 1, 'v': 0, 'r': HexBytes('0x8ee28a85ac42174b9e10c49613c0cddcf5d5a5ecb90bd516f81b45a957a64fe2'), 's': HexBytes('0x05349c1076cc83990f425773d6b5995474782f1fccf1b2e43529ac54ac6ae144'), 'yParity': '0x0'})

Or, for more clarity:

'blockHash': HexBytes('0x92911a95b4d4b07bb13552bfb29cc43bf43eb7d63af7c968ddaa3df9a6f22988')
'blockNumber': 17964189
'from': '0xfd00479c712370C84B398CB8951aebC8039c3024'
'gas': 64744
'gasPrice': 74341019612
'maxFeePerGas': 74341019612
'maxPriorityFeePerGas': 74341019612
'hash': HexBytes('0x6292090633c01105ec72f4ec9cceb6d7f96321074b4faca9ad394c3a35add749')
'input': HexBytes('0xa9059cbb00000000000000000000000061b7b515c1ec603cf21463bcac992b60fd610ca900000000000000000000000000000000000000000000002dbf877cf6ec677800')
'nonce': 38
'to': '0xaea46A60368A7bD060eec7DF8CBa43b7EF41Ad85'
'transactionIndex': 0
'value': 0
'type': 2
'accessList': []
'chainId': 1
'v': 0
'r': HexBytes('0x8ee28a85ac42174b9e10c49613c0cddcf5d5a5ecb90bd516f81b45a957a64fe2')
's': HexBytes('0x05349c1076cc83990f425773d6b5995474782f1fccf1b2e43529ac54ac6ae144')
'yParity': '0x0'

I then got the raw transaction (signed):

>>> w3.eth.get_raw_transaction(mempool_tx.hash.hex())
HexBytes('0x02f8b1012685114f11efdc85114f11efdc82fce894aea46a60368a7bd060eec7df8cba43b7ef41ad8580b844a9059cbb00000000000000000000000061b7b515c1ec603cf21463bcac992b60fd610ca900000000000000000000000000000000000000000000002dbf877cf6ec677800c080a08ee28a85ac42174b9e10c49613c0cddcf5d5a5ecb90bd516f81b45a957a64fe2a005349c1076cc83990f425773d6b5995474782f1fccf1b2e43529ac54ac6ae144')

And then I tried to manually construct the raw RLP transaction in python.

So, the syntax is this:

'0x02' + RLP([chainId, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to, value, data, accessList, v, r, s])

from rlp import encode
from web3.types import HexBytes
from web3.datastructures import AttributeDict

tx = AttributeDict({'blockHash': HexBytes('0x92911a95b4d4b07bb13552bfb29cc43bf43eb7d63af7c968ddaa3df9a6f22988'), 'blockNumber': 17964189, 'from': '0xfd00479c712370C84B398CB8951aebC8039c3024', 'gas': 64744, 'gasPrice': 74341019612, 'maxFeePerGas': 74341019612, 'maxPriorityFeePerGas': 74341019612, 'hash': HexBytes('0x6292090633c01105ec72f4ec9cceb6d7f96321074b4faca9ad394c3a35add749'), 'input': HexBytes('0xa9059cbb00000000000000000000000061b7b515c1ec603cf21463bcac992b60fd610ca900000000000000000000000000000000000000000000002dbf877cf6ec677800'), 'nonce': 38, 'to': '0xaea46A60368A7bD060eec7DF8CBa43b7EF41Ad85', 'transactionIndex': 0, 'value': 0, 'type': 2, 'accessList': [], 'chainId': 1, 'v': 0, 'r': HexBytes('0x8ee28a85ac42174b9e10c49613c0cddcf5d5a5ecb90bd516f81b45a957a64fe2'), 's': HexBytes('0x05349c1076cc83990f425773d6b5995474782f1fccf1b2e43529ac54ac6ae144'), 'yParity': '0x0'})

encoded_tx = encode([ tx['chainId'], tx['nonce'], tx['maxPriorityFeePerGas'], tx['maxFeePerGas'], tx['gas'], tx['to'], tx['value'], tx['input'], tx['accessList'], tx['yParity'], tx['r'], tx['s'] ])
print('0x02' + encoded_tx.hex())

This is the output: f8ca012685114f11efdc85114f11efdc82fce8aa30786165613436413630333638413762443036306565633744463843426134336237454634314164383580b844a9059cbb00000000000000000000000061b7b515c1ec603cf21463bcac992b60fd610ca900000000000000000000000000000000000000000000002dbf877cf6ec677800c083307830a08ee28a85ac42174b9e10c49613c0cddcf5d5a5ecb90bd516f81b45a957a64fe2a005349c1076cc83990f425773d6b5995474782f1fccf1b2e43529ac54ac6ae144

Why is it incorrect? What did I do wrong? Is there a lib that already does this in python?

1 Answer 1

2

You need to convert tx["to"] to bytes before encoding. You also have tx["yParity"] where tx["v"] should be, per your syntax guide.

from rlp import encode
from web3.types import HexBytes
from web3.datastructures import AttributeDict

from eth_utils import to_bytes

tx = AttributeDict({'blockHash': HexBytes('0x92911a95b4d4b07bb13552bfb29cc43bf43eb7d63af7c968ddaa3df9a6f22988'), 'blockNumber': 17964189, 'from': '0xfd00479c712370C84B398CB8951aebC8039c3024', 'gas': 64744, 'gasPrice': 74341019612, 'maxFeePerGas': 74341019612, 'maxPriorityFeePerGas': 74341019612, 'hash': HexBytes('0x6292090633c01105ec72f4ec9cceb6d7f96321074b4faca9ad394c3a35add749'), 'input': HexBytes('0xa9059cbb00000000000000000000000061b7b515c1ec603cf21463bcac992b60fd610ca900000000000000000000000000000000000000000000002dbf877cf6ec677800'), 'nonce': 38, 'to': '0xaea46A60368A7bD060eec7DF8CBa43b7EF41Ad85', 'transactionIndex': 0, 'value': 0, 'type': 2, 'accessList': [], 'chainId': 1, 'v': 0, 'r': HexBytes('0x8ee28a85ac42174b9e10c49613c0cddcf5d5a5ecb90bd516f81b45a957a64fe2'), 's': HexBytes('0x05349c1076cc83990f425773d6b5995474782f1fccf1b2e43529ac54ac6ae144'), 'yParity': '0x0'})

expected = HexBytes('0x02f8b1012685114f11efdc85114f11efdc82fce894aea46a60368a7bd060eec7df8cba43b7ef41ad8580b844a9059cbb00000000000000000000000061b7b515c1ec603cf21463bcac992b60fd610ca900000000000000000000000000000000000000000000002dbf877cf6ec677800c080a08ee28a85ac42174b9e10c49613c0cddcf5d5a5ecb90bd516f81b45a957a64fe2a005349c1076cc83990f425773d6b5995474782f1fccf1b2e43529ac54ac6ae144')

to_as_bytes = to_bytes(hexstr=tx["to"])
encoded_tx = encode([ tx['chainId'], tx['nonce'], tx['maxPriorityFeePerGas'], tx['maxFeePerGas'], tx['gas'], to_as_bytes, tx['value'], tx['input'], tx['accessList'], tx['v'], tx['r'], tx['s'] ])

assert HexBytes("0x02" + encoded_tx.hex()) == expected

Your Answer

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

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