Skip to main content

Overview

The add() method adds a connector function call to the spell sequence. Multiple spells can be chained together to create complex multi-protocol transactions that execute atomically.

Syntax

spells.add(spell)

Parameters

spell
object
required
The spell object containing connector, method, and arguments

Returns

spells
Spells
Returns the spell instance to enable method chaining

Examples

Single Spell

Deposit 1 ETH to Aave:
const spells = dsa.Spell()

spells.add({
  connector: "aave",
  method: "deposit",
  args: [
    "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", // ETH address
    "1000000000000000000", // 1 ETH (10^18 wei)
    0,
    0
  ]
})

await spells.cast()

Chaining Multiple Spells

Deposit ETH to Aave and borrow DAI:
const spells = dsa.Spell()

spells
  .add({
    connector: "aave",
    method: "deposit",
    args: [
      "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
      "1000000000000000000", // 1 ETH
      0,
      0
    ]
  })
  .add({
    connector: "aave",
    method: "borrow",
    args: [
      "0x6B175474E89094C44Da98b954EedeAC495271d0F", // DAI address
      "100000000000000000000", // 100 DAI
      0,
      0
    ]
  })

await spells.cast()

Multi-Protocol Transaction

Deposit to Aave, borrow DAI, and supply DAI to Compound:
const spells = dsa.Spell()

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

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

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

await spells.cast()

Validation

The add() method performs validation on the spell object:
  • connector: Must be defined, throws error if missing
  • method: Must be defined, throws error if missing
  • args: Must be defined, throws error if missing
// This will throw an error
spells.add({
  connector: "aave"
  // Missing method and args
})
// Error: method not defined.

Common Connector Arguments

Aave Deposit

args: [
  token,      // Token address (0xeee...eee for ETH)
  amt,        // Amount in wei
  getId,      // ID to retrieve amount (0 for direct value)
  setId       // ID to store amount (0 if not storing)
]

Aave Borrow

args: [
  token,      // Token address to borrow
  amt,        // Amount to borrow in wei
  rateMode,   // Interest rate mode (1 for stable, 2 for variable)
  getId,      // ID to retrieve amount
  setId       // ID to store amount
]

Compound Deposit

args: [
  token,      // Token address
  amt,        // Amount in wei
  getId,      // ID to retrieve amount
  setId       // ID to store amount
]

Best Practices

  1. Check Balances: Ensure your DSA has sufficient balance before adding deposit or borrow spells
  2. Use Method Chaining: Chain multiple add() calls for cleaner code
  3. Atomic Execution: All spells in a sequence execute atomically - if one fails, all fail
  4. Connector Names: Use lowercase connector names consistently
  5. Amount Precision: Always use wei amounts (multiply by 10^18 for ETH/DAI)

Error Handling

try {
  spells.add({
    connector: "aave",
    method: "deposit",
    args: [
      "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
      "1000000000000000000",
      0,
      0
    ]
  })
  
  await spells.cast()
} catch (error) {
  console.error('Spell execution failed:', error)
}

See Also

Build docs developers (and LLMs) love