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
Parameters
The spell object containing connector, method, and arguments The connector name identifying which DeFi protocol to interact with (e.g., “aave”, “compound”, “uniswap”)
The method name to call on the connector (e.g., “deposit”, “borrow”, “withdraw”)
Array of arguments to pass to the connector method. The specific arguments depend on the connector and method being called.
Returns
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
Check Balances : Ensure your DSA has sufficient balance before adding deposit or borrow spells
Use Method Chaining : Chain multiple add() calls for cleaner code
Atomic Execution : All spells in a sequence execute atomically - if one fails, all fail
Connector Names : Use lowercase connector names consistently
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