Using Account Abstraction in React
By using the wallet SDK alongside the React SDK, you can use smart accounts in your front-end applications easily.
Deploy an Account Factory
Deployable via the explore page or build your own ERC 4337 compatible factory contract using the Solidity SDK.
thirdweb offers the following three different kinds of account factories:
Read about the differences between the three smart account types here.
Create an API key
To use the bundler and paymaster, you must create an API key and a billing account.
To create an API Key:
- Head to the settings page in the dashboard and click the API Keys tab.
- Click on Create API Key:
- Follow the steps to create your API key.
To use account abstraction nfrastructure on mainnet you will also need to create an account and add a payment method.
Create an App
To use smart accounts in a React app you can either:
- Use npx thirdweb create command to create a new project with the dependencies already installed.
- Add the dependencies to an existing project using
npx thirdweb install
or your favorite package manager.
In this guide, we'll create a new project. Open your terminal and run:
When prompted, select/input the following options:
- A name for the project
EVM
as the blockchain- Select your desired environment e.g.
Next.js
orVite
. TypeScript
as the language.
This will create a react project with all the dependencies and a simple connect wallet button.
Build your connection UI
Using Connect Wallet (prebuilt UI)
To use the Connect Wallet component to connect your users to your app using smart accounts, we need to pass a
smartWallet
to thesupportedWallets
prop inThirdwebProvider
.You can change the configuration based on your requirements, but for this demo, we will enable gasless transactions and use a EmbeddedWallet as the personal wallet.
Pass the configuration to the provider:
Now, import the
ConnectWallet
component from the React package and add it to your app:Clicking on the connect button will show the following Modal which allows you to create or import a local wallet.
This is the personal wallet you are using to initialize the smart account. For local wallet, you can create a new wallet with a password or import a previously created wallet.
After connecting your personal wallet, a smart account is created for you and connected to the application.
Using the
useSmartWallet
hook (custom UI)The useSmartWallet hook allows you to programmatically connect your application to the wallet. You will need to build your own UI for connecting the wallet.
Use the Smart account
Now that you have connected your smart account to your app, you can use it to send transactions and interact with smart contracts.
Deploy a NFT Drop contract from the explore page or build your own ERC 721 compatible contract using the Solidity SDK.
Use the dashboard to upload some NFTs that we will claim with our smart account.
To claim an NFT, we will use the useContract hook to fetch the contract and the Web3Button component to send the transaction.
Bonus: Add a Session Key
To add a session key to your smart wallet, you can use the
useCreateSessionKey
hook to add a session key to the currently connected smart wallet:Conclusion
In this guide, we learned how to connect users to a React app using two methods:
- With the Connect Wallet component.
- With a custom UI component via the
useConnect
hook. - Low-level control with
ThirdwebSDKProvider
and the Wallet SDK.
Take a look at the GitHub Repository for the full source code!