Skip to main content

Overview

The Spell class is created using the dsa.Spell() method and provides a fluent interface for building and executing transaction sequences across DeFi protocols. Spells denote a sequence of connector functions that achieve a specific use case.

Creating a Spell Instance

Create a spell instance to start building transaction sequences:
const spells = dsa.Spell()
The Spell() method returns an extended Spells class instance with additional methods for casting and encoding transactions.

Available Methods

The Spell instance provides the following methods:

add()

Add a connector function to the spell sequence. Returns the spell instance for method chaining. View detailed add() documentation

cast()

Execute the spell sequence as a transaction on the blockchain. Returns the transaction hash. View detailed cast() documentation

estimateCastGas()

Estimate the gas required to execute the spell sequence.
const gasEstimate = await spells.estimateCastGas()
params
object
Optional parameters for gas estimation
gasEstimate
string
Estimated gas units required for the transaction

encodeCastABI()

Encode the spell sequence into transaction calldata without executing it.
const encodedData = await spells.encodeCastABI()
params
object
Optional parameters for ABI encoding
encodedData
string
Hex-encoded transaction calldata

encodeSpells()

Encode the spell data into the internal format used by DSA contracts.
const encodedSpells = await spells.encodeSpells()
encodedSpells
object
Object containing targets and encoded spell data

convertToAvocadoActions()

Convert the spell sequence to Avocado-compatible actions format.
const avocadoActions = await spells.convertToAvocadoActions()
avocadoActions
array
Array of actions in Avocado format

Example Usage

Basic Spell Chain

const spells = dsa.Spell()

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

const txHash = await spells.cast()

Complex Multi-Protocol Transaction

const spells = dsa.Spell()

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

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

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

// Execute the transaction
const txHash = await spells.cast({
  gasPrice: web3.utils.toWei('50', 'gwei'),
  value: "1000000000000000000" // Send 1 ETH with transaction
})

Properties

data
Spell[]
Array of spell objects added to the instance

Notes

  • The Spell instance uses method chaining for a fluent API
  • All methods except cast() return the spell instance for chaining
  • Spells are not executed until cast() is called
  • The spell sequence is atomic - either all operations succeed or all fail
  • Make sure your DSA has sufficient balance before casting spells

Build docs developers (and LLMs) love