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.43 Kb · 52 lines
 1package staker
 2
 3import (
 4	"chain"
 5
 6	"gno.land/p/gnoswap/utils"
 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// emitUpdateProtocolFeeRewardAccumulation emits the persisted per-token protocol fee
32// reward accumulation state for every distributed token. Like the emission variant it
33// must be emitted after every path that calls updateAccumulatedProtocolFeeX128PerStake
34// (add/remove/claim). protocolFeeAmount and the accumulator are read from the manager's
35// persisted state so the emitted tuple stays internally consistent.
36func emitUpdateProtocolFeeRewardAccumulation(manager *staker.ProtocolFeeRewardManager, distributedAmounts map[string]int64) {
37	for tokenPath := range distributedAmounts {
38		accumulatedProtocolFeeX128PerStake := "0"
39		if acc := manager.GetAccumulatedProtocolFeeX128PerStake(tokenPath); acc != nil {
40			accumulatedProtocolFeeX128PerStake = acc.ToString()
41		}
42
43		chain.Emit(
44			"UpdateProtocolFeeRewardAccumulation",
45			"tokenPath", tokenPath,
46			"totalStakedAmount", utils.FormatInt(manager.GetTotalStakedAmount()),
47			"protocolFeeAmount", utils.FormatInt(manager.GetProtocolFeeAmounts()[tokenPath]),
48			"accumulatedProtocolFeeX128PerStake", accumulatedProtocolFeeX128PerStake,
49			"accumulatedTimestamp", utils.FormatInt(manager.GetAccumulatedTimestamp()),
50		)
51	}
52}