Search Apps Documentation Source Content File Folder Download Copy Actions Download

protocol_fee_reward_state.gno

4.39 Kb ยท 140 lines
  1package staker
  2
  3import (
  4	u256 "gno.land/p/gnoswap/uint256"
  5)
  6
  7// ProtocolFeeRewardState tracks protocol fee reward information for an individual staker across multiple tokens.
  8// Unlike emission rewards which are single-token, protocol fees can come from various trading pairs,
  9// requiring separate tracking and calculation for each token type.
 10type ProtocolFeeRewardState struct {
 11	// rewardDebtX128 maps token path to reward debt with 128-bit precision scaling
 12	// Used to calculate rewards earned since the last update for each token
 13	rewardDebtX128 map[string]*u256.Uint
 14	// accumulatedRewards maps token path to total rewards accumulated but not yet claimed
 15	accumulatedRewards map[string]int64
 16	// claimedRewards maps token path to total amount of rewards that have been claimed
 17	claimedRewards map[string]int64
 18	// accumulatedTimestamp is the last timestamp when rewards were accumulated
 19	accumulatedTimestamp int64
 20	// claimedTimestamp is the last timestamp when rewards were claimed
 21	claimedTimestamp int64
 22	// stakedAmount is the current amount of tokens staked by this address
 23	stakedAmount int64
 24}
 25
 26func (p *ProtocolFeeRewardState) GetRewardDebtX128() map[string]*u256.Uint {
 27	return p.rewardDebtX128
 28}
 29
 30func (p *ProtocolFeeRewardState) GetAccumulatedRewards() map[string]int64 {
 31	return p.accumulatedRewards
 32}
 33
 34func (p *ProtocolFeeRewardState) GetClaimedRewards() map[string]int64 {
 35	return p.claimedRewards
 36}
 37
 38func (p *ProtocolFeeRewardState) GetAccumulatedTimestamp() int64 {
 39	return p.accumulatedTimestamp
 40}
 41
 42func (p *ProtocolFeeRewardState) GetClaimedTimestamp() int64 {
 43	return p.claimedTimestamp
 44}
 45
 46func (p *ProtocolFeeRewardState) GetStakedAmount() int64 {
 47	return p.stakedAmount
 48}
 49
 50/* Setters */
 51
 52func (p *ProtocolFeeRewardState) SetRewardDebtX128(rewardDebtX128 map[string]*u256.Uint) {
 53	p.rewardDebtX128 = rewardDebtX128
 54}
 55
 56func (p *ProtocolFeeRewardState) SetAccumulatedRewards(accumulatedRewards map[string]int64) {
 57	p.accumulatedRewards = accumulatedRewards
 58}
 59
 60func (p *ProtocolFeeRewardState) SetClaimedRewards(claimedRewards map[string]int64) {
 61	p.claimedRewards = claimedRewards
 62}
 63
 64func (p *ProtocolFeeRewardState) SetAccumulatedTimestamp(accumulatedTimestamp int64) {
 65	p.accumulatedTimestamp = accumulatedTimestamp
 66}
 67
 68func (p *ProtocolFeeRewardState) SetClaimedTimestamp(claimedTimestamp int64) {
 69	p.claimedTimestamp = claimedTimestamp
 70}
 71
 72func (p *ProtocolFeeRewardState) SetStakedAmount(stakedAmount int64) {
 73	p.stakedAmount = stakedAmount
 74}
 75
 76// Additional getters for individual token rewards
 77
 78func (p *ProtocolFeeRewardState) GetRewardDebtX128ForToken(token string) *u256.Uint {
 79	if p.rewardDebtX128 == nil {
 80		return nil
 81	}
 82	return p.rewardDebtX128[token]
 83}
 84
 85func (p *ProtocolFeeRewardState) GetAccumulatedRewardForToken(token string) int64 {
 86	if p.accumulatedRewards == nil {
 87		return 0
 88	}
 89	return p.accumulatedRewards[token]
 90}
 91
 92func (p *ProtocolFeeRewardState) GetClaimedRewardForToken(token string) int64 {
 93	if p.claimedRewards == nil {
 94		return 0
 95	}
 96	return p.claimedRewards[token]
 97}
 98
 99// Additional setters for individual token rewards
100
101func (p *ProtocolFeeRewardState) SetRewardDebtX128ForToken(token string, value *u256.Uint) {
102	if p.rewardDebtX128 == nil {
103		p.rewardDebtX128 = make(map[string]*u256.Uint)
104	}
105	p.rewardDebtX128[token] = value
106}
107
108func (p *ProtocolFeeRewardState) SetAccumulatedRewardForToken(token string, value int64) {
109	if p.accumulatedRewards == nil {
110		p.accumulatedRewards = make(map[string]int64)
111	}
112	p.accumulatedRewards[token] = value
113}
114
115func (p *ProtocolFeeRewardState) SetClaimedRewardForToken(token string, value int64) {
116	if p.claimedRewards == nil {
117		p.claimedRewards = make(map[string]int64)
118	}
119	p.claimedRewards[token] = value
120}
121
122// NewProtocolFeeRewardState creates a new protocol fee reward state for a staker.
123// This factory function initializes the state with the current system reward debt for all tokens.
124func NewProtocolFeeRewardState(accumulatedProtocolFeeX128PerStake map[string]*u256.Uint) *ProtocolFeeRewardState {
125	rewardDebtX128 := make(map[string]*u256.Uint)
126
127	// Clone reward debt for each token to avoid reference issues
128	for token, accumulatedFee := range accumulatedProtocolFeeX128PerStake {
129		rewardDebtX128[token] = accumulatedFee.Clone()
130	}
131
132	return &ProtocolFeeRewardState{
133		rewardDebtX128:       rewardDebtX128,
134		claimedRewards:       make(map[string]int64),
135		accumulatedRewards:   make(map[string]int64),
136		stakedAmount:         0,
137		accumulatedTimestamp: 0,
138		claimedTimestamp:     0,
139	}
140}