Add Sub Accounts to Onchainkit/Minikit
This guide will show you how to add Sub Accounts to your existing OnchainKit or MiniKit project.
Sub Accounts allow you to create a seamless user experience by skipping transaction confirmations, you can read more about it in the Sub Accounts Intro Page.
Before you start, make sure you have checked the following guides:
Smart Wallet compatibility with Mini Apps
The Coinbase Wallet team is working on adding Smart Wallet support to Mini Apps.
For now, you can only use Sub Accounts with OnchainKit/MiniKit outside of the social feed.
Skip ahead
If you want to skip ahead and just get the final code, you can find it here:
About this codebase
The codebase uses MiniKit, but the same code can be used for OnchainKit with minimal changes.
The difference is detailed in the Providers section.
Add Sub Accounts to your OnchainKit/MiniKit project
Override the default Coinbase Wallet SDK version
Currently, Sub Accounts are only available in the Smart Wallet development environment (Canary version).
To override the default Coinbase Wallet SDK version, you can run the following command:
Re-install the dependencies
Make sure to delete the node_modules
folder and package-lock.json
file before re-installing the dependencies after running the command above.
Sub Accounts are coming to prod soon !
This is a temporary solution to get Sub Accounts working.
Once the new version of the Coinbase Wallet SDK is released, you can remove this step.
Setting up the Wagmi config
Make sure to update your wagmi.ts
file to include the following:
Let’s break down the key preference parameters:
-
keysUrl
: Points to the development environment for Smart Wallet testing -
options: 'smartWalletOnly'
: Ensures only Smart Wallet mode is used -
enableAutoSubAccounts: true
: When set to true, automatically creates a Sub Account at connection -
defaultSpendLimits
: Configures Spend Limits for Sub Account for a network (eg. Base Sepolia84532
), including:- Token address (In this case,
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE
represents the native ETH) - Allowance WEI amount (in Hex)
- Time period for the allowance (in seconds, e.g., 86400 for 24 hours)
- Token address (In this case,
About keysUrl
Sub Accounts are currently only available in the Smart Wallet development environment.
To use this environment, you need to set the keysUrl
to https://keys-dev.coinbase.com/connect
in your configuration.
Setting up the OnchainKit/MiniKit providers config
Once the wagmi.ts
file is updated, you can update your providers.tsx
file to include the following:
For OnchainKit
For OnchainKit, use OnchainKitProvider
instead of MiniKitProvider
as it’s detailed in the
OnchainKit Custom Providers page.
Run your app
After installing the dependencies as described above, make sure your environment variables are up to date as per the OnchainKit Quickstartor MiniKit Quickstart.
Then, you can run the app with the following command:
Congratulations! You’ve successfully added Sub Accounts to your OnchainKit/MiniKit project.
If you have any questions, join the #smart-wallet channel on Discord.