type.gno
5.26 Kb · 192 lines
1package v1
2
3import (
4 u256 "gno.land/p/gnoswap/uint256"
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
15type SwapType string
16
17const (
18 Unknown SwapType = rawUnknown
19 // ExactIn represents a swap type where the input amount is exact and the output amount may vary.
20 // Used when a user wants to swap a specific amount of input tokens.
21 ExactIn SwapType = rawExactIn
22
23 // ExactOut represents a swap type where the output amount is exact and the input amount may vary.
24 // Used when a user wants to swap a specific amount of output tokens.
25 ExactOut SwapType = rawExactOut
26)
27
28// trySwapTypeFromStr attempts to convert a string into a SwapType.
29// It validates and converts string representations of swap types into their corresponding enum values.
30func trySwapTypeFromStr(swapType string) (SwapType, error) {
31 switch swapType {
32 case rawExactIn:
33 return ExactIn, nil
34 case rawExactOut:
35 return ExactOut, nil
36 default:
37 return "", ufmt.Errorf("unknown swapType: expected ExactIn or ExactOut, got %s", swapType)
38 }
39}
40
41// String returns the string representation of SwapType.
42func (s SwapType) String() string {
43 switch s {
44 case ExactIn:
45 return rawExactIn
46 case ExactOut:
47 return rawExactOut
48 default:
49 return ""
50 }
51}
52
53// SingleSwapParams contains parameters for executing a single pool swap.
54// It represents the simplest form of swap that occurs within a single liquidity pool.
55type SingleSwapParams struct {
56 tokenIn string // token to spend
57 tokenOut string // token to receive
58 fee uint32 // fee of the pool used to swap
59
60 // Amount specified for the swap:
61 // - Positive: exact input amount (tokenIn)
62 // - Negative: exact output amount (tokenOut)
63 amountSpecified int64
64
65 sqrtPriceLimitX96 *u256.Uint // sqrtPriceLimitX96 for the swap, empty string or zero string means no limit
66}
67
68// TokenIn returns the input token address.
69func (p SingleSwapParams) TokenIn() string { return p.tokenIn }
70
71// TokenOut returns the output token address.
72func (p SingleSwapParams) TokenOut() string { return p.tokenOut }
73
74// Fee returns the pool fee tier.
75func (p SingleSwapParams) Fee() uint32 { return p.fee }
76
77// SqrtPriceLimitX96 returns the sqrtPriceLimitX96 for the swap.
78// If sqrtPriceLimitX96 is empty string, it will return zero.
79func (p SingleSwapParams) SqrtPriceLimitX96() *u256.Uint {
80 if p.sqrtPriceLimitX96 == nil {
81 return u256.Zero()
82 }
83
84 return p.sqrtPriceLimitX96
85}
86
87// SwapParams contains parameters for executing a multi-hop swap operation.
88type SwapParams struct {
89 SingleSwapParams
90 recipient address // address to receive the token
91}
92
93// TokenIn returns the input token address.
94func (p SwapParams) TokenIn() string { return p.tokenIn }
95
96// TokenOut returns the output token address.
97func (p SwapParams) TokenOut() string { return p.tokenOut }
98
99// Fee returns the pool fee tier.
100func (p SwapParams) Fee() uint32 { return p.fee }
101
102// Recipient returns the recipient address.
103func (p SwapParams) Recipient() address { return p.recipient }
104
105// newSwapParams creates a new SwapParams instance with the provided parameters.
106func newSwapParams(tokenIn, tokenOut string, fee uint32, recipient address, amountSpecified int64) *SwapParams {
107 return &SwapParams{
108 SingleSwapParams: SingleSwapParams{
109 tokenIn: tokenIn,
110 tokenOut: tokenOut,
111 fee: fee,
112 amountSpecified: amountSpecified,
113 },
114 recipient: recipient,
115 }
116}
117
118// SwapResult encapsulates the outcome of a swap operation.
119type SwapResult struct {
120 Routes []string
121 Quotes []string
122 AmountIn int64
123 AmountOut int64
124 AmountSpecified int64
125}
126
127// SwapParamsI defines the common interface for swap parameters.
128type SwapParamsI interface {
129 TokenIn() string
130 TokenOut() string
131 Fee() uint32
132}
133
134// SwapCallbackData contains the callback data required for swap execution.
135// This type is used to pass necessary information during the swap callback process,
136// ensuring proper token transfers and pool data updates.
137type SwapCallbackData struct {
138 tokenIn string // token to spend
139 tokenOut string // token to receive
140 fee uint32 // fee of the pool used to swap
141 payer address // address to spend the token
142}
143
144// newSwapCallbackData creates a new SwapCallbackData from a SwapParamsI.
145func newSwapCallbackData(params SwapParamsI, payer address) SwapCallbackData {
146 return SwapCallbackData{
147 tokenIn: params.TokenIn(),
148 tokenOut: params.TokenOut(),
149 fee: params.Fee(),
150 payer: payer,
151 }
152}
153
154// ExactInParams contains parameters for exact input swaps.
155type ExactInParams struct {
156 BaseSwapParams
157 AmountIn int64
158 AmountOutMin int64
159}
160
161// NewExactInParams creates a new ExactInParams instance.
162func NewExactInParams(
163 baseParams BaseSwapParams,
164 amountIn int64,
165 amountOutMin int64,
166) ExactInParams {
167 return ExactInParams{
168 BaseSwapParams: baseParams,
169 AmountIn: amountIn,
170 AmountOutMin: amountOutMin,
171 }
172}
173
174// ExactOutParams contains parameters for exact output swaps.
175type ExactOutParams struct {
176 BaseSwapParams
177 AmountOut int64
178 AmountInMax int64
179}
180
181// NewExactOutParams creates a new ExactOutParams instance.
182func NewExactOutParams(
183 baseParams BaseSwapParams,
184 amountOut int64,
185 amountInMax int64,
186) ExactOutParams {
187 return ExactOutParams{
188 BaseSwapParams: baseParams,
189 AmountOut: amountOut,
190 AmountInMax: amountInMax,
191 }
192}