Overview
Thecast() method executes the spell sequence that has been built using add(). It submits the transaction to the blockchain, executing all spells atomically in a single transaction.
Syntax
Parameters
Optional parameters to configure the transaction
Returns
Transaction object containing the transaction hash and receipt
Examples
Basic Cast
Execute spells with default parameters:Cast with ETH Value
Send ETH along with the transaction:Cast with Gas Configuration
Configure gas price for faster confirmation (Node.js):Cast with EIP-1559 Gas
Use EIP-1559 transaction format:Cast with Callbacks
Monitor transaction confirmation:Multi-Protocol Complex Transaction
Behavior
Empty Spell Sequence
If no spells have been added,cast() will log a message and return without executing:
Atomic Execution
All spells in the sequence execute atomically. If any spell fails, the entire transaction reverts:Console Output
Thecast() method logs configuration information:
Error Handling
Node.js vs Browser
Browser Mode
In browser mode,cast() triggers the user’s Web3 wallet (like MetaMask) to confirm the transaction:
Node.js Mode
In Node.js mode, transactions are automatically signed with the provided private key:Best Practices
- Check Balances First: Ensure your DSA has sufficient balance before casting
- Estimate Gas: Use
estimateCastGas()before casting to verify gas requirements - Handle Errors: Always wrap
cast()in try-catch blocks - Monitor Confirmations: Use callbacks to track transaction status
- Set Appropriate Gas: In Node.js mode, always specify gas price and limit
- Test First: Test spell sequences on testnets before mainnet
Related Methods
- estimateCastGas(): Estimate gas before casting
- encodeCastABI(): Get transaction calldata without executing
- encodeSpells(): Get encoded spell data
See Also
- Spell Class - Complete Spell class documentation
- add() - Add spells to the sequence
- setInstance() - Set the DSA instance to cast to