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

protocol_fee_reward_manager.gno

4.92 Kb · 136 lines
  1package staker
  2
  3import (
  4	bptree "gno.land/p/nt/bptree/v0"
  5	ufmt "gno.land/p/nt/ufmt/v0"
  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 *bptree.BPTree // 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:                       bptree.NewBPTreeN(16),
 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 := p.rewardStates.Get(addr)
 46	if ri == nil {
 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 *bptree.BPTree) {
 86	p.rewardStates = rewardStates
 87}
 88
 89func (p *ProtocolFeeRewardManager) SetAccumulatedProtocolFeeX128PerStake(accumulatedProtocolFeeX128PerStake map[string]*u256.Uint) {
 90	copied := make(map[string]*u256.Uint, len(accumulatedProtocolFeeX128PerStake))
 91	for token, value := range accumulatedProtocolFeeX128PerStake {
 92		copied[token] = u256.Zero().Set(value)
 93	}
 94	p.accumulatedProtocolFeeX128PerStake = copied
 95}
 96
 97func (p *ProtocolFeeRewardManager) SetProtocolFeeAmounts(protocolFeeAmounts map[string]int64) {
 98	p.protocolFeeAmounts = protocolFeeAmounts
 99}
100
101func (p *ProtocolFeeRewardManager) SetAccumulatedProtocolFeeX128PerStakeForToken(token string, value *u256.Uint) {
102	if p.accumulatedProtocolFeeX128PerStake == nil {
103		p.accumulatedProtocolFeeX128PerStake = make(map[string]*u256.Uint)
104	}
105	p.accumulatedProtocolFeeX128PerStake[token] = value
106}
107
108func (p *ProtocolFeeRewardManager) SetProtocolFeeAmountForToken(token string, amount int64) {
109	if p.protocolFeeAmounts == nil {
110		p.protocolFeeAmounts = make(map[string]int64)
111	}
112	p.protocolFeeAmounts[token] = amount
113}
114
115func (p *ProtocolFeeRewardManager) SetAccumulatedTimestamp(accumulatedTimestamp int64) {
116	p.accumulatedTimestamp = accumulatedTimestamp
117}
118
119func (p *ProtocolFeeRewardManager) SetTotalStakedAmount(totalStakedAmount int64) {
120	p.totalStakedAmount = totalStakedAmount
121}
122
123// GetAllAccumulatedProtocolFeeX128PerStake returns all accumulated protocol fees per stake
124func (p *ProtocolFeeRewardManager) GetAllAccumulatedProtocolFeeX128PerStake() map[string]*u256.Uint {
125	return p.accumulatedProtocolFeeX128PerStake
126}
127
128// GetProtocolFeeAmount returns the protocol fee amount for a specific token
129func (p *ProtocolFeeRewardManager) GetProtocolFeeAmount(token string) int64 {
130	return p.protocolFeeAmounts[token]
131}
132
133// SetRewardState sets the reward state for a specific address
134func (p *ProtocolFeeRewardManager) SetRewardState(address string, rewardState *ProtocolFeeRewardState) {
135	p.rewardStates.Set(address, rewardState)
136}