Skip to content

Commit

Permalink
feat(go/client): implement opts to setup go client (#129)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian authored Mar 6, 2024
1 parent 26a6e24 commit 3bf7e6e
Show file tree
Hide file tree
Showing 4 changed files with 357 additions and 87 deletions.
11 changes: 6 additions & 5 deletions go/node/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import (
"context"
"errors"

"github.com/spf13/pflag"

sdkclient "github.com/cosmos/cosmos-sdk/client"
tmjclient "github.com/tendermint/tendermint/rpc/jsonrpc/client"

cltypes "github.com/akash-network/akash-api/go/node/client/types"
"github.com/akash-network/akash-api/go/node/client/v1beta2"
)

var (
ErrUnknownClientVersion = errors.New("akash-api: unknown client version")
)

func DiscoverClient(ctx context.Context, cctx sdkclient.Context, flags *pflag.FlagSet, setup func(interface{}) error) error {
type SetupFn func(interface{}) error

func DiscoverClient(ctx context.Context, cctx sdkclient.Context, setup SetupFn, opts ...cltypes.ClientOption) error {
rpc, err := tmjclient.New(cctx.NodeURI)
if err != nil {
return err
Expand All @@ -39,7 +40,7 @@ func DiscoverClient(ctx context.Context, cctx sdkclient.Context, flags *pflag.Fl

switch result.ClientInfo.ApiVersion {
case "v1beta2":
cl, err = v1beta2.NewClient(ctx, cctx, flags)
cl, err = v1beta2.NewClient(ctx, cctx, opts...)
default:
err = ErrUnknownClientVersion
}
Expand All @@ -55,7 +56,7 @@ func DiscoverClient(ctx context.Context, cctx sdkclient.Context, flags *pflag.Fl
return nil
}

func DiscoverQueryClient(ctx context.Context, cctx sdkclient.Context, setup func(interface{}) error) error {
func DiscoverQueryClient(ctx context.Context, cctx sdkclient.Context, setup SetupFn) error {
rpc, err := tmjclient.New(cctx.NodeURI)
if err != nil {
return err
Expand Down
181 changes: 181 additions & 0 deletions go/node/client/types/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package types

import (
"time"

"github.com/spf13/pflag"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
)

type ClientOptions struct {
AccountNumber uint64
AccountSequence uint64
GasAdjustment float64
Gas flags.GasSetting
GasPrices string
Fees string
Note string
TimeoutHeight uint64
BroadcastTimeout time.Duration
}

type ClientOption func(options *ClientOptions) error

// NewTxFactory creates a new Factory.
func NewTxFactory(cctx client.Context, opts ...ClientOption) (tx.Factory, error) {
clOpts := &ClientOptions{}

for _, opt := range opts {
if err := opt(clOpts); err != nil {
return tx.Factory{}, err
}
}

signMode := signing.SignMode_SIGN_MODE_UNSPECIFIED
switch cctx.SignModeStr {
case flags.SignModeDirect:
signMode = signing.SignMode_SIGN_MODE_DIRECT
case flags.SignModeLegacyAminoJSON:
signMode = signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON
case flags.SignModeEIP191:
signMode = signing.SignMode_SIGN_MODE_EIP_191
}

txf := tx.Factory{}

txf = txf.WithTxConfig(cctx.TxConfig).
WithAccountRetriever(cctx.AccountRetriever).
WithAccountNumber(clOpts.AccountNumber).
WithSequence(clOpts.AccountSequence).
WithKeybase(cctx.Keyring).
WithChainID(cctx.ChainID).
WithGas(clOpts.Gas.Gas).
WithGasAdjustment(clOpts.GasAdjustment).
WithGasPrices(clOpts.GasPrices).
WithSimulateAndExecute(clOpts.Gas.Simulate).
WithTimeoutHeight(clOpts.TimeoutHeight).
WithMemo(clOpts.Note).
WithSignMode(signMode).
WithFees(clOpts.Fees)

if !cctx.Offline {
from := cctx.GetFromAddress()

if err := txf.AccountRetriever().EnsureExists(cctx, from); err != nil {
return txf, err
}

if txf.AccountNumber() == 0 || txf.Sequence() == 0 {
num, seq, err := txf.AccountRetriever().GetAccountNumberSequence(cctx, from)
if err != nil {
return txf, err
}

txf = txf.WithAccountNumber(num).WithSequence(seq)
}
}

return txf, nil
}

func WithAccountNumber(val uint64) ClientOption {
return func(options *ClientOptions) error {
options.AccountNumber = val
return nil
}
}

func WithAccountSequence(val uint64) ClientOption {
return func(options *ClientOptions) error {
options.AccountSequence = val
return nil
}
}

func WithGasAdjustment(val float64) ClientOption {
return func(options *ClientOptions) error {
options.GasAdjustment = val
return nil
}
}

func WithNote(val string) ClientOption {
return func(options *ClientOptions) error {
options.Note = val
return nil
}
}

func WithGas(val flags.GasSetting) ClientOption {
return func(options *ClientOptions) error {
options.Gas = val
return nil
}
}

func WithGasPrices(val string) ClientOption {
return func(options *ClientOptions) error {
options.GasPrices = val
return nil
}
}

func WithFees(val string) ClientOption {
return func(options *ClientOptions) error {
options.Fees = val
return nil
}
}

func WithTimeoutHeight(val uint64) ClientOption {
return func(options *ClientOptions) error {
options.TimeoutHeight = val
return nil
}
}

func ClientOptionsFromFlags(flagSet *pflag.FlagSet) ([]ClientOption, error) {
opts := make([]ClientOption, 0)

if flagSet.Changed(flags.FlagAccountNumber) {
accNum, _ := flagSet.GetUint64(flags.FlagAccountNumber)
opts = append(opts, WithAccountNumber(accNum))
}

if flagSet.Changed(flags.FlagSequence) {
accSeq, _ := flagSet.GetUint64(flags.FlagSequence)
opts = append(opts, WithAccountSequence(accSeq))
}

// if flagSet.Changed(flags.FlagGasAdjustment) {
gasAdj, _ := flagSet.GetFloat64(flags.FlagGasAdjustment)
opts = append(opts, WithGasAdjustment(gasAdj))
// }

if flagSet.Changed(flags.FlagNote) {
memo, _ := flagSet.GetString(flags.FlagNote)
opts = append(opts, WithNote(memo))
}

if flagSet.Changed(flags.FlagTimeoutHeight) {
timeoutHeight, _ := flagSet.GetUint64(flags.FlagTimeoutHeight)
opts = append(opts, WithTimeoutHeight(timeoutHeight))
}

// if flagSet.Changed(flags.FlagGas) {
gasStr, _ := flagSet.GetString(flags.FlagGas)
gasSetting, _ := flags.ParseGasSetting(gasStr)
opts = append(opts, WithGas(gasSetting))
// }

// if flagSet.Changed(flags.FlagFees) {
feesStr, _ := flagSet.GetString(flags.FlagFees)
opts = append(opts, WithFees(feesStr))
// }

return opts, nil
}
12 changes: 5 additions & 7 deletions go/node/client/v1beta2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import (
"context"
"fmt"

"github.com/gogo/protobuf/proto"
"github.com/spf13/pflag"

tmrpc "github.com/tendermint/tendermint/rpc/core/types"

sdkclient "github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
tmrpc "github.com/tendermint/tendermint/rpc/core/types"

atypes "github.com/akash-network/akash-api/go/node/audit/v1beta3"
ctypes "github.com/akash-network/akash-api/go/node/cert/v1beta3"
cltypes "github.com/akash-network/akash-api/go/node/client/types"
dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
mtypes "github.com/akash-network/akash-api/go/node/market/v1beta4"
ptypes "github.com/akash-network/akash-api/go/node/provider/v1beta3"
Expand Down Expand Up @@ -56,7 +54,7 @@ type client struct {

var _ Client = (*client)(nil)

func NewClient(ctx context.Context, cctx sdkclient.Context, flags *pflag.FlagSet) (Client, error) {
func NewClient(ctx context.Context, cctx sdkclient.Context, opts ...cltypes.ClientOption) (Client, error) {
nd := newNode(cctx)

cl := &client{
Expand All @@ -65,7 +63,7 @@ func NewClient(ctx context.Context, cctx sdkclient.Context, flags *pflag.FlagSet
}

var err error
cl.tx, err = newSerialTx(ctx, cctx, flags, nd, BroadcastDefaultTimeout)
cl.tx, err = newSerialTx(ctx, cctx, nd, opts...)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 3bf7e6e

Please sign in to comment.