protocol_fee_reward_state.gno
5.13 Kb · 157 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
52// SetRewardDebtX128 stores a copy of the given map. Copying into a map
53// allocated by this domain method keeps the stored map owned by
54// /r/gnoswap/gov/staker, so later per-token writes (SetRewardDebtX128ForToken)
55// from any realm clear the cross-realm write guard. Storing the caller's map
56// directly would leave the map stamped with the caller's realm.
57func (p *ProtocolFeeRewardState) SetRewardDebtX128(rewardDebtX128 map[string]*u256.Uint) {
58 copied := make(map[string]*u256.Uint, len(rewardDebtX128))
59 for token, value := range rewardDebtX128 {
60 copied[token] = u256.Zero().Set(value)
61 }
62 p.rewardDebtX128 = copied
63}
64
65func (p *ProtocolFeeRewardState) SetAccumulatedRewards(accumulatedRewards map[string]int64) {
66 copied := make(map[string]int64, len(accumulatedRewards))
67 for token, value := range accumulatedRewards {
68 copied[token] = value
69 }
70 p.accumulatedRewards = copied
71}
72
73func (p *ProtocolFeeRewardState) SetClaimedRewards(claimedRewards map[string]int64) {
74 copied := make(map[string]int64, len(claimedRewards))
75 for token, value := range claimedRewards {
76 copied[token] = value
77 }
78 p.claimedRewards = copied
79}
80
81func (p *ProtocolFeeRewardState) SetAccumulatedTimestamp(accumulatedTimestamp int64) {
82 p.accumulatedTimestamp = accumulatedTimestamp
83}
84
85func (p *ProtocolFeeRewardState) SetClaimedTimestamp(claimedTimestamp int64) {
86 p.claimedTimestamp = claimedTimestamp
87}
88
89func (p *ProtocolFeeRewardState) SetStakedAmount(stakedAmount int64) {
90 p.stakedAmount = stakedAmount
91}
92
93// Additional getters for individual token rewards
94
95func (p *ProtocolFeeRewardState) GetRewardDebtX128ForToken(token string) *u256.Uint {
96 if p.rewardDebtX128 == nil {
97 return nil
98 }
99 return p.rewardDebtX128[token]
100}
101
102func (p *ProtocolFeeRewardState) GetAccumulatedRewardForToken(token string) int64 {
103 if p.accumulatedRewards == nil {
104 return 0
105 }
106 return p.accumulatedRewards[token]
107}
108
109func (p *ProtocolFeeRewardState) GetClaimedRewardForToken(token string) int64 {
110 if p.claimedRewards == nil {
111 return 0
112 }
113 return p.claimedRewards[token]
114}
115
116// Additional setters for individual token rewards
117
118func (p *ProtocolFeeRewardState) SetRewardDebtX128ForToken(token string, value *u256.Uint) {
119 if p.rewardDebtX128 == nil {
120 p.rewardDebtX128 = make(map[string]*u256.Uint)
121 }
122 p.rewardDebtX128[token] = u256.Zero().Set(value)
123}
124
125func (p *ProtocolFeeRewardState) SetAccumulatedRewardForToken(token string, value int64) {
126 if p.accumulatedRewards == nil {
127 p.accumulatedRewards = make(map[string]int64)
128 }
129 p.accumulatedRewards[token] = value
130}
131
132func (p *ProtocolFeeRewardState) SetClaimedRewardForToken(token string, value int64) {
133 if p.claimedRewards == nil {
134 p.claimedRewards = make(map[string]int64)
135 }
136 p.claimedRewards[token] = value
137}
138
139// NewProtocolFeeRewardState creates a new protocol fee reward state for a staker.
140// This factory function initializes the state with the current system reward debt for all tokens.
141func NewProtocolFeeRewardState(accumulatedProtocolFeeX128PerStake map[string]*u256.Uint) *ProtocolFeeRewardState {
142 rewardDebtX128 := make(map[string]*u256.Uint)
143
144 // Clone reward debt for each token to avoid reference issues
145 for token, accumulatedFee := range accumulatedProtocolFeeX128PerStake {
146 rewardDebtX128[token] = accumulatedFee.Clone()
147 }
148
149 return &ProtocolFeeRewardState{
150 rewardDebtX128: rewardDebtX128,
151 claimedRewards: make(map[string]int64),
152 accumulatedRewards: make(map[string]int64),
153 stakedAmount: 0,
154 accumulatedTimestamp: 0,
155 claimedTimestamp: 0,
156 }
157}