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?)
Best Answer
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
andfunction_B
. What you can do is callfunction_B
fromfunction_A
. On your python script, callfunction_A
. If your logic is correct, you embedded two action into a single transaction. If either one of the fails, your transactions reverts!