types.gno
8.62 Kb · 301 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 swapCallback func(cur realm, amount0Delta, amount1Delta int64, callbackMarker *CallbackMarker) error,
119 ) (string, string)
120
121 DrySwap(
122 token0Path string,
123 token1Path string,
124 fee uint32,
125 zeroForOne bool,
126 amountSpecified string,
127 sqrtPriceLimitX96 string,
128 ) (string, string, bool)
129
130 SetSwapEndHook(_ int, rlm realm, hook func(cur realm, poolPath string) error)
131
132 SetSwapStartHook(_ int, rlm realm, hook func(cur realm, poolPath string, timestamp int64))
133
134 SetTickCrossHook(_ int, rlm realm, hook func(cur realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64))
135
136 CollectProtocol(
137 _ int,
138 rlm realm,
139 token0Path string,
140 token1Path string,
141 fee uint32,
142 recipient address,
143 amount0Requested string,
144 amount1Requested string,
145 ) (string, string)
146
147 SetFeeProtocol(_ int, rlm realm, feeProtocol0, feeProtocol1 uint8)
148}
149
150// IPoolGetter interface defines data retrieval operations.
151// These methods provide read-only access to pool state and data.
152type IPoolGetter interface {
153 ExistsPoolPath(poolPath string) bool
154
155 GetBalanceToken0(poolPath string) int64
156
157 GetBalanceToken1(poolPath string) int64
158
159 GetFee(poolPath string) uint32
160
161 GetFeeAmountTickSpacing(fee uint32) (spacing int32)
162
163 GetFeeGrowthGlobal0X128(poolPath string) *u256.Uint
164
165 GetFeeGrowthGlobal1X128(poolPath string) *u256.Uint
166
167 GetFeeGrowthGlobalX128(poolPath string) (*u256.Uint, *u256.Uint)
168
169 GetLiquidity(poolPath string) *u256.Uint
170
171 GetLastObservation(poolPath string) (tickCumulative int64, secondsPerLiquidityCumulativeX128 string, blockTimestamp int64)
172
173 GetPoolCreationFee() int64
174
175 GetPositionFeeGrowthInside0LastX128(poolPath, key string) string
176
177 GetPositionFeeGrowthInside1LastX128(poolPath, key string) string
178
179 GetPositionFeeGrowthInsideLastX128(poolPath, key string) (string, string)
180
181 GetPositionLiquidity(poolPath, key string) string
182
183 GetPositionTokensOwed0(poolPath, key string) int64
184
185 GetPositionTokensOwed1(poolPath, key string) int64
186
187 GetProtocolFeesToken0(poolPath string) int64
188
189 GetProtocolFeesToken1(poolPath string) int64
190
191 GetSlot0FeeProtocol(poolPath string) uint8
192
193 GetSlot0SqrtPriceX96(poolPath string) *u256.Uint
194
195 GetSlot0Tick(poolPath string) int32
196
197 GetSlot0Unlocked(poolPath string) bool
198
199 GetTickCumulativeOutside(poolPath string, tick int32) int64
200
201 GetTickFeeGrowthOutside0X128(poolPath string, tick int32) string
202
203 GetTickFeeGrowthOutside1X128(poolPath string, tick int32) string
204
205 GetTickFeeGrowthOutsideX128(poolPath string, tick int32) (string, string)
206
207 GetTickInitialized(poolPath string, tick int32) bool
208
209 GetTickLiquidityGross(poolPath string, tick int32) string
210
211 GetTickLiquidityNet(poolPath string, tick int32) string
212
213 GetTickSecondsOutside(poolPath string, tick int32) uint32
214
215 GetTickSecondsPerLiquidityOutsideX128(poolPath string, tick int32) string
216
217 GetTickSpacing(poolPath string) int32
218
219 GetToken0Path(poolPath string) string
220
221 GetToken1Path(poolPath string) string
222
223 GetWithdrawalFee() uint64
224
225 GetTWAP(poolPath string, secondsAgo uint32) (int32, *u256.Uint, error)
226
227 GetPoolCount() int
228 GetPoolPaths(offset, count int) []string
229 GetFeeAmountTickSpacings() map[uint32]int32
230
231 GetPoolPositionCount(poolPath string) int
232 GetPoolPositionKeys(poolPath string, offset, count int) []string
233
234 GetInitializedTicksInRange(poolPath string, tickLower, tickUpper int32) []int32
235
236 // Structure getters
237 GetPool(token0Path, token1Path string, fee uint32) (*Pool, error)
238 GetTickInfo(poolPath string, tick int32) (TickInfo, error)
239 GetTickBitmaps(poolPath string, wordPos int16) (string, error)
240 GetPosition(poolPath, key string) (PositionInfo, error)
241 GetObservationState(poolPath string) (*ObservationState, error)
242}
243
244// IPoolStore interface defines the storage abstraction for pool data.
245// This interface provides a clean separation between business logic and storage,
246// allowing different implementations to use the same storage interface.
247//
248// All pool implementations (v1, v2, etc.) use this interface to access
249// and modify pool state, ensuring data consistency across versions.
250type IPoolStore interface {
251 HasPools() bool
252 GetPools() *bptree.BPTree
253 SetPools(_ int, rlm realm, pools *bptree.BPTree) error
254
255 HasFeeAmountTickSpacing() bool
256 GetFeeAmountTickSpacing() map[uint32]int32
257 SetFeeAmountTickSpacing(_ int, rlm realm, feeAmountTickSpacing map[uint32]int32) error
258
259 HasSlot0FeeProtocol() bool
260 GetSlot0FeeProtocol() uint8
261 SetSlot0FeeProtocol(_ int, rlm realm, slot0FeeProtocol uint8) error
262
263 HasPoolCreationFee() bool
264 GetPoolCreationFee() int64
265 SetPoolCreationFee(_ int, rlm realm, poolCreationFee int64) error
266
267 HasPendingProtocolFees() bool
268 GetPendingProtocolFees() map[string]int64
269 SetPendingProtocolFees(_ int, rlm realm, pendingProtocolFees map[string]int64) error
270
271 HasWithdrawalFeeBPS() bool
272 GetWithdrawalFeeBPS() uint64
273 SetWithdrawalFeeBPS(_ int, rlm realm, withdrawalFeeBPS uint64) error
274
275 HasUnlocked() bool
276 GetUnlocked() bool
277 SetUnlocked(_ int, rlm realm, unlocked bool) error
278
279 HasSwapStartHook() bool
280 GetSwapStartHook() func(cur realm, poolPath string, timestamp int64)
281 SetSwapStartHook(_ int, rlm realm, swapStartHook func(cur realm, poolPath string, timestamp int64)) error
282
283 HasSwapEndHook() bool
284 GetSwapEndHook() func(cur realm, poolPath string) error
285 SetSwapEndHook(_ int, rlm realm, swapEndHook func(cur realm, poolPath string) error) error
286
287 HasTickCrossHook() bool
288 GetTickCrossHook() func(cur realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64)
289 SetTickCrossHook(_ int, rlm realm, tickCrossHook func(cur realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64)) error
290}
291
292type CallbackMarker struct{}
293
294// NewCallbackMarker allocates a CallbackMarker in the pool realm.
295// Construction must happen here because /r/-declared types can only be
296// allocated in their owning realm (interrealm v2 checkConstructionTime).
297// Callers in other realms (e.g. pool/v1 impl) borrow into pool via
298// borrow rule #1 (function defined in /r/ package).
299func NewCallbackMarker() *CallbackMarker {
300 return &CallbackMarker{}
301}