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