/
keeper.go
89 lines (77 loc) · 2.59 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package keeper
import (
"github.com/cometbft/cometbft/libs/log"
tmbytes "github.com/cometbft/cometbft/libs/bytes"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/bianjieai/tibc-go/modules/tibc/apps/nft_transfer/types"
host "github.com/bianjieai/tibc-go/modules/tibc/core/24-host"
)
type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
paramSpace paramtypes.Subspace
ak types.AccountKeeper
nk types.NftKeeper
pk types.PacketKeeper
ck types.ClientKeeper
}
// NewKeeper creates a new TIBC transfer Keeper instance
func NewKeeper(
cdc codec.BinaryCodec,
key storetypes.StoreKey,
paramSpace paramtypes.Subspace,
ak types.AccountKeeper,
nk types.NftKeeper,
pk types.PacketKeeper,
ck types.ClientKeeper,
) Keeper {
if addr := ak.GetModuleAddress(types.ModuleName); addr == nil {
panic("the TIBC nft-transfer module account has not been set")
}
return Keeper{
cdc: cdc,
storeKey: key,
paramSpace: paramSpace,
ak: ak,
nk: nk,
pk: pk,
ck: ck,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+host.ModuleName+"-"+types.ModuleName)
}
// GetNftTransferModuleAddr returns the nft transfer module addr
func (k Keeper) GetNftTransferModuleAddr(name string) sdk.AccAddress {
return k.ak.GetModuleAddress(name)
}
// HasClassTrace checks if a the key with the given class trace hash exists on the store.
func (k Keeper) HasClassTrace(ctx sdk.Context, classTraceHash tmbytes.HexBytes) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ClassTraceKey)
return store.Has(classTraceHash)
}
// SetClassTrace sets a new {trace hash -> class trace} pair to the store.
func (k Keeper) SetClassTrace(ctx sdk.Context, classTrace types.ClassTrace) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ClassTraceKey)
bz := k.MustMarshalClassTrace(classTrace)
store.Set(classTrace.Hash(), bz)
}
// GetClassTrace retreives the full identifiers trace and base class from the store.
func (k Keeper) GetClassTrace(
ctx sdk.Context,
classTraceHash tmbytes.HexBytes,
) (types.ClassTrace, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.ClassTraceKey)
bz := store.Get(classTraceHash)
if bz == nil {
return types.ClassTrace{}, false
}
var classTrace types.ClassTrace
k.cdc.MustUnmarshal(bz, &classTrace)
return classTrace, true
}