-
Notifications
You must be signed in to change notification settings - Fork 40
/
pool_rewards_accum.go
122 lines (108 loc) · 3.89 KB
/
pool_rewards_accum.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package keeper
import (
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/masterchef/types"
)
func (k Keeper) GetPoolRewardsAccum(ctx sdk.Context, poolId, timestamp uint64) (types.PoolRewardsAccum, error) {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.GetPoolRewardsAccumKey(poolId, timestamp))
if b == nil {
return types.PoolRewardsAccum{}, types.ErrPoolRewardsAccumNotFound
}
accum := types.PoolRewardsAccum{}
k.cdc.MustUnmarshal(b, &accum)
return accum, nil
}
func (k Keeper) SetPoolRewardsAccum(ctx sdk.Context, accum types.PoolRewardsAccum) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&accum)
store.Set(types.GetPoolRewardsAccumKey(accum.PoolId, accum.Timestamp), bz)
}
func (k Keeper) DeletePoolRewardsAccum(ctx sdk.Context, accum types.PoolRewardsAccum) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetPoolRewardsAccumKey(accum.PoolId, accum.Timestamp))
}
func (k Keeper) GetAllPoolRewardsAccum(ctx sdk.Context) (list []types.PoolRewardsAccum) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PoolRewardsAccumKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.PoolRewardsAccum
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
func (k Keeper) IterateAllPoolRewardsAccum(ctx sdk.Context, handler func(accum types.PoolRewardsAccum) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, []byte(types.PoolRewardsAccumKeyPrefix))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
accum := types.PoolRewardsAccum{}
k.cdc.MustUnmarshal(iter.Value(), &accum)
if handler(accum) {
break
}
}
}
func (k Keeper) IteratePoolRewardsAccum(ctx sdk.Context, poolId uint64, handler func(accum types.PoolRewardsAccum) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.GetPoolRewardsAccumPrefix(poolId))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
accum := types.PoolRewardsAccum{}
k.cdc.MustUnmarshal(iter.Value(), &accum)
if handler(accum) {
break
}
}
}
func (k Keeper) FirstPoolRewardsAccum(ctx sdk.Context, poolId uint64) types.PoolRewardsAccum {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.GetPoolRewardsAccumPrefix(poolId))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
accum := types.PoolRewardsAccum{}
k.cdc.MustUnmarshal(iter.Value(), &accum)
return accum
}
return types.PoolRewardsAccum{}
}
func (k Keeper) LastPoolRewardsAccum(ctx sdk.Context, poolId uint64) types.PoolRewardsAccum {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStoreReversePrefixIterator(store, types.GetPoolRewardsAccumPrefix(poolId))
defer iter.Close()
for ; iter.Valid(); iter.Next() {
accum := types.PoolRewardsAccum{}
k.cdc.MustUnmarshal(iter.Value(), &accum)
return accum
}
return types.PoolRewardsAccum{
PoolId: poolId,
BlockHeight: 0,
Timestamp: 0,
DexReward: math.LegacyZeroDec(),
GasReward: math.LegacyZeroDec(),
EdenReward: math.LegacyZeroDec(),
}
}
func (k Keeper) AddPoolRewardsAccum(ctx sdk.Context, poolId, timestamp uint64, height int64, dexReward, gasReward, edenReward math.LegacyDec) {
lastAccum := k.LastPoolRewardsAccum(ctx, poolId)
lastAccum.Timestamp = timestamp
lastAccum.BlockHeight = height
if lastAccum.DexReward.IsNil() {
lastAccum.DexReward = math.LegacyZeroDec()
}
if lastAccum.GasReward.IsNil() {
lastAccum.GasReward = math.LegacyZeroDec()
}
if lastAccum.EdenReward.IsNil() {
lastAccum.EdenReward = math.LegacyZeroDec()
}
lastAccum.DexReward = lastAccum.DexReward.Add(dexReward)
lastAccum.GasReward = lastAccum.GasReward.Add(gasReward)
lastAccum.EdenReward = lastAccum.EdenReward.Add(edenReward)
k.SetPoolRewardsAccum(ctx, lastAccum)
}