Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

external_deposit_fee.gno

6.26 Kb · 193 lines
  1package staker
  2
  3import (
  4	"chain"
  5	"strconv"
  6	"strings"
  7
  8	"gno.land/p/gnoswap/utils/v1"
  9	ufmt "gno.land/p/nt/ufmt/v0"
 10
 11	"gno.land/r/gnoswap/access/v1"
 12	"gno.land/r/gnoswap/halt/v1"
 13)
 14
 15// GetDepositGnsAmount returns the current GNS deposit amount required for each external incentive.
 16//
 17// Returns:
 18//   - amount: GNS deposit amount required per external incentive, in token smallest units.
 19func (s *stakerV1) GetDepositGnsAmount() int64 {
 20	return s.store.GetDepositGnsAmount()
 21}
 22
 23// GetMinimumRewardAmount returns the default minimum reward amount required for external incentives.
 24//
 25// Returns:
 26//   - amount: Default minimum external-incentive reward amount in token smallest units.
 27func (s *stakerV1) GetMinimumRewardAmount() int64 {
 28	return s.store.GetMinimumRewardAmount()
 29}
 30
 31// GetMinimumRewardAmountForToken returns the minimum reward amount for a specific token.
 32//
 33// Parameters:
 34//   - tokenPath: Token path whose configured minimum reward amount should be read.
 35//
 36// Returns:
 37//   - amount: Token-specific minimum reward amount when configured, otherwise the default minimum amount.
 38func (s *stakerV1) GetMinimumRewardAmountForToken(tokenPath string) int64 {
 39	amount, found := s.store.GetTokenSpecificMinimumRewards()[tokenPath]
 40	if found {
 41		return amount
 42	}
 43	// Fallback to default if not found
 44	return s.GetMinimumRewardAmount()
 45}
 46
 47// GetSpecificTokenMinimumRewardAmount returns the explicitly set minimum reward amount for a token.
 48//
 49// Parameters:
 50//   - tokenPath: Token path whose explicit minimum reward override should be read.
 51//
 52// Returns:
 53//   - amount: Explicit minimum reward amount in token smallest units, or 0 when no override is configured.
 54//   - found: True when tokenPath has an explicit override; false when the default would be used.
 55func (s *stakerV1) GetSpecificTokenMinimumRewardAmount(tokenPath string) (int64, bool) {
 56	amount, found := s.store.GetTokenSpecificMinimumRewards()[tokenPath]
 57	if !found {
 58		return 0, false
 59	}
 60	return amount, true
 61}
 62
 63// SetDepositGnsAmount sets the GNS deposit amount required for creating external incentives.
 64// Only admin or governance can call this function.
 65//
 66// Parameters:
 67//   - _: Noncrossing implementation-call discriminator; pass 0.
 68//   - rlm: Current realm context forwarded unchanged by the staker proxy.
 69//   - amount: Non-negative GNS deposit amount in token smallest units to require per external incentive.
 70func (s *stakerV1) SetDepositGnsAmount(_ int, rlm realm, amount int64) {
 71	access.AssertIsRlmCurrent(0, rlm)
 72
 73	halt.AssertIsNotHaltedStaker()
 74
 75	previousRealm := rlm.Previous()
 76	caller := previousRealm.Address()
 77	access.AssertIsAdminOrGovernance(caller)
 78
 79	assertIsValidAmount(amount)
 80
 81	prevDepositGnsAmount := s.store.GetDepositGnsAmount()
 82	s.setDepositGnsAmount(0, rlm, amount)
 83
 84	chain.Emit(
 85		"SetDepositGnsAmount",
 86		"prevAddr", caller.String(),
 87		"prevRealm", previousRealm.PkgPath(),
 88		"prevAmount", utils.FormatInt(prevDepositGnsAmount),
 89		"newAmount", utils.FormatInt(amount),
 90	)
 91}
 92
 93// SetMinimumRewardAmount sets the default minimum reward amount for external incentives.
 94// Only admin or governance can call this function.
 95//
 96// Parameters:
 97//   - _: Noncrossing implementation-call discriminator; pass 0.
 98//   - rlm: Current realm context forwarded unchanged by the staker proxy.
 99//   - amount: Non-negative default minimum reward amount in token smallest units for external incentives.
100func (s *stakerV1) SetMinimumRewardAmount(_ int, rlm realm, amount int64) {
101	access.AssertIsRlmCurrent(0, rlm)
102
103	halt.AssertIsNotHaltedStaker()
104
105	previousRealm := rlm.Previous()
106	caller := previousRealm.Address()
107	access.AssertIsAdminOrGovernance(caller)
108
109	assertIsValidAmount(amount)
110
111	prevMinimumRewardAmount := s.getMinimumRewardAmount()
112	s.setMinimumRewardAmount(0, rlm, amount)
113
114	chain.Emit(
115		"SetMinimumRewardAmount",
116		"prevAddr", caller.String(),
117		"prevRealm", previousRealm.PkgPath(),
118		"prevAmount", utils.FormatInt(prevMinimumRewardAmount),
119		"newAmount", utils.FormatInt(amount),
120	)
121}
122
123// SetTokenMinimumRewardAmount sets the minimum reward amount for a specific token.
124// Only admin or governance can call this function.
125//
126// Parameters:
127//   - _: Noncrossing implementation-call discriminator; pass 0.
128//   - rlm: Current realm context forwarded unchanged by the staker proxy.
129//   - paramsStr: Reward override specification in the form "tokenPath:amount"; amount 0 removes an existing token-specific override, while a nonzero amount stores it.
130func (s *stakerV1) SetTokenMinimumRewardAmount(_ int, rlm realm, paramsStr string) {
131	access.AssertIsRlmCurrent(0, rlm)
132
133	halt.AssertIsNotHaltedStaker()
134
135	previousRealm := rlm.Previous()
136	caller := previousRealm.Address()
137	access.AssertIsAdminOrGovernance(caller)
138
139	assertIsValidRewardAmountFormat(paramsStr)
140
141	// Parse the paramsStr
142	parts := strings.SplitN(paramsStr, ":", 2)
143	tokenPath := parts[0]
144	amountStr := parts[1]
145	amount64, err := strconv.ParseInt(amountStr, 10, 64)
146	if err != nil {
147		panic(makeErrorWithDetails(
148			errInvalidInput,
149			ufmt.Sprintf("invalid amount format in params '%s': %v", paramsStr, err),
150		))
151	}
152
153	prevAmount, found := s.store.GetTokenSpecificMinimumRewards()[tokenPath]
154
155	// If amount is 0, remove the entry; otherwise, set it.
156	if amount64 == 0 {
157		// Only attempt removal if an entry actually existed
158		if found {
159			if err := s.store.RemoveTokenSpecificMinimumRewardItem(0, rlm, tokenPath); err != nil {
160				panic(err)
161			}
162		}
163	} else {
164		if err := s.store.SetTokenSpecificMinimumRewardItem(0, rlm, tokenPath, amount64); err != nil {
165			panic(err)
166		}
167	}
168
169	chain.Emit(
170		"SetTokenMinimumRewardAmount",
171		"prevAddr", caller.String(),
172		"prevRealm", previousRealm.PkgPath(),
173		"tokenPath", tokenPath,
174		"prevAmountFound", utils.FormatBool(found),
175		"prevAmount", utils.FormatInt(prevAmount), // Will be 0 if !found
176		"newAmount", utils.FormatInt(amount64),
177	)
178}
179
180// setDepositGnsAmount internally updates the deposit GNS amount.
181func (s *stakerV1) setDepositGnsAmount(_ int, rlm realm, amount int64) {
182	s.store.SetDepositGnsAmount(0, rlm, amount)
183}
184
185// setMinimumRewardAmount internally updates the minimum reward amount.
186func (s *stakerV1) setMinimumRewardAmount(_ int, rlm realm, amount int64) {
187	s.store.SetMinimumRewardAmount(0, rlm, amount)
188}
189
190// getMinimumRewardAmount internally retrieves the minimum reward amount.
191func (s *stakerV1) getMinimumRewardAmount() int64 {
192	return s.store.GetMinimumRewardAmount()
193}