Skip to main content
This guide will help you initialize DSA Connect, create a Smart Account, and execute your first transaction. By the end, you’ll have a working DSA integration.

Prerequisites

Before you begin, make sure you have:
  • Completed the Installation guide
  • A Web3 provider (MetaMask, WalletConnect, or custom provider)
  • An Ethereum address with some ETH for gas fees

Setup Web3

First, set up your Web3 instance based on your environment:
import Web3 from 'web3';

// Connect to MetaMask or other Web3 wallet
if (window.ethereum) {
  window.web3 = new Web3(window.ethereum);
  // Request account access
  await window.ethereum.request({ method: 'eth_requestAccounts' });
} else if (window.web3) {
  window.web3 = new Web3(window.web3.currentProvider);
} else {
  // Fallback to custom provider
  window.web3 = new Web3(customProvider);
}

Initialize DSA

1

Create DSA Instance

Initialize DSA with your Web3 instance and chain ID:
import DSA from 'dsa-connect';

// Simple browser initialization
const dsa = new DSA(web3);

// Or with specific chain ID (1 = Ethereum Mainnet)
const dsa = new DSA(web3, 1);
Supported Chain IDs: Mainnet (1), Polygon (137), Arbitrum (42161), Avalanche (43114), Optimism (10), Fantom (250), Base (8453), Plasma (9745), BSC (56)
2

Fetch Existing Accounts (Optional)

Check if you already have DSA accounts:
const accounts = await dsa.getAccounts("0xYourAddress");
console.log(accounts);
// [
//   {
//     id: 52,
//     address: "0x...",
//     version: 2
//   }
// ]
If you have existing accounts, set the active instance:
await dsa.setInstance(52); // Use your DSA ID
3

Create New Smart Account

If you don’t have a DSA account, create one:
// Simple creation
const txHash = await dsa.build();
console.log('DSA Created:', txHash);
await dsa.build({
  gasPrice: web3.utils.toWei('50', 'gwei'), // Custom gas price
  origin: "0xOriginAddress",                // For analytics/affiliates
  authority: "0xAuthorityAddress",          // Additional authority address
  from: "0xYourAddress",                     // Account creating the DSA
  version: 2                                 // DSA version (default: 2)
});
ParameterTypeDescription
gasPricestring/numberGas price in wei
originaddressTrack transaction origin for analytics
authorityaddressAdditional authority address
fromaddressCreator address
versionnumberDSA version (default: 2)
noncestring/numberTransaction nonce

Execute Your First Transaction

Now let’s execute a simple DeFi transaction using Spells. This example deposits ETH into Aave:
1

Create a Spell Instance

const spells = dsa.Spell();
2

Add Transaction to Spell

spells.add({
  connector: "aave",
  method: "deposit",
  args: [
    "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", // ETH token address
    "1000000000000000000",                        // 1 ETH (in wei)
    0,                                              // Default: 0
    0                                               // Default: 0
  ]
});
You can chain multiple operations in a single transaction by calling .add() multiple times.
3

Cast the Spell

// Execute the transaction
const txHash = await spells.cast();
console.log('Transaction Hash:', txHash);
await spells.cast({
  gasPrice: web3.utils.toWei('50', 'gwei'),
  value: "1000000000000000000",  // Send 1 ETH with transaction
  from: "0xYourAddress",
  nonce: 42
});

Complete Example

Here’s a complete example that deposits ETH to Aave, borrows DAI, and deposits it to Compound:
import DSA from 'dsa-connect';
import Web3 from 'web3';

// Initialize
const web3 = new Web3(window.ethereum);
const dsa = new DSA(web3);

// Set your DSA account
await dsa.setInstance(52);

// Create spell with multiple operations
const spells = dsa.Spell();

// 1. Deposit 1 ETH to Aave
spells.add({
  connector: "aave",
  method: "deposit",
  args: [
    "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
    "1000000000000000000", // 1 ETH
    0,
    0
  ]
});

// 2. Borrow 100 DAI from Aave
spells.add({
  connector: "aave",
  method: "borrow",
  args: [
    "0x6B175474E89094C44Da98b954EedeAC495271d0F", // DAI
    "100000000000000000000",                       // 100 DAI
    0,
    0
  ]
});

// 3. Deposit borrowed DAI to Compound
spells.add({
  connector: "compound",
  method: "deposit",
  args: [
    "0x6B175474E89094C44Da98b954EedeAC495271d0F", // DAI
    "100000000000000000000",                       // 100 DAI
    0,
    0
  ]
});

// Execute all operations in one transaction
const txHash = await spells.cast();
console.log('Complex transaction executed:', txHash);
Make sure your DSA account has sufficient ETH balance before executing these transactions.

Helper Methods

DSA Connect provides several utility methods:
// Get account count
const count = await dsa.count("0xYourAddress");

// Get max uint256 value (for approvals)
const maxValue = dsa.maxValue;

// Set origin for analytics
dsa.setOrigin("0xOriginAddress");

// Encode spells without executing
const encodedData = await dsa.encodeSpells({ spells });

// Estimate gas for transaction
const gasEstimate = await spells.estimateCastGas();

Next Steps

Explore Connectors

Learn about available DeFi protocol integrations

Advanced Casting

Master complex multi-protocol transactions

API Reference

Explore the complete DSA Connect API

Multi-Chain Guide

Deploy across multiple EVM chains

Build docs developers (and LLMs) love