Skip to main content
This guide will help you set up the DSA Connect SDK in both browser and Node.js environments.

Installation

npm install dsa-connect

Browser Setup

1

Initialize Web3

First, initialize a Web3 instance connected to the user’s wallet:
// Using MetaMask or other injected wallet
if (window.ethereum) {
  window.web3 = new Web3(window.ethereum)
  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)
}
2

Initialize DSA

Create a DSA instance with your Web3 instance:
import { DSA } from 'dsa-connect'

const dsa = new DSA(web3)
In browser mode, the SDK automatically uses the connected wallet to sign transactions.
3

Specify Chain ID (Optional)

By default, DSA Connect uses Ethereum Mainnet (Chain ID 1). To use a different chain:
// Polygon
const dsa = new DSA(web3, 137)

// Arbitrum
const dsa = new DSA(web3, 42161)

// Optimism
const dsa = new DSA(web3, 10)
Supported Chain IDs:
  • 1 - Ethereum Mainnet
  • 137 - Polygon
  • 42161 - Arbitrum
  • 43114 - Avalanche
  • 10 - Optimism
  • 250 - Fantom
  • 8453 - Base
  • 9745 - Plasma
  • 56 - BSC

Node.js Setup

1

Import Required Modules

const Web3 = require('web3')
const DSA = require('dsa-connect')
2

Initialize Web3 with Provider

const web3 = new Web3(
  new Web3.providers.HttpProvider('https://eth-mainnet.alchemyapi.io/v2/YOUR-API-KEY')
)
3

Initialize DSA with Node Mode

In Node.js, you must specify mode: 'node' and provide a private key:
const dsa = new DSA({
  web3: web3,
  mode: 'node',
  privateKey: process.env.PRIVATE_KEY
}, 1) // Chain ID
Never hardcode your private key. Always use environment variables or secure key management solutions.

Simulation Mode

For testing and simulating transactions without actually signing them:
const dsa = new DSA({
  web3: web3,
  mode: 'simulation',
  publicKey: '0x...'
}, 1)
Simulation mode is useful for estimating gas costs and testing transaction flows without requiring a private key.

Configuration Options

DSAConfig Types

type DSAConfig = {
  web3: Web3
  mode?: 'browser' // default
}

Advanced Options

const dsa = new DSA(
  web3,
  chainId,
  {
    skipChainIdValidation: true // Skip automatic chain ID validation
  }
)

Setting the Origin

Set an origin address for analytics and affiliate tracking:
dsa.setOrigin('0xYourOriginAddress')
The origin address is used for:
  • On-chain analytics
  • Affiliate tracking
  • Transaction attribution

Refreshing Chain ID

If the user switches networks, refresh the chain ID:
await dsa.refreshChainId()

Complete Example

import { DSA } from 'dsa-connect'
import Web3 from 'web3'

// Initialize Web3
if (window.ethereum) {
  await window.ethereum.request({ method: 'eth_requestAccounts' })
  const web3 = new Web3(window.ethereum)

  // Initialize DSA
  const dsa = new DSA(web3, 1)

  // Set origin for analytics
  dsa.setOrigin('0xYourOriginAddress')

  console.log('DSA initialized successfully')
}

Next Steps

Creating Accounts

Learn how to create and manage DSA accounts

Casting Spells

Start composing DeFi transactions

Build docs developers (and LLMs) love