-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
67 lines (52 loc) · 1.49 KB
/
keeper.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
package internal
import (
"cosmossdk.io/collections"
"cosmossdk.io/core/store"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
testv1alpha1 "github.com/0tech/andromeda/x/test/andromeda/test/v1alpha1"
)
type Keeper struct {
cdc codec.Codec
schema collections.Schema
assets collections.Map[collections.Pair[sdk.AccAddress, string], testv1alpha1.Asset]
}
func NewKeeper(
cdc codec.Codec,
storeService store.KVStoreService,
prefix []byte,
) (*Keeper, error) {
sb := collections.NewSchemaBuilder(storeService)
if len(prefix) == 0 {
prefix = assetsKeyPrefix
}
k := &Keeper{
cdc: cdc,
assets: collections.NewMap(sb, prefix, "assets",
collections.PairKeyCodec(sdk.AccAddressKey, collections.StringKey),
codec.CollValue[testv1alpha1.Asset](cdc),
),
}
schema, err := sb.Build()
if err != nil {
return nil, err
}
k.schema = schema
return k, nil
}
func (k Keeper) addressBytesToString(addr []byte) (string, error) {
addressCodec := k.cdc.InterfaceRegistry().SigningContext().AddressCodec()
addrStr, err := addressCodec.BytesToString(addr)
if err != nil {
return "", testv1alpha1.ErrInvalidAddress.Wrap(err.Error())
}
return addrStr, nil
}
func (k Keeper) addressStringToBytes(addr string) (sdk.AccAddress, error) {
addressCodec := k.cdc.InterfaceRegistry().SigningContext().AddressCodec()
addrBytes, err := addressCodec.StringToBytes(addr)
if err != nil {
return nil, testv1alpha1.ErrInvalidAddress.Wrap(err.Error())
}
return addrBytes, nil
}