Using Smart Wallet in Typescript
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
Choose the right smart wallet setup for your app. thirdweb offers the following three different kinds of smart wallets:
Read about the differences between the three smart wallet types here.
Create an API key
To use the smart wallet 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 smart wallet infrastructure on mainnet you will also need to create an account and add a payment method.
Create a Node Script
To use smart wallets 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
EVMas the blockchain
Node.jsas the environment
TypeScriptas the language
This will create a repository. Open it in your code editor.
.envfile and add the following:
index.tsfile and delete the starter script, we won't need it.
Creating the Personal Wallet Key
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/walletspackage, and store it in an ecrypted JSON file.
Creating the Smart Wallet
Now, let's create a smart wallet using the SmartWallet class from the
@thirdweb-dev/walletspackage. To do this, we need to pass a
SmartWalletConfigobject to the constructor. This object contains the following properties:
chain: the chain that the smart wallet will be deployed on.
factoryAddress: the address of the account factory contract.
secretKey: the secret key that we created earlier.
gasless: whether the smart wallet should be gasless or not.
Once we have created the config and instantiating the
SmartWalletclass, we can connect the personal wallet to the smart wallet using the
To view all of the methods on the
SmartWalletclass, view the Wallet SDK documentation.
Instantiate the thirdweb SDK
Now that we have created a smart wallet object and connected it, we can instantiate the thirdweb SDK with the smart wallet using the
We have also passed our
secretKeyto the SDK so that we can use the smart wallet bundler and paymaster.
We have also added some helpful logs to view the smart wallet address and balance using the associated
Using the Smart Wallet
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 wallet 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
getContractmethod. Then, we will get the balance of the ERC20 token using the
balancemethod and log it to the console. Finally, we will claim 1 ERC20 token using the
claimmethod 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 wallet is deployed. This is because smart account contracts are deployed when the first transaction is initiated.
We have successfully deployed a smart wallet using our factory contract and claimed an ERC20 token!