Account Abstraction on Base using Biconomy
A tutorial that teaches how to implement Account Abstraction into a Base project using Biconomy paymasters, bundlers, and smart accounts.
Account Abstraction on Base using Biconomy
This page will guide you through the process of implementing Account Abstraction in your Base projects using Biconomy paymasters, bundlers, and smart accounts.
Objectives
By the end of this tutorial you should be able to do the following:
- Set up a smart contract project for Base using Foundry
- Set up a Next.js frontend project using
create next-app
- Setup user login and authentication using Particle Network
- Setup a Biconomy paymaster and bundler
- Create a gasless transaction
Prerequisites
Foundry
This tutorial requires you to have Foundry installed.
- From the command-line (terminal), run:
curl -L https://foundry.paradigm.xyz | bash
- Then run
foundryup
, to install the latest (nightly) build of Foundry
For more information, see the Foundry Book installation guide.
Coinbase Wallet
This tutorial requires you to have a wallet. You can create a wallet by downloading the Coinbase Wallet browser extension:
- Download Coinbase Wallet
Wallet funds
To complete this tutorial, you will need to fund a wallet with ETH on Base Goerli.
The ETH is required for covering gas fees associated with deploying smart contracts to the network.
- To fund your wallet with ETH on Base Goerli, visit a faucet listed on the Base Faucets page.
What is Biconomy?
Biconomy is a toolkit that offers a full-stack solution for Account Abstraction, including smart accounts, paymasters for sponsoring gas fees, and bundlers for bundling user operations into a single transaction.
High-level concepts
Account Abstraction
Account Abstraction (ERC-4337) allows users to use Smart Contract wallets instead of traditional Externally Owned Account (EOA) wallets.
Smart Accounts
A smart account (also known as a smart contract wallet) is a wallet that stores and manages digital assets (ERC-20 tokens, NFTs, etc.) using a smart contract.
User Operations
A user operation is a pseudo-transaction object sent by a smart account that describes a transaction to be sent. Multiple user operations are eventually bundled together and initiated as a single real transaction by a bundler.
Paymaster
A paymaster is a special smart contract that allows applications to “sponsor user operations”, meaning it will pay for the gas fees associated with the resulting transaction.
Bundler
A special node that monitors a mempool of user operations and bundles multiple user operations into a single transaction.
Biconomy’s Paymaster is a service that allows you to sponsor gas fees for your users, enabling gasless transactions. Learn more in the Biconomy documentation.
Creating and deploying a smart contract
Before you begin, you need to set up a smart contract development environment using Foundry.
To create a new project, first create a new directory named myproject
, and change it to your current working directory:
Creating a Foundry project
Next, within the myproject
directory create a new directory named contracts
, and change it to your current working directory:
Then create a new Foundry project by running the following command:
This will create a Foundry project with the following basic layout:
The command creates a boilerplate Solidity smart contract file named src/Counter.sol
. This is the primary contract you will use for this tutorial.
Compiling the smart contract
Compile the smart contract to ensure it builds without any errors.
To compile your smart contract, run:
Setting up a wallet as the deployer
Before you can deploy your smart contract to various chains you will need to set up a wallet to be used as the deployer.
To do so, you can use the cast wallet import
command to import the private key of the wallet into Foundry’s securely encrypted keystore:
After running the command above, you will be prompted to enter your private key, as well as a password for signing transactions.
For instructions on how to get your private key from Coinbase Wallet, visit the Coinbase Wallet documentation. It is critical that you do NOT commit this to a public repo.
To confirm that the wallet was imported as the deployer
account in your Foundry project, run:
Deploying the smart contract
To deploy the smart contract, you can use the forge create
command. The command requires you to specify the smart contract you want to deploy, an RPC URL of the network you want to deploy to, and the account you want to deploy with.
Your wallet must be funded with ETH on the Base Goerli testnet to cover the gas fees associated with the smart contract deployment. Otherwise, the deployment will fail.
To get testnet ETH, see the prerequisites.
To deploy the smart contract to the Base Goerli testnet, run the following command:
When prompted, enter the password that you set earlier, when you imported your wallet’s private key.
After running the command above, the contract will be deployed on the Base Goerli test network. You can view the deployment status and contract by using a [block explorer]: /docs/chain/block-explorers.
Setting up the Paymaster and Bundler
To setup the paymaster and bundler for your project, you will need to visit the Biconomy Dashboard and complete the following steps.
Registering a paymaster
Add and register a Paymaster by completing the following steps:
- Visit the sign in to the Biconomy Dashboard
- From the dashboard, select the Paymasters tab and click Add your first Paymaster
- Provide a Name for your paymaster
- Select Base Goerli from the Network dropdown
- Click Register
You should now have a registered Biconomy paymaster.
The API Key and Paymaster URL for the paymaster are provided under the Overview tab in the Biconomy Dashboard.
Setting up the paymaster gas tank
Set up and fund the paymaster’s gas tank by completing the following steps:
- From the dashboard, navigate to the Paymasters tab
- Click Setup gas tank on the paymaster
- Navigate to Gas-Tank > Deposit, and click Set up gas tank
- Sign the message with your connected wallet to set up the gas tank
- Click Go to deposit
- Enter the amount of ETH you wish to deposit
- Click Deposit
ETH should now be deposited into the gas tank for your paymaster. You can visit the Withdraw tab at a later time if you wish to withdraw the funds.
Setting up the paymaster policies
Set up and fund the paymaster’s gas tank by completing the following steps:
- From the dashboard, navigate to the Paymasters tab
- Select the paymaster to configure
- Navigate to Policies > Contracts, and click Add your first contract
- Add the Name and the Smart contract address for your contract
- Select the increment and setNumber write methods as methods to sponsor
- Click Add Smart Contract
Setting up a bundler
- Visit the sign in to the Biconomy Dashboard
- From the dashboard, select the Bundlers tab
At the time of writing this tutorial, the Bundler service is still under development, however a Bundler URL is provided for testing out UserOperations on test networks. You can specify the chain ID 84531 to use the Bundler URL on Base Goerli testnet.
Setting up the frontend
Creating a Next.js project
After you set up your paymaster and bundler from the Biconomy Dashboard, the next step is to create a Next.js project for your app’s frontend.
From the root of the myproject
directory of your project, create a new Next.js project by running the following command:
Installing the dependencies
To use the paymaster and bundler that were setup from the Biconomy Dashboard, you will need to add a few dependencies to your Next.js project.
To install Biconomy as a dependency to your project, run the following command:
Creating Biconomy smart accounts requires a signer from an EIP-1193 provider. Biconomy works with a variety of different social login and embedded wallet onboarding solutions that provide access to a signer that can be used for creating smart accounts. In this tutorial, you will use Particle Network for user authentication and getting a smart account signer.
To install Particle Network as a dependency to your project, run the following command:
Updating the boilerplate code
The main page (page.tsx
) of the Next.js project created when running the yarn create next-app
command contains a Home
component. This component comes with a lot of code that is unnecessary for this tutorial.
Replace the content of the page.tsx
file with the following simplified code:
Adding social login using Particle Network
Setting up Particle Network
To get started adding social login into the app using Particle Network, import and initialize the Biconomy Particle Auth module in the page.tsx
file as shown below:
You will need to sign up for a Particle Network account and replace the values of PARTICLE_PROJECT_ID
, PARTICLE_CLIENT_ID
, and PARTICLE_APP_ID
.
If you encounter issues with Paymaster integration, ensure your API keys are correct and your contract is whitelisted in the Biconomy dashboard.