Skip to content

Commit

Permalink
Merge pull request #148 from InjectiveLabs/f/customizable-tx-factory
Browse files Browse the repository at this point in the history
feat(chain-client) allows to customize the txFactory as an option
  • Loading branch information
hmoragrega authored Aug 23, 2023
2 parents 7f970bb + 17035d1 commit 8049158
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
11 changes: 8 additions & 3 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,14 @@ func NewChainClient(
}

// init tx factory
txFactory := NewTxFactory(ctx)
if len(opts.GasPrices) > 0 {
txFactory = txFactory.WithGasPrices(opts.GasPrices)
var txFactory tx.Factory
if opts.TxFactory == nil {
txFactory = NewTxFactory(ctx)
if len(opts.GasPrices) > 0 {
txFactory = txFactory.WithGasPrices(opts.GasPrices)
}
} else {
txFactory = *opts.TxFactory
}

// init grpc connection
Expand Down
9 changes: 9 additions & 0 deletions client/common/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
ctypes "github.com/InjectiveLabs/sdk-go/chain/types"
log "github.com/InjectiveLabs/suplog"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"
"google.golang.org/grpc/credentials"
Expand All @@ -20,6 +21,7 @@ func init() {
type ClientOptions struct {
GasPrices string
TLSCert credentials.TransportCredentials
TxFactory *tx.Factory
}

type ClientOption func(opts *ClientOptions) error
Expand Down Expand Up @@ -52,3 +54,10 @@ func OptionTLSCert(tlsCert credentials.TransportCredentials) ClientOption {
return nil
}
}

func OptionTxFactory(txFactory *tx.Factory) ClientOption {
return func(opts *ClientOptions) error {
opts.TxFactory = txFactory
return nil
}
}
4 changes: 3 additions & 1 deletion examples/chain/20_MsgExec/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ func main() {

clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)

txFactory := chainclient.NewTxFactory(clientCtx)
txFactory = txFactory.WithGasPrices("500000000inj")
chainClient, err := chainclient.NewChainClient(
clientCtx,
network.ChainGrpcEndpoint,
common.OptionTLSCert(network.ChainTlsCert),
common.OptionGasPrices("500000000inj"),
common.OptionTxFactory(&txFactory),
)

if err != nil {
Expand Down

0 comments on commit 8049158

Please sign in to comment.