This guide demonstrates how to combine two simple transactions:
Bundling actions into a single transaction ensures they execute atomically, saving gas, improving UX, and avoiding partial execution risks.
Any action available on the API Reference can be used for bundling.
Install dependencies
Install the required packages:
Set Environment Variables
Create a .env
file in your project root:
Initialize SDK
This step loads your environment variables and initialises the Compass API SDK.
Create wallet client and account
This step creates the wallet client and account using your private key and RPC URL.
Get and sign authorization for transaction batching
Before you can bundle transactions, you need to get an authorization from the Compass API and sign it with your private key. This ensures only you can execute the batch.
Bundle the actions
Bundle the allowance and swap actions for atomic execution. This step shows how to combine both actions into a single transaction using the Compass API.
Any transactional endpoint in Compass API can be turned into an action, see API Reference.
Sign and broadcast the transaction
Sign the returned transaction with your private key and broadcast it to the network. This is the final step to actually send your bundled transaction to Ethereum.
Here is the full script from the tutorial. Copy and paste in your code editor and play around!
Access detailed API documentation and references
View more code examples and implementations