Combine two transactions into one transaction

This guide demonstrates how to combine two simple transactions:

  • Set allowance on Uniswap
  • Swap on Uniswap
1

Install dependencies

Install the required packages:

npm install @compass-labs/api-sdk viem
2

Set up your environment

Set your environment variables for your private key, RPC URL, and Compass API key.

export PRIVATE_KEY="your_private_key"
export RPC_URL="your_rpc_url"
export COMPASS_API_KEY="your_compass_api_key"
3

Initialize SDK, Wallet, and Account

This step loads your environment variables, sets up the viem wallet client, and initializes your account and the Compass API SDK.

4

Create wallet client and account

This step creates the wallet client and account using your private key and RPC URL.

5

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.

6

Bundle and execute the actions

Bundle the allowance and swap actions, then execute them atomically. This step shows how to combine both actions into a single transaction using the Compass API.

7

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.

Full Code

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