Search Apps Documentation Source Content File Folder Download Copy Actions Download

types.gno

8.67 Kb · 302 lines
  1package pool
  2
  3import (
  4	bptree "gno.land/p/nt/bptree/v0"
  5
  6	u256 "gno.land/p/gnoswap/uint256"
  7)
  8
  9// IPool interface defines all public methods that must be implemented by pool contract versions.
 10// This interface serves as the contract between the proxy layer and implementation versions,
 11// ensuring that all versions (v1, v2, v3, etc.) maintain the same public API.
 12//
 13// This design enables seamless upgrades while maintaining backwards compatibility.
 14// When upgrading from v1 to v2, the proxy simply switches the implementation pointer
 15// without changing the public interface, ensuring zero downtime and no breaking changes.
 16type IPool interface {
 17	IPoolManager
 18	IPoolPosition
 19	IPoolSwap
 20	IPoolGetter
 21}
 22
 23// IPoolManager interface defines pool management operations.
 24// These methods handle pool creation and fee configuration.
 25type IPoolManager interface {
 26	// CreatePool creates a new concentrated liquidity pool.
 27	CreatePool(
 28		_ int,
 29		rlm realm,
 30		token0Path string,
 31		token1Path string,
 32		fee uint32,
 33		sqrtPriceX96 string,
 34	)
 35
 36	// SetPoolCreationFee sets the pool creation fee.
 37	SetPoolCreationFee(_ int, rlm realm, fee int64)
 38
 39	IncreaseObservationCardinalityNext(
 40		_ int,
 41		rlm realm,
 42		token0Path string,
 43		token1Path string,
 44		fee uint32,
 45		cardinalityNext uint16,
 46	)
 47}
 48
 49// IPoolPosition interface defines position management operations.
 50// These methods handle liquidity provision and position management.
 51type IPoolPosition interface {
 52	// Mint adds liquidity to a pool position.
 53	Mint(
 54		_ int,
 55		rlm realm,
 56		token0Path string,
 57		token1Path string,
 58		fee uint32,
 59		tickLower int32,
 60		tickUpper int32,
 61		liquidityAmount string,
 62		positionCaller address,
 63	) (string, string)
 64
 65	// Burn removes liquidity from a position.
 66	Burn(
 67		_ int,
 68		rlm realm,
 69		token0Path string,
 70		token1Path string,
 71		fee uint32,
 72		tickLower int32,
 73		tickUpper int32,
 74		liquidityAmount string,
 75		positionCaller address,
 76	) (string, string)
 77
 78	// Collect transfers owed tokens from a position to recipient.
 79	Collect(
 80		_ int,
 81		rlm realm,
 82		token0Path string,
 83		token1Path string,
 84		fee uint32,
 85		recipient address,
 86		tickLower int32,
 87		tickUpper int32,
 88		amount0Requested string,
 89		amount1Requested string,
 90	) (string, string)
 91
 92	SetWithdrawalFee(_ int, rlm realm, fee uint64)
 93
 94	HandleWithdrawalFee(
 95		_ int,
 96		rlm realm,
 97		token0Path string,
 98		amount0 string,
 99		token1Path string,
100		amount1 string,
101		positionCaller address,
102	) (string, string, string, string)
103}
104
105// IPoolSwap interface defines swap and protocol fee operations.
106// These methods handle token swaps and protocol fee management.
107type IPoolSwap interface {
108	Swap(
109		_ int,
110		rlm realm,
111		token0Path string,
112		token1Path string,
113		fee uint32,
114		recipient address,
115		zeroForOne bool,
116		amountSpecified string,
117		sqrtPriceLimitX96 string,
118		payer address,
119		swapCallback func(cur realm, amount0Delta, amount1Delta int64, callbackMarker *CallbackMarker) error,
120	) (string, string)
121
122	DrySwap(
123		token0Path string,
124		token1Path string,
125		fee uint32,
126		zeroForOne bool,
127		amountSpecified string,
128		sqrtPriceLimitX96 string,
129	) (string, string, bool)
130
131	SetSwapEndHook(_ int, rlm realm, hook func(cur realm, poolPath string) error)
132
133	SetSwapStartHook(_ int, rlm realm, hook func(cur realm, poolPath string, timestamp int64))
134
135	SetTickCrossHook(_ int, rlm realm, hook func(cur realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64))
136
137	CollectProtocol(
138		_ int,
139		rlm realm,
140		token0Path string,
141		token1Path string,
142		fee uint32,
143		recipient address,
144		amount0Requested string,
145		amount1Requested string,
146	) (string, string)
147
148	SetFeeProtocol(_ int, rlm realm, feeProtocol0, feeProtocol1 uint8)
149}
150
151// IPoolGetter interface defines data retrieval operations.
152// These methods provide read-only access to pool state and data.
153type IPoolGetter interface {
154	ExistsPoolPath(poolPath string) bool
155
156	GetBalanceToken0(poolPath string) int64
157
158	GetBalanceToken1(poolPath string) int64
159
160	GetFee(poolPath string) uint32
161
162	GetFeeAmountTickSpacing(fee uint32) (spacing int32)
163
164	GetFeeGrowthGlobal0X128(poolPath string) *u256.Uint
165
166	GetFeeGrowthGlobal1X128(poolPath string) *u256.Uint
167
168	GetFeeGrowthGlobalX128(poolPath string) (*u256.Uint, *u256.Uint)
169
170	GetLiquidity(poolPath string) *u256.Uint
171
172	GetObservation(poolPath string, secondsAgo int64) (tickCumulative int64, liquidityCumulative, secondsPerLiquidityCumulativeX128 string, blockTimestamp int64)
173
174	GetPoolCreationFee() int64
175
176	GetPositionFeeGrowthInside0LastX128(poolPath, key string) string
177
178	GetPositionFeeGrowthInside1LastX128(poolPath, key string) string
179
180	GetPositionFeeGrowthInsideLastX128(poolPath, key string) (string, string)
181
182	GetPositionLiquidity(poolPath, key string) string
183
184	GetPositionTokensOwed0(poolPath, key string) int64
185
186	GetPositionTokensOwed1(poolPath, key string) int64
187
188	GetProtocolFeesToken0(poolPath string) int64
189
190	GetProtocolFeesToken1(poolPath string) int64
191
192	GetSlot0FeeProtocol(poolPath string) uint8
193
194	GetSlot0SqrtPriceX96(poolPath string) *u256.Uint
195
196	GetSlot0Tick(poolPath string) int32
197
198	GetSlot0Unlocked(poolPath string) bool
199
200	GetTickCumulativeOutside(poolPath string, tick int32) int64
201
202	GetTickFeeGrowthOutside0X128(poolPath string, tick int32) string
203
204	GetTickFeeGrowthOutside1X128(poolPath string, tick int32) string
205
206	GetTickFeeGrowthOutsideX128(poolPath string, tick int32) (string, string)
207
208	GetTickInitialized(poolPath string, tick int32) bool
209
210	GetTickLiquidityGross(poolPath string, tick int32) string
211
212	GetTickLiquidityNet(poolPath string, tick int32) string
213
214	GetTickSecondsOutside(poolPath string, tick int32) uint32
215
216	GetTickSecondsPerLiquidityOutsideX128(poolPath string, tick int32) string
217
218	GetTickSpacing(poolPath string) int32
219
220	GetToken0Path(poolPath string) string
221
222	GetToken1Path(poolPath string) string
223
224	GetWithdrawalFee() uint64
225
226	GetTWAP(poolPath string, secondsAgo uint32) (int32, *u256.Uint, error)
227
228	GetPoolCount() int
229	GetPoolPaths(offset, count int) []string
230	GetFeeAmountTickSpacings() map[uint32]int32
231
232	GetPoolPositionCount(poolPath string) int
233	GetPoolPositionKeys(poolPath string, offset, count int) []string
234
235	GetInitializedTicksInRange(poolPath string, tickLower, tickUpper int32) []int32
236
237	// Structure getters
238	GetPool(token0Path, token1Path string, fee uint32) (*Pool, error)
239	GetTickInfo(poolPath string, tick int32) (TickInfo, error)
240	GetTickBitmaps(poolPath string, wordPos int16) (string, error)
241	GetPosition(poolPath, key string) (PositionInfo, error)
242	GetObservationState(poolPath string) (*ObservationState, error)
243}
244
245// IPoolStore interface defines the storage abstraction for pool data.
246// This interface provides a clean separation between business logic and storage,
247// allowing different implementations to use the same storage interface.
248//
249// All pool implementations (v1, v2, etc.) use this interface to access
250// and modify pool state, ensuring data consistency across versions.
251type IPoolStore interface {
252	HasPools() bool
253	GetPools() *bptree.BPTree
254	SetPools(_ int, rlm realm, pools *bptree.BPTree) error
255
256	HasFeeAmountTickSpacing() bool
257	GetFeeAmountTickSpacing() map[uint32]int32
258	SetFeeAmountTickSpacing(_ int, rlm realm, feeAmountTickSpacing map[uint32]int32) error
259
260	HasSlot0FeeProtocol() bool
261	GetSlot0FeeProtocol() uint8
262	SetSlot0FeeProtocol(_ int, rlm realm, slot0FeeProtocol uint8) error
263
264	HasPoolCreationFee() bool
265	GetPoolCreationFee() int64
266	SetPoolCreationFee(_ int, rlm realm, poolCreationFee int64) error
267
268	HasPendingProtocolFees() bool
269	GetPendingProtocolFees() map[string]int64
270	SetPendingProtocolFees(_ int, rlm realm, pendingProtocolFees map[string]int64) error
271
272	HasWithdrawalFeeBPS() bool
273	GetWithdrawalFeeBPS() uint64
274	SetWithdrawalFeeBPS(_ int, rlm realm, withdrawalFeeBPS uint64) error
275
276	HasUnlocked() bool
277	GetUnlocked() bool
278	SetUnlocked(_ int, rlm realm, unlocked bool) error
279
280	HasSwapStartHook() bool
281	GetSwapStartHook() func(cur realm, poolPath string, timestamp int64)
282	SetSwapStartHook(_ int, rlm realm, swapStartHook func(cur realm, poolPath string, timestamp int64)) error
283
284	HasSwapEndHook() bool
285	GetSwapEndHook() func(cur realm, poolPath string) error
286	SetSwapEndHook(_ int, rlm realm, swapEndHook func(cur realm, poolPath string) error) error
287
288	HasTickCrossHook() bool
289	GetTickCrossHook() func(cur realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64)
290	SetTickCrossHook(_ int, rlm realm, tickCrossHook func(cur realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64)) error
291}
292
293type CallbackMarker struct{}
294
295// NewCallbackMarker allocates a CallbackMarker in the pool realm.
296// Construction must happen here because /r/-declared types can only be
297// allocated in their owning realm (interrealm v2 checkConstructionTime).
298// Callers in other realms (e.g. pool/v1 impl) borrow into pool via
299// borrow rule #1 (function defined in /r/ package).
300func NewCallbackMarker() *CallbackMarker {
301	return &CallbackMarker{}
302}