Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

getter_utils.gno

1.31 Kb · 53 lines
 1package staker
 2
 3import (
 4	sr "gno.land/r/gnoswap/staker"
 5)
 6
 7// The functions below are makeEntrySafeFn transformations handed to
 8// rotree.Wrap. They run on every read through a read-only view, so each one
 9// returns a value the caller may hold without reaching realm-owned state.
10
11// cloneExternalIncentiveEntry is the transformation for a pool's incentive
12// tree. ExternalIncentive is mutable and its setters are declared in
13// /r/gnoswap/staker, so an external holder of the stored pointer could write
14// realm state through it.
15func cloneExternalIncentiveEntry(value any) any {
16	incentive, ok := value.(*sr.ExternalIncentive)
17	if !ok {
18		return nil
19	}
20
21	return incentive.Clone()
22}
23
24// rewardCacheEntry, accumulationEntry, and historicalTickEntry are the
25// transformations for the UintTree-backed views. Their values are immutable
26// scalars, so nothing needs copying; they type-check instead, which keeps the
27// filtering the replaced slice getters did when an entry failed its cast.
28func rewardCacheEntry(value any) any {
29	reward, ok := value.(int64)
30	if !ok {
31		return nil
32	}
33
34	return reward
35}
36
37func accumulationEntry(value any) any {
38	accumulation, ok := value.(string)
39	if !ok {
40		return nil
41	}
42
43	return accumulation
44}
45
46func historicalTickEntry(value any) any {
47	tick, ok := value.(int32)
48	if !ok {
49		return nil
50	}
51
52	return tick
53}