forked from tendermint/spn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinator_by_address.go
70 lines (59 loc) · 2.15 KB
/
coordinator_by_address.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
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/spn/x/profile/types"
)
// SetCoordinatorByAddress set a specific coordinatorByAddress in the store from its index
func (k Keeper) SetCoordinatorByAddress(ctx sdk.Context, coordinatorByAddress types.CoordinatorByAddress) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.CoordinatorByAddressKeyPrefix))
b := k.cdc.MustMarshalBinaryBare(&coordinatorByAddress)
store.Set(types.CoordinatorByAddressKey(
coordinatorByAddress.Address,
), b)
}
// GetCoordinatorByAddress returns a coordinatorByAddress from its index
func (k Keeper) GetCoordinatorByAddress(
ctx sdk.Context,
address string,
) (val types.CoordinatorByAddress, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.CoordinatorByAddressKeyPrefix))
b := store.Get(types.CoordinatorByAddressKey(
address,
))
if b == nil {
return val, false
}
k.cdc.MustUnmarshalBinaryBare(b, &val)
return val, true
}
// RemoveCoordinatorByAddress removes a coordinatorByAddress from the store
func (k Keeper) RemoveCoordinatorByAddress(
ctx sdk.Context,
address string,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.CoordinatorByAddressKeyPrefix))
store.Delete(types.CoordinatorByAddressKey(
address,
))
}
// GetAllCoordinatorByAddress returns all coordinatorByAddress
func (k Keeper) GetAllCoordinatorByAddress(ctx sdk.Context) (list []types.CoordinatorByAddress) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.CoordinatorByAddressKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.CoordinatorByAddress
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &val)
list = append(list, val)
}
return
}
// CoordinatorIDFromAddress returns the coordinator id associated to an address
func (k Keeper) CoordinatorIDFromAddress(
ctx sdk.Context,
address string,
) (id uint64, found bool) {
coord, found := k.GetCoordinatorByAddress(ctx, address)
return coord.CoordinatorId, found
}