0

I would like to sign_transaction and send raw transaction with custom transaction fields. But I could not do it. Any help will be appreciated.

Thanks web3.py code to sign and send transaction

read a text file containing transaction details error logs

1 Answer 1

2

There are a few issues.

If it's just Python syntax, then you are missing commas and the dictionary for row:

# Fixed commas and row.id and row.trust_value
for row in addresses_df.itertuples():
    nonce_cnt = int(nonce_count)
    transaction = {
    'to': row.address,
    'value': int(row.balance),
    'gas': 21000,
    'gasPrice': 1000000000,
    'nonce': nonce_cnt,
    'id': row.id,
    'trust value': row.trust_value
    }

This might be enough since I don't know what's in the 90 lines of code leading to this.

But in general, id & trust_value are not a part of an Ethereum transaction, so hypothetically anyone that'd want to add them as a custom field to a transaction, they'd need to encode them and add as data to a transaction.

For example, this would work:

import os
import time
import pandas as pd
from web3 import Web3

from pathlib import Path
from eth_account.messages import encode_defunct

ganache_url = "http://127.0.0.1:7545"
web3 = Web3(Web3.HTTPProvider(ganache_url))

account_1 = "ADDRESS"
account_2 = "ADDRESS"
private_key = "PRIVATE_KEY"

addresses_df = pd.read_csv(
    'test.txt',
    header=None, names=['id', 'address', 'balance', 'trust_value']
)

# Encode id and trust_value to hex format
def encode_to_hex(id_val, trust_val):
    id_hex = hex(id_val)[2:].rjust(8, '0')
    trust_val_hex = hex(trust_val)[2:].rjust(2, '0')
    return "0x" + id_hex + trust_val_hex

nonce_count = web3.eth.get_transaction_count(account_1)

# Iterate through addresses and send txs with the encoded input in the data field for the tx
for row in addresses_df.itertuples():
    nonce_cnt = int(nonce_count)
    encoded_input = encode_to_hex(row.id, row.trust_value)
    transaction = {
        'to': row.address,
        'value': int(row.balance),
        'gas': 100000, # Change gas from 21k to higher since we are now sending a tx with data, which is higher cost
        'gasPrice': 1000000000,
        'nonce': nonce_cnt,
        'data': encoded_input
    }
    signed = web3.eth.account.sign_transaction(transaction, private_key)
    TxHash = web3.eth.send_raw_transaction(signed.rawTransaction)
    print("Address:", row.address, "Nonce:", nonce_cnt, "TX:", TxHash.hex(), "balance:", row.balance, "trust value:", row.trust_value)
    nonce_count += 1
    time.sleep(1)
2
  • Hi Ake thanks for your answer. However it is throwing inconsistent use of tabs and spaces in indentation in the following line of the code: encoded_input = encode_to_hex(row.id, row.trust_value) Oct 11, 2023 at 12:20
  • Thank you Ake, there was some indentation issues and it is solved. Oct 11, 2023 at 13:32

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.