Skip to main content

Description

The getAccounts() method retrieves all DSA (DeFi Smart Account) instances that are owned by a specific authority address. This is useful for discovering all DSA accounts that a user controls.

Syntax

await dsa.getAccounts(authority: string)

Parameters

authority
string
required
The ethereum address or ENS name (e.g., “vitalik.eth”) of the authority whose DSA accounts you want to retrieve. If not provided, it will use the currently connected wallet address.

Returns

Returns a promise that resolves to an array of account objects. Each account object contains:

Example Usage

Get Accounts by Address

import DSA from "dsa-connect";
import Web3 from "web3";

const web3 = new Web3(window.ethereum);
const dsa = new DSA(web3, 1); // 1 for Ethereum mainnet

// Get all DSA accounts for a specific address
const accounts = await dsa.getAccounts("0x1234...");

console.log(`Found ${accounts.length} DSA accounts:`);
accounts.forEach((account) => {
  console.log(`ID: ${account.id}`);
  console.log(`Address: ${account.address}`);
  console.log(`Version: ${account.version}`);
  console.log("---");
});

Get Accounts by ENS Name

// Get all DSA accounts for an ENS name
const accounts = await dsa.getAccounts("vitalik.eth");

console.log(`DSAs owned by vitalik.eth:`, accounts);

Get Accounts for Connected Wallet

// Get accounts for the currently connected wallet
const userAddress = (await web3.eth.getAccounts())[0];
const accounts = await dsa.getAccounts(userAddress);

if (accounts.length === 0) {
  console.log("No DSA accounts found. Create one with dsa.build()");
} else {
  console.log(`You have ${accounts.length} DSA account(s)`);
}

Select and Set Active Account

// Get all accounts and set the first one as active
const accounts = await dsa.getAccounts("0x...");

if (accounts.length > 0) {
  // Set the first account as the active instance
  await dsa.setInstance(accounts[0].id);
  console.log(`Active DSA set to ID: ${accounts[0].id}`);
  console.log(`Active DSA address: ${accounts[0].address}`);
}

Filter by Version

// Get all accounts and filter by version
const accounts = await dsa.getAccounts("0x...");

// Get only version 2 accounts (recommended)
const v2Accounts = accounts.filter((account) => account.version === 2);

console.log(`Found ${v2Accounts.length} version 2 DSA accounts`);

Display Account Details

const accounts = await dsa.getAccounts("0x...");

accounts.forEach((account, index) => {
  console.log(`\nAccount ${index + 1}:`);
  console.log(`  ID: ${account.id}`);
  console.log(`  Address: ${account.address}`);
  console.log(`  Version: ${account.version}`);
  console.log(`  Type: ${account.version === 2 ? "Latest" : "Legacy"}`);
});

Return Value Example

[
  {
    id: 12345,
    address: "0xabcd...",
    version: 2
  },
  {
    id: 12346,
    address: "0xef01...",
    version: 2
  }
]

Notes

  • The method returns an empty array if no DSA accounts are found for the specified authority
  • ENS names are automatically resolved to ethereum addresses
  • The method queries the blockchain, so it will always return the most up-to-date list of accounts
  • Authority addresses can control multiple DSA accounts
  • Each DSA account has a unique ID that can be used with setInstance() to make it the active account

Error Handling

try {
  const accounts = await dsa.getAccounts("0x...");
  
  if (accounts.length === 0) {
    console.log("No DSA accounts found for this address");
  } else {
    console.log(`Found ${accounts.length} DSA account(s)`);
  }
} catch (error) {
  console.error("Failed to fetch accounts:", error.message);
}

Common Use Cases

Check if User Has DSA

const userAddress = (await web3.eth.getAccounts())[0];
const accounts = await dsa.getAccounts(userAddress);

if (accounts.length === 0) {
  console.log("User needs to create a DSA");
  // Prompt user to call dsa.build()
} else {
  console.log("User has existing DSA accounts");
  // Set instance and proceed
  await dsa.setInstance(accounts[0].id);
}

List All DSAs in UI

const accounts = await dsa.getAccounts(userAddress);

// Display in your UI
const accountList = accounts.map((account) => ({
  label: `DSA #${account.id} (v${account.version})`,
  value: account.id,
  address: account.address,
}));

Build docs developers (and LLMs) love