protocol_fee_reward_manager.gno
4.22 Kb ยท 118 lines
1package staker
2
3import (
4 "gno.land/p/nt/avl"
5 "gno.land/p/nt/ufmt"
6
7 u256 "gno.land/p/gnoswap/uint256"
8)
9
10// ProtocolFeeRewardManager manages the distribution of protocol fee rewards to stakers.
11// Unlike emission rewards, protocol fees can come from multiple tokens, requiring
12// separate tracking and distribution mechanisms for each token type.
13type ProtocolFeeRewardManager struct {
14 // rewardStates maps address to ProtocolFeeRewardState for tracking individual staker rewards
15 rewardStates *avl.Tree // address -> ProtocolFeeRewardState
16
17 // accumulatedProtocolFeeX128PerStake maps token path to accumulated fee per stake with 128-bit precision
18 accumulatedProtocolFeeX128PerStake map[string]*u256.Uint
19 // protocolFeeAmounts maps token path to total distributed protocol fee amounts
20 protocolFeeAmounts map[string]int64
21 // accumulatedTimestamp tracks the last timestamp when fees were accumulated
22 accumulatedTimestamp int64
23 // totalStakedAmount tracks the total amount of tokens staked in the system
24 totalStakedAmount int64
25}
26
27// NewProtocolFeeRewardManager creates a new instance of ProtocolFeeRewardManager.
28// This factory function initializes all tracking structures for multi-token protocol fee reward management.
29//
30// Returns:
31// - *ProtocolFeeRewardManager: new protocol fee reward manager instance
32func NewProtocolFeeRewardManager() *ProtocolFeeRewardManager {
33 return &ProtocolFeeRewardManager{
34 rewardStates: avl.NewTree(),
35 protocolFeeAmounts: make(map[string]int64),
36 accumulatedProtocolFeeX128PerStake: make(map[string]*u256.Uint),
37 accumulatedTimestamp: 0,
38 totalStakedAmount: 0,
39 }
40}
41
42/* Getters */
43
44func (p *ProtocolFeeRewardManager) GetRewardState(addr string) (*ProtocolFeeRewardState, bool, error) {
45 ri, ok := p.rewardStates.Get(addr)
46 if !ok {
47 return nil, false, nil
48 }
49 rs, castOk := ri.(*ProtocolFeeRewardState)
50 if !castOk {
51 return nil, false, ufmt.Errorf(errFailedToCastRewardState, ri)
52 }
53 return rs, true, nil
54}
55
56// GetAccumulatedProtocolFeeX128PerStake returns the accumulated protocol fee per stake for a specific token.
57//
58// Parameters:
59// - token: token path to get accumulated fee for
60//
61// Returns:
62// - *u256.Uint: accumulated protocol fee per stake for the token (scaled by 2^128)
63func (p *ProtocolFeeRewardManager) GetAccumulatedProtocolFeeX128PerStake(token string) *u256.Uint {
64 return p.accumulatedProtocolFeeX128PerStake[token]
65}
66
67func (p *ProtocolFeeRewardManager) GetProtocolFeeAmounts() map[string]int64 {
68 return p.protocolFeeAmounts
69}
70
71// GetAccumulatedTimestamp returns the last timestamp when protocol fees were accumulated.
72//
73// Returns:
74// - int64: last accumulated timestamp
75func (p *ProtocolFeeRewardManager) GetAccumulatedTimestamp() int64 {
76 return p.accumulatedTimestamp
77}
78
79func (p *ProtocolFeeRewardManager) GetTotalStakedAmount() int64 {
80 return p.totalStakedAmount
81}
82
83/* Setters */
84
85func (p *ProtocolFeeRewardManager) SetRewardStates(rewardStates *avl.Tree) {
86 p.rewardStates = rewardStates
87}
88
89func (p *ProtocolFeeRewardManager) SetAccumulatedProtocolFeeX128PerStake(accumulatedProtocolFeeX128PerStake map[string]*u256.Uint) {
90 p.accumulatedProtocolFeeX128PerStake = accumulatedProtocolFeeX128PerStake
91}
92
93func (p *ProtocolFeeRewardManager) SetProtocolFeeAmounts(protocolFeeAmounts map[string]int64) {
94 p.protocolFeeAmounts = protocolFeeAmounts
95}
96
97func (p *ProtocolFeeRewardManager) SetAccumulatedTimestamp(accumulatedTimestamp int64) {
98 p.accumulatedTimestamp = accumulatedTimestamp
99}
100
101func (p *ProtocolFeeRewardManager) SetTotalStakedAmount(totalStakedAmount int64) {
102 p.totalStakedAmount = totalStakedAmount
103}
104
105// GetAllAccumulatedProtocolFeeX128PerStake returns all accumulated protocol fees per stake
106func (p *ProtocolFeeRewardManager) GetAllAccumulatedProtocolFeeX128PerStake() map[string]*u256.Uint {
107 return p.accumulatedProtocolFeeX128PerStake
108}
109
110// GetProtocolFeeAmount returns the protocol fee amount for a specific token
111func (p *ProtocolFeeRewardManager) GetProtocolFeeAmount(token string) int64 {
112 return p.protocolFeeAmounts[token]
113}
114
115// SetRewardState sets the reward state for a specific address
116func (p *ProtocolFeeRewardManager) SetRewardState(address string, rewardState *ProtocolFeeRewardState) {
117 p.rewardStates.Set(address, rewardState)
118}