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