Python SDK - Multi-Agent Transactions
Multi-agent transactions allow multiple accounts to participate in the logic of a Move contract.
This can be used to require multiple parties agree to a transaction before executing or to use resources from multiple accounts.
Writing Multi-Agent Transactions
Section titled “Writing Multi-Agent Transactions”Creating and executing a multi-agent transaction follows a similar flow to the regular transaction flow, but with several notable differences.
-
Build a raw transaction, then wrap it in
MultiAgentRawTransactionwith each additional signer.raw_txn = await aptos.transaction.build(sender=alice.address,payload=TransactionPayload(payload),)multi_agent_txn = MultiAgentRawTransaction(raw_txn, [bob.address]) -
Sign once for each signer.
alice_auth = multi_agent_txn.sign(alice.private_key)bob_auth = multi_agent_txn.sign(bob.private_key) -
Combine the signatures with a
MultiAgentAuthenticator.authenticator = Authenticator(MultiAgentAuthenticator(sender=alice_auth,secondary_signers=[(bob.address, bob_auth)],)) -
Submit the signed transaction.
txn_hash = await aptos.transaction.submit(SignedTransaction(raw_txn, authenticator)) -
Wait for the transaction to resolve.
result = await aptos.transaction.wait_for_transaction(txn_hash)
Python Multi-Agent Code Sample
Section titled “Python Multi-Agent Code Sample”import asyncio
from aptos_sdk.v2 import Account, Aptos, AptosConfig, Networkfrom aptos_sdk.v2.bcs import Serializerfrom aptos_sdk.v2.errors import TransactionFailedErrorfrom aptos_sdk.v2.transactions import ( Authenticator, EntryFunction, MultiAgentAuthenticator, MultiAgentRawTransaction, SignedTransaction, TransactionArgument, TransactionPayload,)from aptos_sdk.v2.types import StructTag, TypeTag
async def main(): async with Aptos(AptosConfig(network=Network.DEVNET)) as aptos: alice = Account.generate() bob = Account.generate()
await asyncio.gather( aptos.faucet.fund_account(alice.address, 100_000_000), aptos.faucet.fund_account(bob.address, 10_000_000), )
payload = EntryFunction.natural( "0x1::coin", "transfer", [TypeTag(StructTag.from_str("0x1::aptos_coin::AptosCoin"))], [ TransactionArgument(bob.address, Serializer.struct), TransactionArgument(5_000, Serializer.u64), ], ) raw_txn = await aptos.transaction.build( sender=alice.address, payload=TransactionPayload(payload), ) multi_agent_txn = MultiAgentRawTransaction(raw_txn, [bob.address])
alice_auth = multi_agent_txn.sign(alice.private_key) bob_auth = multi_agent_txn.sign(bob.private_key) authenticator = Authenticator( MultiAgentAuthenticator( sender=alice_auth, secondary_signers=[(bob.address, bob_auth)], ) )
txn_hash = await aptos.transaction.submit(SignedTransaction(raw_txn, authenticator)) try: result = await aptos.transaction.wait_for_transaction(txn_hash) print(f"Success: {result['success']}") except TransactionFailedError as err: print(f"Expected without a multi-signer entry function: {err}")
asyncio.run(main())Common Errors
Section titled “Common Errors”NUMBER_OF_SIGNER_ARGUMENTS_MISMATCH - This happens when you are attempting to
do multi-agent signing for a function which does not require that number of
accounts. For example, if you try using multiple signatures for a
0x1::aptos_account::transfer function - it only expects one address, and so
produces an error when more than one is provided.