Using Account abstraction in Typescript
By using the wallet SDK alongside the TypeScript SDK, you can use smart accounts in your applications easily.
Example Use Cases
The wallet SDK with the TypeScript SDK is primarily used when creating a backend for your application or when creating a node script. In this guide, we will be using the wallet SDK to create a Node script but the logic for creating a backend is the same.
If you are working in a React environment, you are recommended to follow this guide.
Deploy an Account Factory
Deployable via the explore page or build your own ERC 4337 compatible factory contract using the Solidity SDK. hirdweb offers the following three different kinds of account factories:
Read about the differences between the three factory 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 infrastructure on mainnet you will also need to create an account and add a payment method.
Create a Node Script
To use smart accounts in a node script, we are going to setup our project using the CLI create command. Open your terminal and run:
When promted, select/input the following options:
- A name for the project
EVM
as the blockchainNode.js
as the environmentTypeScript
as the language
This will create a repository. Open it in your code editor.
Create a
.env
file and add the following:Open the
index.ts
file and delete the starter script, we won't need it.Creating the Personal Wallet Key
This smart account is unlocked by a 'key' - a personal wallet. This key can be anything from a MetaMask wallet or even a Local Wallet and is used as a way to 'sign in' to the wallet.
First install the wallets package if you haven't already:
To create a personal wallet key, we are going to use the Local Wallet, which we need to import from the
@thirdweb-dev/wallets
package, and store it in an ecrypted JSON file.Creating the Smart account
Now, let's create a smart account using the SmartWallet class from the
@thirdweb-dev/wallets
package. To do this, we need to pass aSmartWalletConfig
object to the constructor. This object contains the following properties:chain
: the chain that the smart account will be deployed on.factoryAddress
: the address of the account factory contract.secretKey
: the secret key that we created earlier.gasless
: whether the smart account should have sponsored transactions or not.
Once we have created the config and instantiating the
SmartWallet
class, we can connect the personal wallet to the smart account using theconnect
method.To view all of the methods on the
SmartWallet
class, view the Wallet SDK documentation.Instantiate the thirdweb SDK
Now that we have created a smart account object and connected it, we can instantiate the thirdweb SDK with the smart account using the
fromWallet
method.We have also passed our
secretKey
to the SDK so that we can use the bundler and paymaster.We have also added some helpful logs to view the smart account address and balance using the associated
balance
andgetAddress
methods.Using the Smart Account
In order to follow this specific example, you will need the following setup:
- Deploy a token drop contract deployed on Goerli.
- Claim conditions set so that tokens can be claimed
Now let's use this smart account to claim an ERC20 token from a token drop contract!
First, we will get the contract instance of an ERC20 token drop contract using the
getContract
method. Then, we will get the balance of the ERC20 token using thebalance
method and log it to the console. Finally, we will claim 1 ERC20 token using theclaim
method and log the transaction hash to the console.Run the Script
To run the script, run the following command:
As you can see in the terminal output, upon claiming the token, the smart account is deployed. This is because smart account contracts are deployed when the first transaction is initiated.
We have successfully deployed a smart account using our factory contract and claimed an ERC20 token!
Conclusion
In this guide, we have learnt how to use the wallet SDK with the TypeScript SDK to create a smart account and claim an ERC20 token.
Take a look at the GitHub Repository for the full source code!