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.
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, simply create a new Node.js project and install the
thirdwebpackage.Create a
.envfile and add the following:Create an
index.tsfile where we'll write our script.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 an In-App Wallet or just a private key and is used as a way to 'sign in' to the smart account.
To create a personal wallet key, we are going to use the
privateKeyAccount, which we need to import from thethirdweb/walletspackage.Creating the Smart account
Now, let's create a smart account using the SmartWallet class from the
@thirdweb-dev/walletspackage. To do this, we need to pass aSmartWalletConfigobject to the constructor. This object contains the following properties:chain: the chain that the smart account will be deployed on.sponsorGas: whether the smart account should have sponsored transactions or not.
Once we have created the config and instantiated the
SmartWalletclass, we can connect the personal wallet to the smart account using theconnectmethod.Using the Smart Account
Now that we have created a smart account object and connected it, we can use it to perform onchain actions gaslessly.
In this example, we will claim a NFT using the
claimTomethod and then send the transaction using thesendTransactionmethod.We have also passed our
secretKeyto 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
balanceandgetAddressmethods.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 and claimed an ERC20 token!
Conclusion
In this guide, we have learned how to use the wallet SDK with the TypeScript SDK to create a smart account and claim an NFT.