-
Notifications
You must be signed in to change notification settings - Fork 119
/
distribution.go
61 lines (53 loc) · 2.01 KB
/
distribution.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
package keeper
import (
"fmt"
"github.com/Sifchain/sifnode/x/dispensation/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
)
// This package adds set and get operations for distributions
func (k Keeper) SetDistribution(ctx sdk.Context, ar types.Distribution) error {
if !ar.Validate() {
return errors.Wrapf(types.ErrInvalid, "Record Details : %s", ar.String())
}
store := ctx.KVStore(k.storeKey)
key := types.GetDistributionsKey(ar.DistributionName, ar.DistributionType, ar.Runner)
store.Set(key, k.cdc.MustMarshal(&ar))
return nil
}
func (k Keeper) GetDistribution(ctx sdk.Context, name string, distributionType types.DistributionType, authorizedRunner string) (*types.Distribution, error) {
var ar types.Distribution
store := ctx.KVStore(k.storeKey)
key := types.GetDistributionsKey(name, distributionType, authorizedRunner)
if !k.Exists(ctx, key) {
return &ar, errors.Wrapf(types.ErrInvalid, "Record Does not Exist : %s", ar.String())
}
bz := store.Get(key)
k.cdc.MustUnmarshal(bz, &ar)
return &ar, nil
}
func (k Keeper) ExistsDistribution(ctx sdk.Context, name string, distributionType types.DistributionType, authorizedRunner string) bool {
key := types.GetDistributionsKey(name, distributionType, authorizedRunner)
return k.Exists(ctx, key)
}
func (k Keeper) GetDistributionIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.DistributionsPrefix)
}
func (k Keeper) GetDistributions(ctx sdk.Context) *types.Distributions {
var res types.Distributions
iterator := k.GetDistributionIterator(ctx)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var dl types.Distribution
bytesValue := iterator.Value()
err := k.cdc.Unmarshal(bytesValue, &dl)
if err != nil {
// Log unmarshal distribution error instead of panic.
ctx.Logger().Error(fmt.Sprintf("Unmarshal failed for distribution bytes : %s ", bytesValue))
continue
}
res.Distributions = append(res.Distributions, &dl)
}
return &res
}