const MaxQuotePercentage, MinQuotePercentage, PERCENTAGE_DENOMINATOR
QuoteConstraints defines the valid range for swap quote percentages
package v1 handles token swaps through GnoSwap liquidity pools.
package v1 handles token swaps through GnoSwap liquidity pools.
The router provides user-facing swap functions with slippage protection, multi-hop routing, and automatic GNOT wrapping/unwrapping. It supports both exact input and exact output swap modes.
All swap functions include deadline checks and minimum output validation to protect users from unfavorable price movements.
QuoteConstraints defines the valid range for swap quote percentages
ErrorMessages for DrySwapRoute operations
swap can be done by multiple pools to separate each pool, we use POOL_SEPARATOR
1const (
2 _ SwapDirection = iota
3 // Forward indicates a swap processing direction from the first pool to the last pool.
4 // Used primarily for exactIn swaps where the input amount is known.
5 Forward
6
7 // Backward indicates a swap processing direction from the last pool to the first pool.
8 // Used primarily for exactOut swaps where the output amount is known and input amounts
9 // Need to be calculated in reverse order.
10 Backward
11) 1const (
2 Unknown SwapType = rawUnknown
3 // ExactIn represents a swap type where the input amount is exact and the output amount may vary.
4 // Used when a user wants to swap a specific amount of input tokens.
5 ExactIn SwapType = rawExactIn
6
7 // ExactOut represents a swap type where the output amount is exact and the input amount may vary.
8 // Used when a user wants to swap a specific amount of output tokens.
9 ExactOut SwapType = rawExactOut
10)BuildSingleHopPath creates a single-hop route path string. Format: "tokenA:tokenB:fee"
Parameters: - tokenA: input token address - tokenB: output token address - fee: pool fee (e.g., 500, 3000, 10000)
Returns: - string: formatted single-hop route path
Example:
NewExactInParams creates a new ExactInParams instance.
NewExactOutParams creates a new ExactOutParams instance.
NewExactOutSwapOperation creates a new exact-out swap operation.
NewRouteParser creates a new route parser instance.
DryMultiSwapExecutor implements MultiSwapExecutor for dry run simulations.
DrySwapExecutor implements SwapExecutor for dry swaps.
ExactInParams contains parameters for exact input swaps.
ExactOutParams contains parameters for exact output swaps.
ExactOutSwapOperation handles swaps where the output amount is specified.
MultiSwapExecutor defines the interface for multi-hop swap operation execution.
1type MultiSwapProcessor struct {
2 executor MultiSwapExecutor
3 direction SwapDirection
4 router *routerV1
5 isSimulate bool
6 // payer is the address used to identify the user. For real swaps it is the
7 // PreviousRealm address; for dry-run paths it is resolved at the entry point.
8 payer address
9}MultiSwapProcessor handles the execution flow for multi-hop swaps.
RealMultiSwapExecutor implements MultiSwapExecutor for actual swap operations.
RealSwapExecutor implements SwapExecutor for actual swaps.
RouteParser handles parsing and validation of routes and quotes
ParseRoutes parses route and quote strings into slices and validates them.
ValidateQuoteSum ensures all quotes add up to 100%.
ValidateRoutesAndQuotes ensures routes and quotes meet required criteria.
1type SingleSwapParams struct {
2 tokenIn string // token to spend
3 tokenOut string // token to receive
4 fee uint32 // fee of the pool used to swap
5
6 // Amount specified for the swap:
7 // - Positive: exact input amount (tokenIn)
8 // - Negative: exact output amount (tokenOut)
9 amountSpecified int64
10
11 sqrtPriceLimitX96 *u256.Uint // sqrtPriceLimitX96 for the swap, empty string or zero string means no limit
12}SingleSwapParams contains parameters for executing a single pool swap. It represents the simplest form of swap that occurs within a single liquidity pool.
Fee returns the pool fee tier.
SqrtPriceLimitX96 returns the sqrtPriceLimitX96 for the swap. If sqrtPriceLimitX96 is empty string, it will return zero.
TokenIn returns the input token address.
TokenOut returns the output token address.
SwapCallbackData contains the callback data required for swap execution. This type is used to pass necessary information during the swap callback process, ensuring proper token transfers and pool data updates.
SwapDirection represents the direction of swap execution in multi-hop swaps. It determines whether swaps are processed in forward order (first to last pool) or backward order (last to first pool).
SwapExecutor defines the interface for executing swaps.
SwapParams contains parameters for executing a multi-hop swap operation.
Fee returns the pool fee tier.
Recipient returns the recipient address.
TokenIn returns the input token address.
TokenOut returns the output token address.
SwapParamsI defines the common interface for swap parameters.
SwapProcessor handles the execution of swap operations
AddSwapResults safely adds swap result amounts, checking for overflow.
Parameters:
Returns:
ProcessMultiSwap handles a multi-hop swap simulation.
Parameters:
Returns:
1func (p *SwapProcessor) ProcessSingleSwap(route string, amountSpecified int64) (amountIn, amountOut int64, err error)ProcessSingleSwap handles a single-hop swap simulation.
Parameters:
Returns:
ValidateSwapResults checks if the swap results meet the required constraints.
Parameters:
Returns:
SwapResult encapsulates the outcome of a swap operation.
1type SwapRouteParams struct {
2 inputToken string
3 outputToken string
4 routeArr string
5 quoteArr string
6 deadline int64
7 typ SwapType
8 exactAmount int64 // amountIn for ExactIn, amountOut for ExactOut
9 limitAmount int64 // amountOutMin for ExactIn, amountInMax for ExactOut
10 sqrtPriceLimitX96 *u256.Uint // if sqrtPriceLimitX96 is zero string, it will be set to MIN_PRICE or MAX_PRICE
11}SwapRouteParams contains all parameters needed for swap route execution
when exact out, calculate amount to fetch from pool including router fee
SwapValidator provides validation methods for swap operations