Search Apps Documentation Source Content File Folder Download Copy Actions Download

protocol_fee_unstaking.gno

1.96 Kb ยท 82 lines
 1package v1
 2
 3import (
 4	"chain"
 5	"chain/runtime"
 6	"errors"
 7
 8	prbac "gno.land/p/gnoswap/rbac"
 9	"gno.land/r/gnoswap/access"
10	"gno.land/r/gnoswap/common"
11	"gno.land/r/gnoswap/halt"
12
13	pf "gno.land/r/gnoswap/protocol_fee"
14)
15
16// GetUnstakingFee returns the current unstaking fee rate in basis points.
17func (s *stakerV1) GetUnstakingFee() int64 { return s.store.GetUnstakingFee() }
18
19// handleStakingRewardFee calculates and applies the unstaking fee.
20func (s *stakerV1) handleStakingRewardFee(
21	tokenPath string,
22	amount int64,
23	internal bool,
24) (int64, int64, error) {
25	unstakingFee := s.GetUnstakingFee()
26	if unstakingFee == 0 {
27		return amount, 0, nil
28	}
29
30	// Do not change the order of the operation.
31	feeAmount := safeMulDivInt64(amount, unstakingFee, 10000)
32	if feeAmount < 0 {
33		return 0, 0, errors.New("fee amount cannot be negative")
34	}
35
36	if feeAmount == 0 {
37		return amount, 0, nil
38	}
39
40	if internal {
41		tokenPath = GNS_PATH
42	}
43
44	// external contract has fee
45	protocolFeeAddr := access.MustGetAddress(prbac.ROLE_PROTOCOL_FEE.String())
46	common.SafeGRC20Transfer(cross, tokenPath, protocolFeeAddr, feeAmount)
47	pf.AddToProtocolFee(cross, tokenPath, feeAmount)
48
49	return safeSubInt64(amount, feeAmount), feeAmount, nil
50}
51
52// SetUnStakingFee sets the unstaking fee rate in basis points.
53// Only admin or governance can call this function.
54func (s *stakerV1) SetUnStakingFee(fee int64) {
55	halt.AssertIsNotHaltedStaker()
56
57	previousRealm := runtime.PreviousRealm()
58	caller := previousRealm.Address()
59	access.AssertIsAdminOrGovernance(caller)
60
61	assertIsValidFeeRate(fee)
62
63	prevUnStakingFee := s.GetUnstakingFee()
64
65	err := s.setUnStakingFee(fee)
66	if err != nil {
67		panic(err)
68	}
69
70	chain.Emit(
71		"SetUnStakingFee",
72		"prevAddr", caller.String(),
73		"prevRealm", previousRealm.PkgPath(),
74		"prevFee", formatAnyInt(prevUnStakingFee),
75		"newFee", formatAnyInt(fee),
76	)
77}
78
79// setUnStakingFee internally updates the unstaking fee.
80func (s *stakerV1) setUnStakingFee(fee int64) error {
81	return s.store.SetUnstakingFee(fee)
82}