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

emits.gno

2.92 Kb · 65 lines
 1package staker
 2
 3import (
 4	"chain"
 5
 6	"gno.land/p/gnoswap/utils/v1"
 7	"gno.land/r/gnoswap/gov/staker"
 8)
 9
10// emitUpdateEmissionRewardAccumulation emits the persisted global emission reward
11// accumulation state so that off-chain indexers can consume the authoritative
12// accumulator instead of re-deriving it. It must be emitted after every path that
13// calls updateAccumulatedRewardX128PerStake (add/remove/claim), otherwise the
14// off-chain accumulator drifts from the contract.
15//
16// All values are read from the manager's persisted state (post-update):
17//   - distributedAmount is GetDistributedAmount() (the stored baseline the contract
18//     will use for the next delta), not the live emission distribution snapshot.
19//   - totalStakedAmount reflects the current total after the stake change and is the
20//     divisor for the next accumulation delta.
21func emitUpdateEmissionRewardAccumulation(manager *staker.EmissionRewardManager) {
22	chain.Emit(
23		"UpdateEmissionRewardAccumulation",
24		"totalStakedAmount", utils.FormatInt(manager.GetTotalStakedAmount()),
25		"distributedAmount", utils.FormatInt(manager.GetDistributedAmount()),
26		"accumulatedRewardX128PerStake", manager.GetAccumulatedRewardX128PerStake().ToString(),
27		"accumulatedTimestamp", utils.FormatInt(manager.GetAccumulatedTimestamp()),
28	)
29}
30
31// emitUpdateProtocolFeeStakeAccrual emits the accrual epoch opened by a stake change
32// together with the total stake in force from it, so that off-chain indexers can
33// attribute fees bucketed under that epoch without re-deriving the stake history.
34func emitUpdateProtocolFeeStakeAccrual(manager *staker.ProtocolFeeRewardManager) {
35	chain.Emit(
36		"UpdateProtocolFeeStakeAccrual",
37		"epoch", utils.FormatInt(manager.GetCurrentEpoch()),
38		"totalStakedAmount", utils.FormatInt(manager.GetTotalStakedAmount()),
39	)
40}
41
42// emitUpdateProtocolFeeRewardAccumulation emits the persisted accumulation state of
43// every token that was just folded. Like the emission variant it must be emitted after
44// every path that folds accrual buckets, otherwise the off-chain accumulator drifts
45// from the contract.
46func emitUpdateProtocolFeeRewardAccumulation(manager *staker.ProtocolFeeRewardManager, tokenPaths []string) {
47	for _, tokenPath := range tokenPaths {
48		accumulatedProtocolFeeX128PerStake := "0"
49		foldedEpoch := int64(-1)
50		if accumulator, ok := manager.GetTokenAccumulator(tokenPath); ok {
51			accumulatedProtocolFeeX128PerStake = accumulator.GetAccumulatedX128PerStake().ToString()
52			foldedEpoch = accumulator.GetFoldedEpoch()
53		}
54
55		chain.Emit(
56			"UpdateProtocolFeeRewardAccumulation",
57			"tokenPath", tokenPath,
58			"totalStakedAmount", utils.FormatInt(manager.GetTotalStakedAmount()),
59			"protocolFeeAmount", utils.FormatInt(manager.GetProtocolFeeAmount(tokenPath)),
60			"accumulatedProtocolFeeX128PerStake", accumulatedProtocolFeeX128PerStake,
61			"foldedEpoch", utils.FormatInt(foldedEpoch),
62			"accumulatedTimestamp", utils.FormatInt(manager.GetAccumulatedTimestamp()),
63		)
64	}
65}