Guide for creating and managing DSA (DeFi Smart Account) accounts
Every user needs to create a Smart Account to interact with DeFi protocols seamlessly. DSA accounts allow developers to build extensible use-cases with maximum security and composability.
// Create DSA with a different authorityawait dsa.build({ authority: '0xMultiSigWallet...', // Use a multisig as authority origin: '0xYourDapp...', // Track your dapp as origin})
Before casting spells, you must set which DSA account to use:
// Set the active DSA instanceawait dsa.setInstance(52) // Use DSA ID 52// Verify the instanceconsole.log('Active DSA:', dsa.instance)// {// id: 52,// address: '0x...',// version: 2,// chainId: 1// }
The configured account will be used for all subsequent spell casts.
// Get user's addressconst accounts = await web3.eth.getAccounts()const userAddress = accounts[0]// Check if user has any DSA accountsconst dsaAccounts = await dsa.getAccounts(userAddress)console.log(`Found ${dsaAccounts.length} DSA accounts`)
2
Create or Select Account
let dsaIdif (dsaAccounts.length === 0) { // No DSA accounts, create one console.log('Creating new DSA account...') const txHash = await dsa.build() // Wait for confirmation await waitForTransaction(txHash) // Get the new account const updatedAccounts = await dsa.getAccounts(userAddress) dsaId = updatedAccounts[updatedAccounts.length - 1].id console.log('Created DSA ID:', dsaId)} else { // Use existing account dsaId = dsaAccounts[0].id console.log('Using existing DSA ID:', dsaId)}
3
Set Active Instance
// Set the DSA instance for subsequent operationsawait dsa.setInstance(dsaId)console.log('Active DSA:', dsa.instance.address)
4
Ready to Cast Spells
// Now you can start casting spellsconst spell = dsa.Spell()// ... add spells and cast
try { await dsa.setInstance(999)} catch (error) { if (error.message.includes('does not exist')) { console.log('DSA ID does not exist') // Create a new one await dsa.build() }}
For users with multiple accounts, let them choose which one to use:
const accounts = await dsa.getAccounts(userAddress)// Display accounts to user and let them selectconst selectedId = await userSelectAccount(accounts)await dsa.setInstance(selectedId)