Sending Transactions to Multiple Contracts
NEAR allows you to dispatch multiple transactions simultaneously, so users only interact with their wallet once. However, these transactions remain independent - if one fails, the others are not rolled back.
Transaction Structure
Each transaction targets a specific contract and contains one or more actions:
Loading...
Building Transactions
Let's break down the transaction structure:
Guest Book Transaction
const guestTx = {
receiverId: GUEST_ADDRESS,
actions: [
{
type: 'FunctionCall',
params: {
methodName: 'add_message',
args: { text: greeting.value },
gas: THIRTY_TGAS,
deposit: GUEST_DEPOSIT
}
}
]
}
receiverId: The contract account ID (guestbook.near-examples.testnet)actions: Array of actions to execute on this contractmethodName: The contract method to callargs: Arguments passed to the methodgas: Gas limit (30 TGas = 30,000,000,000,000 gas units)deposit: NEAR tokens to attach (0 for regular, 0.1Ⓝ for premium)
Hello NEAR Transaction
const helloTx = {
receiverId: HELLO_ADDRESS,
actions: [
{
type: 'FunctionCall',
params: {
methodName: 'set_greeting',
args: { greeting: greeting.value },
gas: THIRTY_TGAS,
deposit: NO_DEPOSIT
}
}
]
}
Similar structure, but:
- Targets a different contract (
hello.near-examples.testnet) - Calls
set_greetingmethod - No deposit required
Signing and Sending
The wallet selector handles signing and broadcasting:
Loading...
await wallet.signAndSendTransactions({
transactions: [ helloTx, guestTx ]
});
This opens the wallet once for the user to approve both transactions.
Important Characteristics
Independence
Even though transactions are signed together, they are completely independent:
- If
helloTxsucceeds andguestTxfails,helloTxis NOT rolled back - Each transaction is processed separately by the network
- Success of one does not guarantee success of another
Handling Transaction Responses
After submission, you'll receive transaction outcomes:
const outcome = await wallet.signAndSendTransactions({
transactions: [ helloTx, guestTx ]
});
// Each transaction has its own outcome
console.log(outcome);
Complete Form Handler
Loading...
The form handler:
- Prevents default form submission
- Extracts form values (greeting text and premium checkbox)
- Shows loading state
- Calculates deposit based on premium selection
- Constructs both transactions
- Submits them for signing
Testing Multiple Transactions
Try these scenarios:
- Both succeed: Valid greeting text, sufficient balance
- One fails: Insufficient balance for premium message (Guest Book fails, Hello NEAR succeeds)
- Network issues: Transactions may process at different times
Next, we'll explore batching actions within a single contract for atomic operations.