/
incentives.go
111 lines (93 loc) · 3.08 KB
/
incentives.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
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/evmos/evmos/v10/x/incentives/types"
)
// GetAllIncentives - get all registered Incentives
func (k Keeper) GetAllIncentives(ctx sdk.Context) []types.Incentive {
incentives := []types.Incentive{}
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixIncentive)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var incentive types.Incentive
k.cdc.MustUnmarshal(iterator.Value(), &incentive)
incentives = append(incentives, incentive)
}
return incentives
}
// IterateIncentives iterates over all registered `Incentives` and performs a
// callback.
func (k Keeper) IterateIncentives(
ctx sdk.Context,
handlerFn func(incentive types.Incentive) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixIncentive)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var incentive types.Incentive
k.cdc.MustUnmarshal(iterator.Value(), &incentive)
if handlerFn(incentive) {
break
}
}
}
// GetIncentive - get registered incentive from the identifier
func (k Keeper) GetIncentive(
ctx sdk.Context,
contract common.Address,
) (types.Incentive, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixIncentive)
bz := store.Get(contract.Bytes())
if len(bz) == 0 {
return types.Incentive{}, false
}
var incentive types.Incentive
k.cdc.MustUnmarshal(bz, &incentive)
return incentive, true
}
// SetIncentive stores an incentive
func (k Keeper) SetIncentive(ctx sdk.Context, incentive types.Incentive) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixIncentive)
key := common.HexToAddress(incentive.Contract)
bz := k.cdc.MustMarshal(&incentive)
store.Set(key.Bytes(), bz)
}
// DeleteIncentiveAndUpdateAllocationMeters removes an incentive and updates the
// percentage of incentives allocated to each denomination.
func (k Keeper) DeleteIncentiveAndUpdateAllocationMeters(ctx sdk.Context, incentive types.Incentive) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixIncentive)
key := common.HexToAddress(incentive.Contract)
store.Delete(key.Bytes())
// Subtract allocations from allocation meters
for _, al := range incentive.Allocations {
// NOTE: existence of incentive is already checked
am, _ := k.GetAllocationMeter(ctx, al.Denom)
amount := am.Amount.Sub(al.Amount)
am = sdk.DecCoin{
Denom: al.Denom,
Amount: amount,
}
k.SetAllocationMeter(ctx, am)
}
}
// IsIncentiveRegistered - check if registered Incentive is registered
func (k Keeper) IsIncentiveRegistered(
ctx sdk.Context,
contract common.Address,
) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixIncentive)
return store.Has(contract.Bytes())
}
// Set total cummulative gas of a given incentive
func (k Keeper) SetIncentiveTotalGas(
ctx sdk.Context,
incentive types.Incentive,
gas uint64,
) {
incentive.TotalGas = gas
k.SetIncentive(ctx, incentive)
}