Documentation

Execute Token Swaps on Uniswap V3 with Pimlico Alto & BuildBear

Pimlico Alto simplifies bundling user operations into transactions and submitting them to the blockchain via standard JSON-RPC requests.

Introduction

This tutorial guides you through integrating ERC-4337-compatible bundlers for account abstraction in blockchain applications using the Pimlico Alto plugin. Pimlico Alto simplifies bundling user operations into transactions and submitting them to the blockchain via standard JSON-RPC requests. It supports permissionless.js, enabling seamless Web3 application development and testing.


What You'll Learn

  • Install and configure Pimlico Alto in your BuildBear Sandbox.
  • Use permissionless.js to create and manage smart accounts.
  • Set up Alto Bundler on your BuildBear network.
  • Fund smart accounts for transactions.
  • Swap DAI to USDC on Uniswap V3 using Alto Bundler.

Why Test Pimlico AA with BuildBear Sandbox?

✅ Seamless Integration

Pimlico Alto Plugin is designed for effortless integration with BuildBear Sandboxes, making it easy to experiment with Account Abstraction (AA) features.

Permissionless.js Support

  • High-level API for managing ERC-4337 smart accounts, bundlers, and paymasters.
  • Built on Viem for modularity and extensibility.

✅ JSON-RPC Support for Bundler

Interact with the bundler using standard methods:

  • eth_sendUserOperation
  • eth_estimateUserOperationGas
  • eth_getUserOperationReceipt
  • eth_getUserOperationByHash
  • eth_supportedEntryPoints
  • pimlico_getUserOperationGasPrice
  • pimlico_getUserOperationStatus

✅ Unlocked Faucets

BuildBear provides unlimited access to native and ERC-20 tokens, ensuring smooth development and testing.


Step 1: Setting Up Pimlico Alto

1. Create a BuildBear Sandbox

  • Navigate to BuildBear and create a new Sandbox or use an existing one.
  • Open the Plugins tab in your Sandbox dashboard.

2. Install the Pimlico Alto Plugin

  • Locate Pimlico Alto Bundler in the plugin marketplace.
  • Click Install to add it to your environment.

Install Pimlico Alto

  • Verify installation in the Installed Plugins tab.

3. Retrieve the RPC URL

Copy the RPC URL from your dashboard:

BuildBear Sandbox RPC URL: https://rpc.buildbear.io/{SANDBOX-ID}

4. Install Dependencies

{
  "name": "pimlico-tutorial-template",
  "version": "1.0.0",
  "dependencies": {
    "dotenv": "^16.3.1",
    "ethers": "^6.13.5",
    "permissionless": "^0.2.0",
    "viem": "^2.20.0"
  },
  "devDependencies": {
    "@types/node": "^20.11.10",
    "tsx": "^3.13.0"
  }
}

Step 2: Configuring BuildBear

Define and configure your sandbox network with Viem:

const buildbearSandboxUrl = "https://rpc.buildbear.io/{SANDBOX-ID}";

Setup network and client:

const BBSandboxNetwork = defineChain({
  id: 23351,
  name: "BuildBear x Polygon Mainnet Sandbox",
  nativeCurrency: { name: "MATIC", symbol: "MATIC", decimals: 18 },
  rpcUrls: { default: { http: [buildbearSandboxUrl] } },
  blockExplorers: {
    default: {
      name: "BuildBear Explorer",
      url: "https://explorer.buildbear.io/{SANDBOX-ID}",
      apiUrl: "https://api.buildbear.io/{SANDBOX-ID}/api",
    },
  },
});
 
const privateKey = process.env.PRIVATE_KEY || generatePrivateKey();
appendFileSync(".env", `\nPRIVATE_KEY=${privateKey}`);

Step 3: Configuring Pimlico Client & Smart Accounts

Setup Pimlico and Smart Accounts:

const pimlicoClient = createPimlicoClient({
  transport: http(buildbearSandboxUrl),
  entryPoint: { address: entryPoint07Address, version: "0.7" },
});
 
const signer = privateKeyToAccount(privateKey);
const account = await toSafeSmartAccount({
  client: publicClient,
  owners: [signer],
  entryPoint: { address: entryPoint07Address, version: "0.7" },
  version: "1.4.1",
});
 
const smartAccountClient = createSmartAccountClient({
  account,
  chain: BBSandboxNetwork,
  bundlerTransport: http(buildbearSandboxUrl),
  userOperation: {
    estimateFeesPerGas: async () => (await pimlicoClient.getUserOperationGasPrice()).fast,
  },
});

Step 4: Funding & Executing Transactions

Fund your account using BuildBear faucets:

  • Native Faucet: buildbear_nativeFaucet
  • ERC20 Faucet: buildbear_ERC20Faucet

Setup swap transaction:

let swapParams = {
  tokenIn: "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063",
  tokenOut: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
  fee: 3000,
  recipient: account.address,
  deadline: BigInt(Math.floor(Date.now() / 1000) + 1800),
  amountIn: parseEther("1"),
  amountOutMinimum: 0n,
  sqrtPriceLimitX96: 0n,
};

Send and execute:

const txHash = await smartAccountClient.sendUserOperation({
  account,
  calls: [ /* calls array as given */ ],
});
 
await smartAccountClient.waitForUserOperationReceipt({ hash: txHash });

View Transaction on Explorer

Transaction


(Optional) Debugging and Deep Dive into UserOp with Sentio

Clicking on View On Sentio will open the Sentio debugger for the transaction, offering detailed insights into the transaction's execution, including fund flow, contract interactions, and gas consumption.

✅ Fund Flow

Depicts the flow of funds among different contracts and their order of execution:

Fund Flow

✅ Call Trace

Displays the detailed sequence of contract calls executed during the transaction, including function calls, inputs, and outputs:

Call Trace

✅ Call Graph

Provides a high-level visual representation of contracts and the functions involved in the transaction execution:

Call Graph

✅ Gas Profiler

Illustrates gas usage metrics, including gas limits, actual gas used, initial gas provided, and a detailed breakdown of gas consumption per function call:

Gas Profiler

✅ Debugging with Sentio

Beyond the overview, Sentio offers a detailed debugging interface:

  • Debugger Tab: Inspect individual function calls, inputs, gas metrics, and return values:

    Debugger Tab

  • Contracts Tab: Provides deeper insight into contract details and the specific functions executed:

    Contracts Tab

  • Events Tab: Lists all events emitted by contracts during transaction execution:

    Events Tab

  • State Tab: Offers an overview of account balances and state changes before and after the transaction.


✅ Conclusion

By following this tutorial, you have successfully:

  • Set up Pimlico Alto
  • Configured smart accounts
  • Funded accounts
  • Executed token swaps on Uniswap V3

For full code and demos, check out the GitHub repository