external_deposit_fee.gno
4.63 Kb · 164 lines
1package v1
2
3import (
4 "chain"
5 "strconv"
6 "strings"
7
8 ufmt "gno.land/p/nt/ufmt/v0"
9
10 "gno.land/r/gnoswap/access"
11 "gno.land/r/gnoswap/halt"
12)
13
14// GetDepositGnsAmount returns the current deposit amount in GNS.
15func (s *stakerV1) GetDepositGnsAmount() int64 {
16 return s.store.GetDepositGnsAmount()
17}
18
19// GetMinimumRewardAmount returns the default minimum reward amount required for external incentives.
20func (s *stakerV1) GetMinimumRewardAmount() int64 {
21 return s.store.GetMinimumRewardAmount()
22}
23
24// GetMinimumRewardAmountForToken returns the minimum reward amount for a specific token.
25func (s *stakerV1) GetMinimumRewardAmountForToken(tokenPath string) int64 {
26 amount, found := s.store.GetTokenSpecificMinimumRewards()[tokenPath]
27 if found {
28 return amount
29 }
30 // Fallback to default if not found
31 return s.GetMinimumRewardAmount()
32}
33
34// GetSpecificTokenMinimumRewardAmount returns the explicitly set minimum reward amount for a token.
35func (s *stakerV1) GetSpecificTokenMinimumRewardAmount(tokenPath string) (int64, bool) {
36 amount, found := s.store.GetTokenSpecificMinimumRewards()[tokenPath]
37 if !found {
38 return 0, false
39 }
40 return amount, true
41}
42
43// SetDepositGnsAmount sets the GNS deposit amount required for creating external incentives.
44// Only admin or governance can call this function.
45func (s *stakerV1) SetDepositGnsAmount(_ int, rlm realm, amount int64) {
46 if !rlm.IsCurrent() {
47 panic(errSpoofedRealm)
48 }
49
50 halt.AssertIsNotHaltedStaker()
51
52 previousRealm := rlm.Previous()
53 caller := previousRealm.Address()
54 access.AssertIsAdminOrGovernance(caller)
55
56 assertIsValidAmount(amount)
57
58 prevDepositGnsAmount := s.GetDepositGnsAmount()
59 s.setDepositGnsAmount(0, rlm, amount)
60
61 chain.Emit(
62 "SetDepositGnsAmount",
63 "prevAddr", caller.String(),
64 "prevRealm", previousRealm.PkgPath(),
65 "prevAmount", formatAnyInt(prevDepositGnsAmount),
66 "newAmount", formatAnyInt(amount),
67 )
68}
69
70// SetMinimumRewardAmount sets the default minimum reward amount for external incentives.
71// Only admin or governance can call this function.
72func (s *stakerV1) SetMinimumRewardAmount(_ int, rlm realm, amount int64) {
73 if !rlm.IsCurrent() {
74 panic(errSpoofedRealm)
75 }
76
77 halt.AssertIsNotHaltedStaker()
78
79 previousRealm := rlm.Previous()
80 caller := previousRealm.Address()
81 access.AssertIsAdminOrGovernance(caller)
82
83 assertIsValidAmount(amount)
84
85 prevMinimumRewardAmount := s.getMinimumRewardAmount()
86 s.setMinimumRewardAmount(0, rlm, amount)
87
88 chain.Emit(
89 "SetMinimumRewardAmount",
90 "prevAddr", caller.String(),
91 "prevRealm", previousRealm.PkgPath(),
92 "prevAmount", formatAnyInt(prevMinimumRewardAmount),
93 "newAmount", formatAnyInt(amount),
94 )
95}
96
97// SetTokenMinimumRewardAmount sets the minimum reward amount for a specific token.
98// Only admin or governance can call this function.
99func (s *stakerV1) SetTokenMinimumRewardAmount(_ int, rlm realm, paramsStr string) {
100 if !rlm.IsCurrent() {
101 panic(errSpoofedRealm)
102 }
103
104 halt.AssertIsNotHaltedStaker()
105
106 previousRealm := rlm.Previous()
107 caller := previousRealm.Address()
108 access.AssertIsAdminOrGovernance(caller)
109
110 assertIsValidRewardAmountFormat(paramsStr)
111
112 // Parse the paramsStr
113 parts := strings.SplitN(paramsStr, ":", 2)
114 tokenPath := parts[0]
115 amountStr := parts[1]
116 amount64, err := strconv.ParseInt(amountStr, 10, 64)
117 if err != nil {
118 panic(makeErrorWithDetails(
119 errInvalidInput,
120 ufmt.Sprintf("invalid amount format in params '%s': %v", paramsStr, err),
121 ))
122 }
123
124 prevAmount, found := s.GetSpecificTokenMinimumRewardAmount(tokenPath)
125
126 // If amount is 0, remove the entry; otherwise, set it.
127 if amount64 == 0 {
128 // Only attempt removal if an entry actually existed
129 if found {
130 if err := s.store.RemoveTokenSpecificMinimumRewardItem(0, rlm, tokenPath); err != nil {
131 panic(err)
132 }
133 }
134 } else {
135 if err := s.store.SetTokenSpecificMinimumRewardItem(0, rlm, tokenPath, amount64); err != nil {
136 panic(err)
137 }
138 }
139
140 chain.Emit(
141 "SetTokenMinimumRewardAmount",
142 "prevAddr", caller.String(),
143 "prevRealm", previousRealm.PkgPath(),
144 "tokenPath", tokenPath,
145 "prevAmountFound", formatBool(found),
146 "prevAmount", formatAnyInt(prevAmount), // Will be 0 if !found
147 "newAmount", formatAnyInt(amount64),
148 )
149}
150
151// setDepositGnsAmount internally updates the deposit GNS amount.
152func (s *stakerV1) setDepositGnsAmount(_ int, rlm realm, amount int64) {
153 s.store.SetDepositGnsAmount(0, rlm, amount)
154}
155
156// setMinimumRewardAmount internally updates the minimum reward amount.
157func (s *stakerV1) setMinimumRewardAmount(_ int, rlm realm, amount int64) {
158 s.store.SetMinimumRewardAmount(0, rlm, amount)
159}
160
161// getMinimumRewardAmount internally retrieves the minimum reward amount.
162func (s *stakerV1) getMinimumRewardAmount() int64 {
163 return s.store.GetMinimumRewardAmount()
164}