Bridge Assets Between Base and Your Appchain Using OnchainKit
Appchains are for mature projects who are seeking scale through dedicated blockspace, full control over their infrastructure, or may be looking to utilize custom gas tokens.
After you’ve deployed an Appchain, offering your users a hassle-free way to move funds is key to a great user experience. If you are looking to launch an Appchain or learn more about Appchains broadly, refer to the Appchains FAQs page.
In this guide, you’ll walk you through integrating the [Bridge component] from OnchainKit into your app to facilitate easy fund transfers.
Make sure you’re running OnchainKit version >= 0.37.0 and have a basic understanding of Viem, JavaScript/TypeScript, and Tanstack before you begin.
Objectives
By the end of this tutorial you will be able to:
- Install and configure the required dependencies.
- Set up environment variables for OnchainKit.
- Configure a custom appchain using Viem’s
defineChain
function. - Integrate a custom Wagmi configuration to support your new chain.
- Create a dedicated Bridge page for users to move funds seamlessly.
Prerequisites
Ensure you have the following:
- OnchainKit (version >= 0.37.0)
- Viem for chain definitions and interactions
- JavaScript/TypeScript development environment
- Tanstack for state management
Setup
Install Dependencies
Start by creating an OnchainKit sample app by running the following commands in your terminal:
Configure Environment Variables
For your application to work, there are a few required environment variables that need to be set. To find them, navigate to the [Coinbase Developer Platform] site, select the API Keys tab, then click the Client API Key button.
With your client API key copied to your clipboard, create a .env
file in your project root with the following content. Replace the placeholder for your API key as needed:
Configure Appchain details
Create a new file named Chain.ts
in the app/
directory. In this new file, define your appchain using Viem’s defineChain
function. In this example, you will configure the Blocklords Appchain:
Extend Your Wagmi Configuration
To enable the OnchainKit Provider to communicate with both your Base chain and your Appchain, update your Wagmi configuration. Create a wagmi.ts
file or modify your configuration file as shown below:
After creating your custom Wagmi configuration, be sure to import it into your OnchainKitProvider
component (typically within your main provider file e.g. Providers.tsx
).
This is a crucial step in enabling our OnchainKit Provider to communicate with both our Base chain and our Appchain.
Create the Bridge Component Page
Now, create a dedicated page to display the bridge component. For example, create a new folder named Bridge
in your app/
directory and add a page.tsx
file with the following content:
Running Locally
For reference, the final demo application is available on GitHub at https://github.com/hughescoin/ock-bridge-tutorial.git.
Conclusion
In this tutorial, you learned how to:
- Set up the required dependencies and environment variables.
- Configure your custom appchain using Viem.
- Extend your Wagmi configuration to integrate both your Base chain and your Appchain.
- Build a dedicated Bridge page to allow users to move funds seamlessly into your appchain.
For additional details or troubleshooting tips, refer to the [OnchainKit documentation] and explore further components.
Happy Building!