Overview
Hooks allow you to:- Execute pre-order logic: Approve tokens, wrap/unwrap assets, or setup flash loans
- Execute post-order logic: Stake tokens, compound yields, or trigger other contracts
- Chain complex operations: Combine multiple DeFi protocols in a single transaction
- Integrate with dApps: Enable seamless integration with other protocols
Hook Structure
Hooks are defined in the order’s app data and consist of:Adding Hooks to Orders
Basic Hook Setup
Using TradingSdk with Hooks
Pre-Hooks
Pre-hooks execute before the order settlement.Token Approval
Wrap Native Token
Flash Loan Setup
Post-Hooks
Post-hooks execute after the order settlement.Stake Tokens
Deposit to Yield Protocol
Unwrap Wrapped Token
Complete Example
Here’s a complete example with pre and post hooks:Gas Limit Considerations
Recommended Gas Limits
Estimating Gas
Hook Best Practices
Test hooks thoroughly
Test hooks thoroughly
Always test your hooks on testnets before production:
Handle failures gracefully
Handle failures gracefully
Consider whether your hook should fail the entire order:
- Critical hooks (approvals): Must succeed
- Optional hooks (staking): Can fail without breaking order
Minimize hook complexity
Minimize hook complexity
Keep hooks simple to reduce gas costs and failure risk:
Use dappId for tracking
Use dappId for tracking
Always include a
dappId to identify your hooks:Use Cases
1. Automated Yield Farming
Swap and immediately deposit into yield protocol:2. Leveraged Trading
Flash loan, swap, and repay:3. Cross-Chain Preparation
Prepare tokens for bridging:Debugging Hooks
Check Hook Execution
Limitations
- Hooks must complete within their specified gas limit
- Hooks are executed in order (pre-hooks → order → post-hooks)
- Failed hooks will cause the entire transaction to revert
- Hooks cannot be updated after order submission
- Maximum gas for all hooks combined is subject to block gas limit
Next Steps
Partner Fee
Configure partner fee collection
Cross-Chain Bridging
Execute cross-chain swaps