Skip to main content

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.
This is experimental pilot software. See Disclaimers for details.

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

FunctionParametersReturns
get_swap_estimatevault, trader, token_in, token_out, amount_ini128 (estimated output)
get_multi_swap_estimatevault, trader, amount_in, path: Vec<Address>i128 (estimated output)
Estimation functions verify trader authorization and query the Soroswap router for expected output amounts without executing a trade.
FunctionParametersReturns
swap_exact_tokens_for_tokensvault, trader, token_in, token_out, amount_in, amount_out_min, deadlinei128 (output amount)
multi_swap_exact_invault, trader, amount_in, amount_out_min, path: Vec<Address>, deadlinei128 (output amount)
FunctionParametersReturns
swap_tokens_for_exact_tokensvault, trader, token_in, token_out, amount_out, amount_in_max, deadlinei128 (input amount used)
multi_swap_exact_outvault, trader, amount_out, amount_in_max, path: Vec<Address>, deadlinei128 (input amount used)

Approval Flow

When executing a swap, the SwapManager coordinates token approvals through the vault:
1

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.
2

Balance Check

Validates the vault has sufficient balance of the input token.
3

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).
4

Swap Execution

Routes the swap through the Soroswap router and returns the result.

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

FeatureDirect Vault SwapsSwapManager
Trader validationBuilt into vaultSwapManager validates before calling vault
EstimationNot availableget_swap_estimate, get_multi_swap_estimate
Audit eventsSwapEvent from vaultAdditional SwapEvent from SwapManager
Used byManual tradersOrderManager (keeper execution), manual traders