init.gno
2.94 Kb · 125 lines
1package v1
2
3import (
4 "gno.land/r/gnoswap/pool"
5)
6
7const (
8 // slot0FeeProtocol represents the protocol fee percentage (0-10).
9 // This parameter can be modified through governance.
10 defaultSlot0FeeProtocol = uint8(0)
11
12 // poolCreationFee is the fee that is charged when a user creates a pool.
13 // The fee is denominated in GNS tokens.
14 // This parameter can be modified through governance.
15 defaultPoolCreationFee = int64(100_000_000) // 100_GNS
16
17 // withdrawalFeeBPS is the fee that is charged when a user withdraws their collected fees
18 // The fee is denominated in BPS (Basis Points)
19 // Example: 100 BPS = 1%
20 // This parameter can be modified through governance.
21 defaultWithdrawalFeeBPS = uint64(100)
22
23 defaultUnlocked = true
24)
25
26func init(cur realm) {
27 registerPoolV1(cur)
28}
29
30func registerPoolV1(cur realm) {
31 pool.RegisterInitializer(cross(cur), func(_ int, rlm realm, poolStore pool.IPoolStore) pool.IPool {
32 if !rlm.IsCurrent() {
33 panic(errSpoofedRealm)
34 }
35
36 err := initStoreData(0, rlm, poolStore)
37 if err != nil {
38 panic(err)
39 }
40
41 return NewPoolV1(poolStore)
42 })
43}
44
45func initStoreData(_ int, rlm realm, poolStore pool.IPoolStore) error {
46 if !poolStore.HasPools() {
47 err := poolStore.SetPools(0, rlm, pool.NewPoolsTree())
48 if err != nil {
49 return err
50 }
51 }
52
53 if !poolStore.HasFeeAmountTickSpacing() {
54 err := poolStore.SetFeeAmountTickSpacing(0, rlm, pool.NewDefaultFeeAmountTickSpacing())
55 if err != nil {
56 return err
57 }
58 }
59
60 if !poolStore.HasPoolCreationFee() {
61 err := poolStore.SetPoolCreationFee(0, rlm, defaultPoolCreationFee)
62 if err != nil {
63 return err
64 }
65 }
66
67 if !poolStore.HasPendingProtocolFees() {
68 err := poolStore.SetPendingProtocolFees(0, rlm, make(map[string]int64))
69 if err != nil {
70 return err
71 }
72 }
73
74 if !poolStore.HasSlot0FeeProtocol() {
75 err := poolStore.SetSlot0FeeProtocol(0, rlm, defaultSlot0FeeProtocol)
76 if err != nil {
77 return err
78 }
79 }
80
81 if !poolStore.HasWithdrawalFeeBPS() {
82 err := poolStore.SetWithdrawalFeeBPS(0, rlm, defaultWithdrawalFeeBPS)
83 if err != nil {
84 return err
85 }
86 }
87
88 if !poolStore.HasUnlocked() {
89 err := poolStore.SetUnlocked(0, rlm, defaultUnlocked)
90 if err != nil {
91 return err
92 }
93 }
94
95 // Initialize swap start hook with no-op function
96 if !poolStore.HasSwapStartHook() {
97 noopSwapStart := func(cur realm, poolPath string, timestamp int64) {}
98 err := poolStore.SetSwapStartHook(0, rlm, noopSwapStart)
99 if err != nil {
100 return err
101 }
102 }
103
104 // Initialize swap end hook with no-op function
105 if !poolStore.HasSwapEndHook() {
106 noopSwapEnd := func(cur realm, poolPath string) error {
107 return nil
108 }
109 err := poolStore.SetSwapEndHook(0, rlm, noopSwapEnd)
110 if err != nil {
111 return err
112 }
113 }
114
115 // Initialize tick cross hook with no-op function
116 if !poolStore.HasTickCrossHook() {
117 noopTickCross := func(cur realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64) {}
118 err := poolStore.SetTickCrossHook(0, rlm, noopTickCross)
119 if err != nil {
120 return err
121 }
122 }
123
124 return nil
125}