Swap Manager
The SwapManager contract provides a higher-level swap interface that validates trader authorization before routing swaps through the Soroswap router. It serves as an intermediary between authorized traders and the vault’s swap functionality.Purpose
While the vault contract has built-in swap functions, the SwapManager provides additional capabilities:Trader Validation
Verifies that the caller is an authorized trader for the target vault before executing any swap.
Swap Estimation
Provides pre-trade estimates by querying the Soroswap router without executing a swap.
Multi-Hop Routing
Supports custom token paths with up to 5 hops, with validation against duplicate consecutive tokens and gas exhaustion.
Audit Trail
Emits
SwapEvent for every executed swap, providing a complete audit trail with vault, trader, tokens, and amounts.Functions
Swap Estimation
Swap Estimation
| Function | Parameters | Returns |
|---|---|---|
get_swap_estimate | vault, trader, token_in, token_out, amount_in | i128 (estimated output) |
get_multi_swap_estimate | vault, trader, amount_in, path: Vec<Address> | i128 (estimated output) |
Exact Input Swaps
Exact Input Swaps
| Function | Parameters | Returns |
|---|---|---|
swap_exact_tokens_for_tokens | vault, trader, token_in, token_out, amount_in, amount_out_min, deadline | i128 (output amount) |
multi_swap_exact_in | vault, trader, amount_in, amount_out_min, path: Vec<Address>, deadline | i128 (output amount) |
Exact Output Swaps
Exact Output Swaps
| Function | Parameters | Returns |
|---|---|---|
swap_tokens_for_exact_tokens | vault, trader, token_in, token_out, amount_out, amount_in_max, deadline | i128 (input amount used) |
multi_swap_exact_out | vault, trader, amount_out, amount_in_max, path: Vec<Address>, deadline | i128 (input amount used) |
Approval Flow
When executing a swap, the SwapManager coordinates token approvals through the vault:Trader Verification
The SwapManager verifies the caller is an authorized trader for the specified vault. If the caller is the SwapManager itself (e.g., called by OrderManager), no additional auth is needed.
Token Approval
Calls
vault.approve_for_swap() to authorize the Soroswap router to spend the vault’s tokens. Approvals expire after ~300 ledgers (~5 minutes).Path Validation
Multi-hop swap paths are validated to prevent abuse:- Minimum length: Path must contain at least 2 tokens
- Maximum length: Path is limited to 5 tokens (hops) to prevent gas exhaustion
- No consecutive duplicates: Adjacent tokens in the path must be different
How It Differs from Direct Vault Swaps
| Feature | Direct Vault Swaps | SwapManager |
|---|---|---|
| Trader validation | Built into vault | SwapManager validates before calling vault |
| Estimation | Not available | get_swap_estimate, get_multi_swap_estimate |
| Audit events | SwapEvent from vault | Additional SwapEvent from SwapManager |
| Used by | Manual traders | OrderManager (keeper execution), manual traders |

