-
Notifications
You must be signed in to change notification settings - Fork 40
/
external_incentive.go
63 lines (51 loc) · 2.28 KB
/
external_incentive.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
package keeper
import (
"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) GetExternalIncentiveIndex(ctx sdk.Context) (index uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ExternalIncentiveIndexKeyPrefix))
index = sdk.BigEndianToUint64(store.Get(types.ExternalIncentiveIndex()))
return index
}
func (k Keeper) SetExternalIncentiveIndex(ctx sdk.Context, index uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ExternalIncentiveIndexKeyPrefix))
store.Set(types.ExternalIncentiveIndex(), sdk.Uint64ToBigEndian(index))
}
func (k Keeper) AddExternalIncentive(ctx sdk.Context, externalIncentive types.ExternalIncentive) {
// Update external incentive index and increase +1
index := k.GetExternalIncentiveIndex(ctx)
externalIncentive.Id = index
k.SetExternalIncentiveIndex(ctx, index+1)
k.SetExternalIncentive(ctx, externalIncentive)
}
func (k Keeper) SetExternalIncentive(ctx sdk.Context, externalIncentive types.ExternalIncentive) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ExternalIncentiveKeyPrefix))
b := k.cdc.MustMarshal(&externalIncentive)
store.Set(types.ExternalIncentiveKey(externalIncentive.Id), b)
}
func (k Keeper) GetExternalIncentive(ctx sdk.Context, id uint64) (val types.ExternalIncentive, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ExternalIncentiveKeyPrefix))
b := store.Get(types.ExternalIncentiveKey(id))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
func (k Keeper) RemoveExternalIncentive(ctx sdk.Context, id uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ExternalIncentiveKeyPrefix))
store.Delete(types.ExternalIncentiveKey(id))
}
func (k Keeper) GetAllExternalIncentives(ctx sdk.Context) (list []types.ExternalIncentive) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ExternalIncentiveKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.ExternalIncentive
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}