getter_utils.gno
2.87 Kb · 134 lines
1package pool
2
3import (
4 "time"
5
6 bptree "gno.land/p/nt/bptree/v0"
7)
8
9func cloneStringSlice(src []string) []string {
10 if src == nil {
11 return nil
12 }
13
14 cloned := make([]string, len(src))
15 copy(cloned, src)
16 return cloned
17}
18
19func cloneInt32Slice(src []int32) []int32 {
20 if src == nil {
21 return nil
22 }
23
24 cloned := make([]int32, len(src))
25 copy(cloned, src)
26 return cloned
27}
28
29func cloneFeeAmountTickSpacings(src map[uint32]int32) map[uint32]int32 {
30 if src == nil {
31 return nil
32 }
33
34 cloned := make(map[uint32]int32, len(src))
35 for fee, spacing := range src {
36 cloned[fee] = spacing
37 }
38
39 return cloned
40}
41
42func clonePositionInfo(src PositionInfo) PositionInfo {
43 return PositionInfo{
44 liquidity: src.liquidity,
45 feeGrowthInside0LastX128: src.feeGrowthInside0LastX128,
46 feeGrowthInside1LastX128: src.feeGrowthInside1LastX128,
47 tokensOwed0: src.tokensOwed0,
48 tokensOwed1: src.tokensOwed1,
49 }
50}
51
52func cloneObservationState(src *ObservationState) *ObservationState {
53 if src == nil {
54 return nil
55 }
56
57 observations := make(map[uint16]*Observation)
58 for index, observation := range src.observations {
59 observations[index] = observation.Clone()
60 }
61
62 return &ObservationState{
63 observations: observations,
64 index: src.index,
65 cardinality: src.cardinality,
66 cardinalityNext: src.cardinalityNext,
67 }
68}
69
70func clonePool(src *Pool) *Pool {
71 if src == nil {
72 return nil
73 }
74
75 return &Pool{
76 token0Path: src.token0Path,
77 token1Path: src.token1Path,
78 fee: src.fee,
79 tickSpacing: src.tickSpacing,
80 slot0: Slot0{
81 sqrtPriceX96: src.slot0.sqrtPriceX96.Clone(),
82 tick: src.slot0.tick,
83 feeProtocol: src.slot0.feeProtocol,
84 unlocked: src.slot0.unlocked,
85 },
86 balances: src.balances,
87 protocolFees: src.protocolFees,
88 feeGrowthGlobal0X128: src.feeGrowthGlobal0X128.Clone(),
89 feeGrowthGlobal1X128: src.feeGrowthGlobal1X128.Clone(),
90 liquidity: src.liquidity.Clone(),
91 ticks: bptree.NewBPTreeN(32),
92 tickBitmaps: make(map[int16]string),
93 positions: bptree.NewBPTreeN(16),
94 observationState: NewObservationState(time.Now().Unix()),
95 }
96}
97
98func clonePoolTicks(src *bptree.BPTree) *bptree.BPTree {
99 if src == nil {
100 return bptree.NewBPTreeN(32)
101 }
102
103 cloned := bptree.NewBPTreeN(32)
104
105 src.Iterate("", "", func(key string, value any) bool {
106 cloned.Set(key, value)
107 return false
108 })
109
110 return cloned
111}
112
113func clonePoolTickBitmaps(src map[int16]string) map[int16]string {
114 if src == nil {
115 return make(map[int16]string)
116 }
117
118 cloned := make(map[int16]string, len(src))
119 for wordPos, bitmap := range src {
120 cloned[wordPos] = bitmap
121 }
122
123 return cloned
124}
125
126func NewDefaultPositionInfo() PositionInfo {
127 return PositionInfo{
128 liquidity: "0",
129 feeGrowthInside0LastX128: "0",
130 feeGrowthInside1LastX128: "0",
131 tokensOwed0: 0,
132 tokensOwed1: 0,
133 }
134}