🕐 Time to complete: ±10 minutes 🧰 What you need:

Introduction

This guide demonstrates how to combine two simple transactions:
  • Set allowance on Uniswap
  • Swap on Uniswap
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.

Setup

1

Install Dependencies

Install the required packages.
npm install @compass-labs/api-sdk viem dotenv
2

Set Environment Variables

Create a .env file in your project root.
.env
PRIVATE_KEY="your_wallet_private_key"
RPC_URL="your_ethereum_rpc_url"
COMPASS_API_KEY="your_compass_api_key"
3

Import Libraries & Environment Variables

4

Initialize SDK and Account

The full, uninterrupted code is available at the end of the tutorial.

Get Authorization

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.
1

Get and Sign Authorization

Request authorization from the Compass API and sign it with your wallet to authenticate the transaction batching.

Bundle the Actions

Now let’s 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.
1

Create Bundled Transaction

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.

Execute the Transaction

The final step is to sign and broadcast the transaction to the network. This will execute your bundled actions in a single atomic transaction.
1

Sign and Broadcast 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.
For further practice, try bundling different combinations of actions, test with various token pairs, or explore other Compass Bundler features to create more complex transaction bundles.

Full Code

Here is the full script from the tutorial. Copy and paste into your code editor and play around!

Resources