This repository has been archived by the owner on Mar 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
keeper.go
139 lines (113 loc) · 3.92 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
host "github.com/cosmos/cosmos-sdk/x/ibc/core/24-host"
"github.com/tendermint/tendermint/libs/log"
"github.com/chainapsis/cosmos-sdk-interchain-account/x/ibc-account/types"
)
func SerializeCosmosTx(cdc codec.BinaryMarshaler, registry codectypes.InterfaceRegistry) func(data interface{}) ([]byte, error) {
return func(data interface{}) ([]byte, error) {
msgs := make([]sdk.Msg, 0)
switch data := data.(type) {
case sdk.Msg:
msgs = append(msgs, data)
case []sdk.Msg:
msgs = append(msgs, data...)
default:
return nil, types.ErrInvalidOutgoingData
}
msgAnys := make([]*codectypes.Any, len(msgs))
for i, msg := range msgs {
var err error
msgAnys[i], err = codectypes.NewAnyWithValue(msg)
if err != nil {
return nil, err
}
}
txBody := &types.IBCTxBody{
Messages: msgAnys,
}
txRaw := &types.IBCTxRaw{
BodyBytes: cdc.MustMarshalBinaryBare(txBody),
}
bz, err := cdc.MarshalBinaryBare(txRaw)
if err != nil {
return nil, err
}
return bz, nil
}
}
// Keeper defines the IBC transfer keeper
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryMarshaler
// Key can be chain type which means what blockchain framework the host chain was built on or just direct chain id.
txEncoders map[string]types.TxEncoder
hook types.IBCAccountHooks
channelKeeper types.ChannelKeeper
portKeeper types.PortKeeper
accountKeeper types.AccountKeeper
scopedKeeper capabilitykeeper.ScopedKeeper
router types.Router
}
// NewKeeper creates a new IBC account Keeper instance
func NewKeeper(
cdc codec.BinaryMarshaler, key sdk.StoreKey,
txEncoders map[string]types.TxEncoder, channelKeeper types.ChannelKeeper, portKeeper types.PortKeeper,
accountKeeper types.AccountKeeper, scopedKeeper capabilitykeeper.ScopedKeeper, router types.Router,
) Keeper {
return Keeper{
storeKey: key,
cdc: cdc,
txEncoders: txEncoders,
channelKeeper: channelKeeper,
portKeeper: portKeeper,
accountKeeper: accountKeeper,
scopedKeeper: scopedKeeper,
router: router,
}
}
func (k Keeper) AddTxEncoder(typ string, txEncoder types.TxEncoder) error {
_, ok := k.txEncoders[typ]
if ok {
return types.ErrTxEncoderAlreadyRegistered
}
k.txEncoders[typ] = txEncoder
return nil
}
func (k Keeper) GetTxEncoder(typ string) (types.TxEncoder, bool) {
info, ok := k.txEncoders[typ]
return info, ok
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s-%s", host.ModuleName, types.ModuleName))
}
// IsBound checks if the interchain account module is already bound to the desired port
func (k Keeper) IsBound(ctx sdk.Context, portID string) bool {
_, ok := k.scopedKeeper.GetCapability(ctx, host.PortPath(portID))
return ok
}
// BindPort defines a wrapper function for the ort Keeper's function in
// order to expose it to module's InitGenesis function
func (k Keeper) BindPort(ctx sdk.Context, portID string) error {
// Set the portID into our store so we can retrieve it later
store := ctx.KVStore(k.storeKey)
store.Set([]byte(types.PortKey), []byte(portID))
cap := k.portKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, cap, host.PortPath(portID))
}
// GetPort returns the portID for the ibc account module. Used in ExportGenesis
func (k Keeper) GetPort(ctx sdk.Context) string {
store := ctx.KVStore(k.storeKey)
return string(store.Get([]byte(types.PortKey)))
}
// ClaimCapability allows the transfer module that can claim a capability that IBC module
// passes to it
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}