I'm able to build and broadcast a simple transaction from a contract:

raw_tx = contract.functions.someFunction()

tx_params = {
    "from": WALLET,
    "nonce": nonce,
    "gas": 8000000,
    "gasPrice": w3.toWei("25", "gwei"),

tx = raw_tx.buildTransaction(tx_params)

signed_tx = w3.eth.account.sign_transaction(tx, private_key=PRIVATE_KEY)

tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)

Nothing really hard here. What I would like to do next is bundle two instructions like the one above in one single transaction. If the two instructions are called A and B, I would like A to run first, then B. But if for some reason B fails, I would like A to be reverted. Of course if A fails, we shouldn't proceed to B.

I have been looking around for a few hours but I wasn't able to find anything that would allow me to do that with web3.py.

I have found this SO thread tagged with web3js that claims it's possible: https://stackoverflow.com/questions/71451583/best-practice-of-sending-multiple-dependent-transaction-solana-web3js/71457527#71457527

Apparently it would be possible to:

you can pack instructions into a single transaction and have any failure fail the transaction as a whole.

So my question would be: can I do that with web3.py? Is it possible at all to do something along those lines (maybe by deploying a contract?)

  • SO question is for Solana a different blockchain so it doesn't apply to Ethereum.
    – Ismael
    Jun 12, 2022 at 21:05

1 Answer 1


Technically yes. The smart contracts are the one who provides atomic transactions.

You need to embed the whole logic into a single contract and trigger a single transaction from web3.py.

This is the same reason attackers use smart contracts for attack.

Here is brief sketch

Imagine you have two function in smart contract: function_A and function_B. What you can do is call function_B from function_A. On your python script, call function_A. If your logic is correct, you embedded two action into a single transaction. If either one of the fails, your transactions reverts!

  • Ok, fair. What about the piece of JS from the SO post?
    – JPFrancoia
    Jun 9, 2022 at 20:38
  • What about them?
    – Emrah
    Jun 9, 2022 at 21:14

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.