Search Apps Documentation Source Content File Folder Download Copy Actions Download

external_deposit_fee.gno

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