chore: move generic txdecoder (#22653)
This commit is contained in:
parent
a60dfdab12
commit
f153426ab2
@ -1,8 +1,10 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"cosmossdk.io/core/transaction"
|
||||
txsigning "cosmossdk.io/x/tx/signing"
|
||||
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
@ -68,3 +70,44 @@ type (
|
||||
SetExtensionOptions(extOpts ...*codectypes.Any)
|
||||
}
|
||||
)
|
||||
|
||||
var _ transaction.Codec[transaction.Tx] = &DefaultTxDecoder[transaction.Tx]{}
|
||||
|
||||
// DefaultTxDecoder is a generic transaction decoder that implements the transaction.Codec interface.
|
||||
type DefaultTxDecoder[T transaction.Tx] struct {
|
||||
TxConfig TxConfig
|
||||
}
|
||||
|
||||
// Decode decodes a binary transaction into type T using the TxConfig's TxDecoder.
|
||||
func (t *DefaultTxDecoder[T]) Decode(bz []byte) (T, error) {
|
||||
var out T
|
||||
tx, err := t.TxConfig.TxDecoder()(bz)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
|
||||
var ok bool
|
||||
out, ok = tx.(T)
|
||||
if !ok {
|
||||
return out, errors.New("unexpected Tx type")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// DecodeJSON decodes a JSON transaction into type T using the TxConfig's TxJSONDecoder.
|
||||
func (t *DefaultTxDecoder[T]) DecodeJSON(bz []byte) (T, error) {
|
||||
var out T
|
||||
tx, err := t.TxConfig.TxJSONDecoder()(bz)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
|
||||
var ok bool
|
||||
out, ok = tx.(T)
|
||||
if !ok {
|
||||
return out, errors.New("unexpected Tx type")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@ -108,7 +107,7 @@ func InitRootCmd[T transaction.Tx](
|
||||
simApp.Store(),
|
||||
simApp.App.AppManager,
|
||||
simApp.AppCodec(),
|
||||
&genericTxDecoder[T]{deps.TxConfig},
|
||||
&client.DefaultTxDecoder[T]{TxConfig: deps.TxConfig},
|
||||
simApp.App.QueryHandlers(),
|
||||
simApp.App.SchemaDecoderResolver(),
|
||||
initCometOptions[T](),
|
||||
@ -243,43 +242,3 @@ func RootCommandPersistentPreRun(clientCtx client.Context) func(*cobra.Command,
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var _ transaction.Codec[transaction.Tx] = &genericTxDecoder[transaction.Tx]{}
|
||||
|
||||
type genericTxDecoder[T transaction.Tx] struct {
|
||||
txConfig client.TxConfig
|
||||
}
|
||||
|
||||
// Decode implements transaction.Codec.
|
||||
func (t *genericTxDecoder[T]) Decode(bz []byte) (T, error) {
|
||||
var out T
|
||||
tx, err := t.txConfig.TxDecoder()(bz)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
|
||||
var ok bool
|
||||
out, ok = tx.(T)
|
||||
if !ok {
|
||||
return out, errors.New("unexpected Tx type")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// DecodeJSON implements transaction.Codec.
|
||||
func (t *genericTxDecoder[T]) DecodeJSON(bz []byte) (T, error) {
|
||||
var out T
|
||||
tx, err := t.txConfig.TxJSONDecoder()(bz)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
|
||||
var ok bool
|
||||
out, ok = tx.(T)
|
||||
if !ok {
|
||||
return out, errors.New("unexpected Tx type")
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user