laconicd/utils/tx.go
Roy Crihfield dd39fb2365 [wip] refactor laconic app cli
- use runtime, server v2

- move generic tx decoder to utils
2025-02-13 11:41:58 +08:00

49 lines
880 B
Go

package utils
import (
"errors"
"cosmossdk.io/core/transaction"
"github.com/cosmos/cosmos-sdk/client"
)
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
}