-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoding.go
74 lines (65 loc) · 3.03 KB
/
encoding.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
// APACHE NOTICE
// Sourced with modifications from https://github.com/strangelove-ventures/lens
package client
import (
"fmt"
osmosisGammTypes "github.com/DefiantLabs/probe/client/codec/osmosis/v15/x/gamm/types"
osmosisLockupTypes "github.com/DefiantLabs/probe/client/codec/osmosis/v15/x/lockup/types"
osmosisPoolManagerTypes "github.com/DefiantLabs/probe/client/codec/osmosis/v15/x/poolmanager/types"
tendermintLiquidityTypes "github.com/DefiantLabs/probe/client/codec/tendermint/liquidity/x/liquidity/types"
probeCodecTypes "github.com/DefiantLabs/probe/client/codec/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/std"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
)
type Codec struct {
ProbeInterfaceRegistry *probeCodecTypes.ProbeInterfaceRegistry
InterfaceRegistry types.InterfaceRegistry
Marshaler codec.Codec
TxConfig client.TxConfig
Amino *codec.LegacyAmino
}
func MakeCodec(moduleBasics []module.AppModuleBasic, customMsgTypeRegistry map[string]sdkTypes.Msg) (Codec, error) {
modBasic := module.NewBasicManager(moduleBasics...)
encodingConfig := MakeCodecConfig()
std.RegisterLegacyAminoCodec(encodingConfig.Amino)
std.RegisterInterfaces(encodingConfig.InterfaceRegistry)
modBasic.RegisterLegacyAminoCodec(encodingConfig.Amino)
modBasic.RegisterInterfaces(encodingConfig.InterfaceRegistry)
for typeURL, msg := range customMsgTypeRegistry {
if encodingConfig.ProbeInterfaceRegistry.TypeURLIsRegistered(typeURL) {
return Codec{}, fmt.Errorf("error registering custom message type in codec, typeURL %s is already registered", typeURL)
}
encodingConfig.ProbeInterfaceRegistry.RegisterCustomTypeURL((*sdkTypes.Msg)(nil), typeURL, msg)
}
return encodingConfig, nil
}
// Split out from base codec to not include explicitly.
// Should be included only when needed.
func RegisterOsmosisInterfaces(registry types.InterfaceRegistry) {
// Needs to be extended in order to cover all the modules
osmosisGammTypes.RegisterInterfaces(registry)
osmosisPoolManagerTypes.RegisterInterfaces(registry)
osmosisLockupTypes.RegisterInterfaces(registry)
}
// Split out from base codec to not include explicitly.
// Should be included only when needed.
func RegisterTendermintLiquidityInterfaces(aminoCodec *codec.LegacyAmino, registry types.InterfaceRegistry) {
tendermintLiquidityTypes.RegisterLegacyAminoCodec(aminoCodec)
tendermintLiquidityTypes.RegisterInterfaces(registry)
}
func MakeCodecConfig() Codec {
cosmosInterfaceRegistry, probeRegistry := probeCodecTypes.NewInterfaceRegistry()
marshaler := codec.NewProtoCodec(cosmosInterfaceRegistry)
return Codec{
ProbeInterfaceRegistry: probeRegistry,
InterfaceRegistry: cosmosInterfaceRegistry,
Marshaler: marshaler,
TxConfig: tx.NewTxConfig(marshaler, tx.DefaultSignModes),
Amino: codec.NewLegacyAmino(),
}
}