Skip to content

Commit

Permalink
Merge pull request #23 from DefiantLabs/pharr117/custom-msg-type-regi…
Browse files Browse the repository at this point in the history
…stry

pharr117/Add custom msg type registry map to client config, pull into codec during client creation
  • Loading branch information
pharr117 authored Sep 14, 2024
2 parents ffbbb18 + f28ea36 commit cde993a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
11 changes: 10 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,26 @@ type ChainClient struct {

func NewChainClient(ccc *ChainClientConfig, homepath string, input io.Reader, output io.Writer, kro ...keyring.Option) (*ChainClient, error) {
ccc.KeyDirectory = keysDir(homepath, ccc.ChainID)

codec, err := MakeCodec(ccc.Modules, ccc.CustomMsgTypeRegistry)

if err != nil {
return nil, err
}

cc := &ChainClient{
KeyringOptions: kro,
Config: ccc,
Input: input,
Output: output,
Codec: MakeCodec(ccc.Modules),
Codec: codec,
Logger: log.NewTMLogger(log.NewSyncWriter(output)),
}

if err := cc.Init(); err != nil {
return nil, err
}

return cc, nil
}

Expand Down
5 changes: 5 additions & 0 deletions client/codec/types/interface_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ func (registry *ProbeInterfaceRegistry) RegisterCustomTypeURL(iface interface{},
registry.registerImpl(iface, typeURL, impl)
}

func (registry *ProbeInterfaceRegistry) TypeURLIsRegistered(typeURL string) bool {
_, found := registry.typeURLMap[typeURL]
return found
}

// registerImpl registers a concrete type which implements the given
// interface under `typeURL`.
//
Expand Down
22 changes: 12 additions & 10 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package client

import (
"github.com/CosmWasm/wasmd/x/wasm"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
Expand Down Expand Up @@ -48,14 +49,15 @@ var (
)

type ChainClientConfig struct {
Key string `json:"key" yaml:"key"`
ChainID string `json:"chain-id" yaml:"chain-id"`
RPCAddr string `json:"rpc-addr" yaml:"rpc-addr"`
AccountPrefix string `json:"account-prefix" yaml:"account-prefix"`
KeyringBackend string `json:"keyring-backend" yaml:"keyring-backend"`
KeyDirectory string `json:"key-directory" yaml:"key-directory"`
Debug bool `json:"debug" yaml:"debug"`
Timeout string `json:"timeout" yaml:"timeout"`
OutputFormat string `json:"output-format" yaml:"output-format"`
Modules []module.AppModuleBasic `json:"-" yaml:"-"`
Key string `json:"key" yaml:"key"`
ChainID string `json:"chain-id" yaml:"chain-id"`
RPCAddr string `json:"rpc-addr" yaml:"rpc-addr"`
AccountPrefix string `json:"account-prefix" yaml:"account-prefix"`
KeyringBackend string `json:"keyring-backend" yaml:"keyring-backend"`
KeyDirectory string `json:"key-directory" yaml:"key-directory"`
Debug bool `json:"debug" yaml:"debug"`
Timeout string `json:"timeout" yaml:"timeout"`
OutputFormat string `json:"output-format" yaml:"output-format"`
Modules []module.AppModuleBasic `json:"-" yaml:"-"`
CustomMsgTypeRegistry map[string]sdkTypes.Msg `json:"-" yaml:"-"`
}
14 changes: 12 additions & 2 deletions client/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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"
Expand All @@ -12,6 +14,7 @@ import (
"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"
)
Expand All @@ -24,15 +27,22 @@ type Codec struct {
Amino *codec.LegacyAmino
}

func MakeCodec(moduleBasics []module.AppModuleBasic) Codec {
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)

return encodingConfig
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.
Expand Down

0 comments on commit cde993a

Please sign in to comment.