Search Apps Documentation Source Content File Folder Download Copy Actions Download

type.gno

5.42 Kb ยท 196 lines
  1package v1
  2
  3import (
  4	u256 "gno.land/p/gnoswap/uint256"
  5
  6	"gno.land/p/nt/ufmt"
  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	withUnwrap bool   // whether to unwrap the token
 60
 61	// Amount specified for the swap:
 62	//  - Positive: exact input amount (tokenIn)
 63	//  - Negative: exact output amount (tokenOut)
 64	amountSpecified int64
 65
 66	sqrtPriceLimitX96 *u256.Uint // sqrtPriceLimitX96 for the swap, empty string or zero string means no limit
 67}
 68
 69// TokenIn returns the input token address.
 70func (p SingleSwapParams) TokenIn() string { return p.tokenIn }
 71
 72// TokenOut returns the output token address.
 73func (p SingleSwapParams) TokenOut() string { return p.tokenOut }
 74
 75// Fee returns the pool fee tier.
 76func (p SingleSwapParams) Fee() uint32 { return p.fee }
 77
 78// SqrtPriceLimitX96 returns the sqrtPriceLimitX96 for the swap.
 79// If sqrtPriceLimitX96 is empty string, it will return zero.
 80func (p SingleSwapParams) SqrtPriceLimitX96() *u256.Uint {
 81	if p.sqrtPriceLimitX96 == nil {
 82		return u256.Zero()
 83	}
 84
 85	return p.sqrtPriceLimitX96
 86}
 87
 88// SwapParams contains parameters for executing a multi-hop swap operation.
 89type SwapParams struct {
 90	SingleSwapParams
 91	recipient  address // address to receive the token
 92	withUnwrap bool    // whether to unwrap the token
 93}
 94
 95// TokenIn returns the input token address.
 96func (p SwapParams) TokenIn() string { return p.tokenIn }
 97
 98// TokenOut returns the output token address.
 99func (p SwapParams) TokenOut() string { return p.tokenOut }
100
101// Fee returns the pool fee tier.
102func (p SwapParams) Fee() uint32 { return p.fee }
103
104// Recipient returns the recipient address.
105func (p SwapParams) Recipient() address { return p.recipient }
106
107// newSwapParams creates a new SwapParams instance with the provided parameters.
108func newSwapParams(tokenIn, tokenOut string, fee uint32, recipient address, amountSpecified int64, withUnwrap bool) *SwapParams {
109	return &SwapParams{
110		SingleSwapParams: SingleSwapParams{
111			tokenIn:         tokenIn,
112			tokenOut:        tokenOut,
113			fee:             fee,
114			amountSpecified: amountSpecified,
115		},
116		withUnwrap: withUnwrap,
117		recipient:  recipient,
118	}
119}
120
121// SwapResult encapsulates the outcome of a swap operation.
122type SwapResult struct {
123	Routes          []string
124	Quotes          []string
125	AmountIn        int64
126	AmountOut       int64
127	AmountSpecified int64
128	WithUnwrap      bool
129}
130
131// SwapParamsI defines the common interface for swap parameters.
132type SwapParamsI interface {
133	TokenIn() string
134	TokenOut() string
135	Fee() uint32
136}
137
138// SwapCallbackData contains the callback data required for swap execution.
139// This type is used to pass necessary information during the swap callback process,
140// ensuring proper token transfers and pool data updates.
141type SwapCallbackData struct {
142	tokenIn  string  // token to spend
143	tokenOut string  // token to receive
144	fee      uint32  // fee of the pool used to swap
145	payer    address // address to spend the token
146}
147
148// newSwapCallbackData creates a new SwapCallbackData from a SwapParamsI.
149func newSwapCallbackData(params SwapParamsI, payer address) SwapCallbackData {
150	return SwapCallbackData{
151		tokenIn:  params.TokenIn(),
152		tokenOut: params.TokenOut(),
153		fee:      params.Fee(),
154		payer:    payer,
155	}
156}
157
158// ExactInParams contains parameters for exact input swaps.
159type ExactInParams struct {
160	BaseSwapParams
161	AmountIn     int64
162	AmountOutMin int64
163}
164
165// NewExactInParams creates a new ExactInParams instance.
166func NewExactInParams(
167	baseParams BaseSwapParams,
168	amountIn int64,
169	amountOutMin int64,
170) ExactInParams {
171	return ExactInParams{
172		BaseSwapParams: baseParams,
173		AmountIn:       amountIn,
174		AmountOutMin:   amountOutMin,
175	}
176}
177
178// ExactOutParams contains parameters for exact output swaps.
179type ExactOutParams struct {
180	BaseSwapParams
181	AmountOut   int64
182	AmountInMax int64
183}
184
185// NewExactOutParams creates a new ExactOutParams instance.
186func NewExactOutParams(
187	baseParams BaseSwapParams,
188	amountOut int64,
189	amountInMax int64,
190) ExactOutParams {
191	return ExactOutParams{
192		BaseSwapParams: baseParams,
193		AmountOut:      amountOut,
194		AmountInMax:    amountInMax,
195	}
196}