Overview
AnteHandlers are executed before message execution in the transaction lifecycle. They validate transaction properties and ensure the transaction meets all requirements before state changes occur.AnteHandler Signature
ctx: The current context containing state and metadatatx: The transaction to validatesimulate: Whether this is a simulation run
Default AnteHandler Chain
The default AnteHandler inx/auth chains multiple decorators:
x/auth/ante/ante.go
Key Decorators
SetUpContextDecorator
Must be the first decorator. Sets up the gas meter and handles out-of-gas panics:x/auth/ante/setup.go
ValidateBasicDecorator
Validates basic transaction properties:x/auth/ante/basic.go
DeductFeeDecorator
Deducts transaction fees from the fee payer:x/auth/ante/fee.go
SigVerificationDecorator
Verifies all transaction signatures and handles unordered transactions:x/auth/ante/sigverify.go
Unordered Transactions
As of v0.53.0, the SDK supports unordered transactions with TTL-based nonces:Custom AnteHandlers
You can create custom AnteHandlers by implementing the decorator pattern:HandlerOptions
TheHandlerOptions struct configures the AnteHandler:
Best Practices
- Order Matters:
SetUpContextDecoratormust be first,SetPubKeyDecoratorbefore signature verification - Gas Metering: Always set up gas meters properly to prevent infinite gas attacks
- Simulate Mode: Handle simulation mode appropriately in custom decorators
- Error Handling: Return clear, actionable errors for validation failures
- Performance: Keep decorator logic lightweight as it runs for every transaction
- Unordered Txs: Use sequence 0 for unordered transactions with TTL timeout
See Also
- PostHandler - Post-transaction processing
- Transaction Lifecycle - Complete transaction flow
- Gas and Fees - Gas metering details