Why Approvals Are Needed
ERC20 tokens require explicit approval before a smart contract can transfer them on your behalf. This is a security feature that prevents contracts from moving your tokens without permission.Native tokens like ETH don’t require approval - Only ERC20 tokens need the approval step.
Two-Transaction Flow
When depositing ERC20 tokens, LeoKit returns two unsigned transactions:- Approval Transaction - Authorizes the protocol contract to spend your tokens
- Deposit Transaction - Transfers tokens to initiate the swap
Example: ETH USDC → BTC
Let’s swap 2000 USDC on Ethereum to BTC.Step 1: Get Quote
Step 2: Generate Deposit (Returns 2 Transactions)
Step 3: Sign & Send Both Transactions
Understanding Approval Amounts
LeoKit approves the exact swap amount by default. This is more secure than infinite approvals but requires a new approval for each swap.Exact Amount Approval (Default)
- More secure (limits exposure)
- User-friendly (clear intent)
- Requires approval for every swap
- Higher gas costs over time
Infinite Approval (Not Recommended)
Some protocols support infinite approvals, but LeoKit doesn’t use them for security reasons:USDT Edge Case: Approval Reset
If you’re swapping USDT and have an existing approval, LeoKit will return three transactions:- Reset Approval - Set allowance to 0
- New Approval - Set allowance to swap amount
- Deposit - Execute the swap
Checking Current Allowance
You can check if an approval already exists using ethers.js:Gas Optimization
Approvals cost gas. Here are strategies to minimize costs:Batch Multiple Swaps
Batch Multiple Swaps
If you’re doing multiple swaps of the same token, consider approving a larger amount once instead of per-swap approvals. Note: LeoKit doesn’t currently support this, but you can manually approve higher amounts.
Use EIP-2612 Permits (Advanced)
Use EIP-2612 Permits (Advanced)
Some tokens support gasless approvals via EIP-2612 permits. This requires signature-based approvals instead of transactions. Not yet supported by LeoKit.
Monitor Gas Prices
Monitor Gas Prices
Submit approval transactions during low gas periods (typically weekends) to save costs.
Error Handling
Common approval errors and solutions:Best Practices
Always wait for approval confirmation - Don’t send the deposit transaction until the approval is confirmed on-chain.
Handle USDT specially - Check for 3-transaction responses when swapping USDT.
Show clear UI feedback - Inform users they’ll need to sign 2+ transactions before starting.
Estimate total gas costs - Calculate approval + deposit gas costs and show users upfront.
Support transaction resumption - If approval succeeds but deposit fails, allow users to retry just the deposit.
Next Steps
Complete Swap Flow
See the full swap workflow from quote to completion
Error Debugging
Debug failed approvals using trace IDs