type.gno
7.53 Kb · 261 lines
1package router
2
3import (
4 u256 "gno.land/p/gnoswap/uint256/v1"
5
6 ufmt "gno.land/p/nt/ufmt/v0"
7)
8
9const (
10 rawUnknown = "UNKNOWN"
11 rawExactIn = "EXACT_IN"
12 rawExactOut = "EXACT_OUT"
13
14 zeroAddress = address("")
15)
16
17type SwapType string
18
19const (
20 Unknown SwapType = rawUnknown
21 // ExactIn represents a swap type where the input amount is exact and the output amount may vary.
22 // Used when a user wants to swap a specific amount of input tokens.
23 ExactIn SwapType = rawExactIn
24
25 // ExactOut represents a swap type where the output amount is exact and the input amount may vary.
26 // Used when a user wants to swap a specific amount of output tokens.
27 ExactOut SwapType = rawExactOut
28)
29
30// trySwapTypeFromStr attempts to convert a string into a SwapType.
31// It validates and converts string representations of swap types into their corresponding enum values.
32func trySwapTypeFromStr(swapType string) (SwapType, error) {
33 switch swapType {
34 case rawExactIn:
35 return ExactIn, nil
36 case rawExactOut:
37 return ExactOut, nil
38 default:
39 return "", ufmt.Errorf("unknown swapType: expected ExactIn or ExactOut, got %s", swapType)
40 }
41}
42
43// String returns the wire-format string for a SwapType.
44//
45// Returns:
46// - swapType: EXACT_IN or EXACT_OUT for known values, or an empty string for Unknown
47func (s SwapType) String() string {
48 switch s {
49 case ExactIn:
50 return rawExactIn
51 case ExactOut:
52 return rawExactOut
53 default:
54 return ""
55 }
56}
57
58// SingleSwapParams contains parameters for executing a single pool swap.
59// It represents the simplest form of swap that occurs within a single liquidity pool.
60type SingleSwapParams struct {
61 tokenIn string // token to spend
62 tokenOut string // token to receive
63 fee uint32 // fee of the pool used to swap
64
65 // Amount specified for the swap:
66 // - Positive: exact input amount (tokenIn)
67 // - Negative: exact output amount (tokenOut)
68 amountSpecified int64
69
70 sqrtPriceLimitX96 *u256.Uint // sqrtPriceLimitX96 for the swap, empty string or zero string means no limit
71}
72
73// TokenIn returns the input token contract path.
74//
75// Returns:
76// - tokenIn: token spent by this single-pool swap
77func (p SingleSwapParams) TokenIn() string { return p.tokenIn }
78
79// TokenOut returns the output token contract path.
80//
81// Returns:
82// - tokenOut: token received by this single-pool swap
83func (p SingleSwapParams) TokenOut() string { return p.tokenOut }
84
85// Fee returns the pool fee tier.
86//
87// Returns:
88// - fee: fee tier encoded as the pool's uint32 fee value
89func (p SingleSwapParams) Fee() uint32 { return p.fee }
90
91// SqrtPriceLimitX96 returns the optional Q64.96 square-root price limit.
92// A nil stored limit is normalized to the zero value, which callers interpret as no explicit limit.
93//
94// Returns:
95// - limit: configured Q64.96 price limit, or a zero uint when no limit was supplied
96func (p SingleSwapParams) SqrtPriceLimitX96() *u256.Uint {
97 if p.sqrtPriceLimitX96 == nil {
98 return u256.Zero()
99 }
100
101 return p.sqrtPriceLimitX96
102}
103
104// SwapParams contains parameters for executing a multi-hop swap operation.
105type SwapParams struct {
106 SingleSwapParams
107 recipient address // address to receive the token
108}
109
110// TokenIn returns the input token contract path.
111//
112// Returns:
113// - tokenIn: token spent by this multi-hop swap parameters object
114func (p SwapParams) TokenIn() string { return p.tokenIn }
115
116// TokenOut returns the output token contract path.
117//
118// Returns:
119// - tokenOut: token received by this multi-hop swap parameters object
120func (p SwapParams) TokenOut() string { return p.tokenOut }
121
122// Fee returns the pool fee tier.
123//
124// Returns:
125// - fee: fee tier encoded as the pool's uint32 fee value
126func (p SwapParams) Fee() uint32 { return p.fee }
127
128// Recipient returns the address that receives swap output.
129//
130// Returns:
131// - recipient: configured output recipient address
132func (p SwapParams) Recipient() address { return p.recipient }
133
134// newSwapParams creates a new SwapParams instance with the provided parameters.
135func newSwapParams(tokenIn, tokenOut string, fee uint32, recipient address, amountSpecified int64) *SwapParams {
136 return &SwapParams{
137 SingleSwapParams: SingleSwapParams{
138 tokenIn: tokenIn,
139 tokenOut: tokenOut,
140 fee: fee,
141 amountSpecified: amountSpecified,
142 },
143 recipient: recipient,
144 }
145}
146
147// SwapResult encapsulates the outcome of a swap operation.
148type SwapResult struct {
149 Routes []string
150 Quotes []string
151 AmountIn int64
152 AmountOut int64
153 AmountSpecified int64
154}
155
156// SwapParamsI defines the common interface for swap parameters.
157type SwapParamsI interface {
158 // TokenIn returns the input token path required by swap processing.
159 //
160 // Returns:
161 // - tokenIn: token spent by the swap
162 TokenIn() string
163 // TokenOut returns the output token path required by swap processing.
164 //
165 // Returns:
166 // - tokenOut: token received by the swap
167 TokenOut() string
168 // Fee returns the pool fee tier required by swap processing.
169 //
170 // Returns:
171 // - fee: pool fee tier
172 Fee() uint32
173}
174
175// SwapCallbackData contains the callback data required for swap execution.
176// This type is used to pass necessary information during the swap callback process,
177// ensuring proper token transfers and pool data updates.
178type SwapCallbackData struct {
179 tokenIn string // token to spend
180 tokenOut string // token to receive
181 fee uint32 // fee of the pool used to swap
182 payer address // address to spend the token
183}
184
185// newSwapCallbackData creates a new SwapCallbackData from a SwapParamsI.
186func newSwapCallbackData(params SwapParamsI, payer address) SwapCallbackData {
187 return SwapCallbackData{
188 tokenIn: params.TokenIn(),
189 tokenOut: params.TokenOut(),
190 fee: params.Fee(),
191 payer: payer,
192 }
193}
194
195// newDrySwapCallbackData creates callback data for a dry swap. The payer is
196// deliberately the zero address because dry swaps do not execute callbacks or
197// token transfers.
198func newDrySwapCallbackData(params SwapParamsI) SwapCallbackData {
199 return SwapCallbackData{
200 tokenIn: params.TokenIn(),
201 tokenOut: params.TokenOut(),
202 fee: params.Fee(),
203 payer: zeroAddress,
204 }
205}
206
207// ExactInParams contains parameters for exact input swaps.
208type ExactInParams struct {
209 BaseSwapParams
210 AmountIn int64
211 AmountOutMin int64
212}
213
214// NewExactInParams creates parameters for an exact-input swap.
215//
216// Parameters:
217// - baseParams: shared token, route, quote, price-limit, and deadline settings
218// - amountIn: exact input amount in token units; must be positive before execution
219// - amountOutMin: minimum acceptable output amount in token units
220//
221// Returns:
222// - params: exact-input parameters combining baseParams and amount limits
223func NewExactInParams(
224 baseParams BaseSwapParams,
225 amountIn int64,
226 amountOutMin int64,
227) ExactInParams {
228 return ExactInParams{
229 BaseSwapParams: baseParams,
230 AmountIn: amountIn,
231 AmountOutMin: amountOutMin,
232 }
233}
234
235// ExactOutParams contains parameters for exact output swaps.
236type ExactOutParams struct {
237 BaseSwapParams
238 AmountOut int64
239 AmountInMax int64
240}
241
242// NewExactOutParams creates parameters for an exact-output swap.
243//
244// Parameters:
245// - baseParams: shared token, route, quote, price-limit, and deadline settings
246// - amountOut: requested output amount in token units; must be positive before execution
247// - amountInMax: maximum acceptable input amount in token units
248//
249// Returns:
250// - params: exact-output parameters combining baseParams and amount limits
251func NewExactOutParams(
252 baseParams BaseSwapParams,
253 amountOut int64,
254 amountInMax int64,
255) ExactOutParams {
256 return ExactOutParams{
257 BaseSwapParams: baseParams,
258 AmountOut: amountOut,
259 AmountInMax: amountInMax,
260 }
261}