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.

Prerequisites

1

Install dependencies

Install the required packages:
pip install compass_api_sdk web3 python-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"

Implementation Guide

1

Initialize SDK, Web3, and account

This step loads your environment variables, sets up the Web3 provider, and initializes your account and the Compass API SDK.
2

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

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

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!