Skip to content

Instantly share code, notes, and snippets.

@jsvisa
Last active August 1, 2023 12:59
Show Gist options
  • Save jsvisa/543bfe57a4a3c95a5090a2aedb4ca166 to your computer and use it in GitHub Desktop.
Save jsvisa/543bfe57a4a3c95a5090a2aedb4ca166 to your computer and use it in GitHub Desktop.
send gasPrice 0 tx
#!/usr/bin/env python
import argparse
import logging
from web3 import Web3, HTTPProvider
logging.basicConfig(
format="[%(asctime)s] - %(levelname)s - %(message)s", level=logging.INFO
)
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Send ethereum transaction",
)
parser.add_argument(
"--provider",
default="http://127.0.0.1:8545",
help="Web3 provider",
)
parser.add_argument(
"--private-key-file",
default="private_key.json",
help="Private key file",
)
parser.add_argument(
"--nonce",
type=int,
default=0,
help="nonce",
)
parser.add_argument(
"--count",
type=int,
default=10000,
help="count",
)
args = parser.parse_args()
w3 = Web3(HTTPProvider(args.provider))
with open(args.private_key_file) as keyfile:
encrypted_key = keyfile.read()
private_key = w3.eth.account.decrypt(encrypted_key, "")
acct = w3.eth.account.from_key(private_key)
if args.nonce == 0:
nonce = w3.eth.get_transaction_count(acct.address)
else:
nonce = args.nonce
chain_id = w3.eth.chain_id
for n in range(0, args.count):
txn = dict(
nonce=nonce + n,
gasPrice=0,
gas=10240000,
to=acct.address,
value=0,
data=b"1" * 102400,
)
signed_txn = w3.eth.account.sign_transaction(txn, private_key)
txhash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
logging.info(f"{txn} -> {txhash.hex()}")
if __name__ == "__main__":
main()
@jsvisa
Copy link
Author

jsvisa commented Aug 1, 2023

  1. provide a valid private keyfile(password is empty) which contains some Ether
  2. execute this python script, eg:
python send_tx.py --private-key-file xxx --provider HTTP://127.0.0.1:8545 --nonce 1 --count 100000

then check the txpool status, you'll see something like this:

$ http :8545 method=txpool_status id=1 jsonrpc=2.0 params:='[]'

{
    "id": "1",
    "jsonrpc": "2.0",
    "result": {
        "pending": "0x2710",
        "queued": "0x0"
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment