emission_reward_state.gno
4.91 Kb · 130 lines
1package staker
2
3import (
4 u256 "gno.land/p/gnoswap/uint256/v1"
5)
6
7// EmissionRewardState tracks emission reward information for an individual staker.
8// This struct maintains reward debt, accumulated rewards, and claiming history
9// to ensure accurate reward calculations and prevent double-claiming.
10type EmissionRewardState struct {
11 // rewardDebtX128 represents the reward debt with 128-bit precision scaling
12 // Used to calculate rewards earned since the last update
13 rewardDebtX128 *u256.Uint
14 // accumulatedRewardAmount is the total rewards accumulated but not yet claimed
15 accumulatedRewardAmount int64
16 // accumulatedTimestamp is the last timestamp when rewards were accumulated
17 accumulatedTimestamp int64
18 // claimedRewardAmount is the total amount of rewards that have been claimed
19 claimedRewardAmount 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
26// NewEmissionRewardState creates a new emission reward state for a staker.
27// This factory function initializes the state with the current system reward debt.
28//
29// Parameters:
30// - accumulatedRewardX128PerStake: current system-wide accumulated reward per stake
31//
32// Returns:
33// - *EmissionRewardState: new emission reward state instance
34func NewEmissionRewardState(accumulatedRewardX128PerStake *u256.Uint) *EmissionRewardState {
35 return &EmissionRewardState{
36 // Deep copy the input to snapshot the current accumulator value.
37 rewardDebtX128: accumulatedRewardX128PerStake.Clone(),
38 accumulatedRewardAmount: 0,
39 accumulatedTimestamp: 0,
40 claimedRewardAmount: 0,
41 claimedTimestamp: 0,
42 stakedAmount: 0,
43 }
44}
45
46/* Getters */
47
48// GetRewardDebtX128 returns the scaled reward debt used for accrual calculations.
49//
50// Returns:
51// - *u256.Uint: current reward debt in the protocol's 128-bit fixed-point scale.
52func (e *EmissionRewardState) GetRewardDebtX128() *u256.Uint { return e.rewardDebtX128 }
53
54// GetAccumulatedRewardAmount returns rewards accrued but not yet claimed.
55//
56// Returns:
57// - int64: accumulated reward amount in the smallest token unit.
58func (e *EmissionRewardState) GetAccumulatedRewardAmount() int64 { return e.accumulatedRewardAmount }
59
60// GetAccumulatedTimestamp returns when accumulated rewards were last updated.
61//
62// Returns:
63// - int64: Unix timestamp in seconds of the last accumulation update.
64func (e *EmissionRewardState) GetAccumulatedTimestamp() int64 { return e.accumulatedTimestamp }
65
66// GetClaimedRewardAmount returns the total rewards already claimed.
67//
68// Returns:
69// - int64: claimed reward amount in the smallest token unit.
70func (e *EmissionRewardState) GetClaimedRewardAmount() int64 { return e.claimedRewardAmount }
71
72// GetClaimedTimestamp returns when rewards were last claimed.
73//
74// Returns:
75// - int64: Unix timestamp in seconds of the last claim.
76func (e *EmissionRewardState) GetClaimedTimestamp() int64 { return e.claimedTimestamp }
77
78// GetStakedAmount returns the amount currently staked for this state.
79//
80// Returns:
81// - int64: staked token amount in the smallest token unit.
82func (e *EmissionRewardState) GetStakedAmount() int64 { return e.stakedAmount }
83
84/* Setters */
85// SetRewardDebtX128 replaces the reward debt with a copied fixed-point value.
86//
87// Parameters:
88// - rewardDebtX128: reward debt in the protocol's 128-bit fixed-point scale.
89func (e *EmissionRewardState) SetRewardDebtX128(rewardDebtX128 *u256.Uint) {
90 e.rewardDebtX128 = u256.Zero().Set(rewardDebtX128)
91}
92
93// SetAccumulatedRewardAmount replaces the unclaimed accumulated reward amount.
94//
95// Parameters:
96// - accumulatedRewardAmount: accumulated reward in the smallest token unit.
97func (e *EmissionRewardState) SetAccumulatedRewardAmount(accumulatedRewardAmount int64) {
98 e.accumulatedRewardAmount = accumulatedRewardAmount
99}
100
101// SetAccumulatedTimestamp records the latest reward accumulation timestamp.
102//
103// Parameters:
104// - accumulatedTimestamp: Unix timestamp in seconds for the accumulation update.
105func (e *EmissionRewardState) SetAccumulatedTimestamp(accumulatedTimestamp int64) {
106 e.accumulatedTimestamp = accumulatedTimestamp
107}
108
109// SetClaimedRewardAmount replaces the total claimed reward amount.
110//
111// Parameters:
112// - claimedRewardAmount: claimed reward in the smallest token unit.
113func (e *EmissionRewardState) SetClaimedRewardAmount(claimedRewardAmount int64) {
114 e.claimedRewardAmount = claimedRewardAmount
115}
116
117// SetClaimedTimestamp records the latest reward claim timestamp.
118//
119// Parameters:
120// - claimedTimestamp: Unix timestamp in seconds for the claim.
121func (e *EmissionRewardState) SetClaimedTimestamp(claimedTimestamp int64) {
122 e.claimedTimestamp = claimedTimestamp
123}
124
125// SetStakedAmount replaces the current staked amount.
126// Parameters:
127// - stakedAmount: staked token amount in the smallest token unit.
128func (e *EmissionRewardState) SetStakedAmount(stakedAmount int64) {
129 e.stakedAmount = stakedAmount
130}