Skip to content

Commit 6bcbe4e

Browse files
authored
feat(cosmosclient): add tx options (incl memo setting) (#4276)
1 parent e7aa711 commit 6bcbe4e

File tree

5 files changed

+65
-24
lines changed

5 files changed

+65
-24
lines changed

Diff for: changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- [#4113](https://github.com/ignite/cli/pull/4113) Generate chain config documentation automatically
1818
- [#4131](https://github.com/ignite/cli/pull/4131) Support `bytes` as data type in the `scaffold` commands
1919
- [#4269](https://github.com/ignite/cli/pull/4269) Add custom flag parser for extensions
20+
- [#4276](https://github.com/ignite/cli/pull/4276) Add `cosmosclient.CreateTxWithOptions` method to facilite more custom tx creation
2021

2122
### Changes
2223

Diff for: ignite/pkg/cosmosclient/cosmosclient.go

+47-23
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ type Client struct {
136136
}
137137

138138
// Option configures your client.
139+
// Option, are global to the client and affect all transactions.
140+
// If you want to override a global option on a transaction, use the TxOptions struct.
139141
type Option func(*Client)
140142

141143
// WithHome sets the data dir of your chain. This option is used to access your chain's
@@ -177,12 +179,14 @@ func WithNodeAddress(addr string) Option {
177179
}
178180
}
179181

182+
// WithAddressPrefix sets the address prefix on the client.
180183
func WithAddressPrefix(prefix string) Option {
181184
return func(c *Client) {
182185
c.addressPrefix = prefix
183186
}
184187
}
185188

189+
// WithUseFaucet sets the faucet address on the client.
186190
func WithUseFaucet(faucetAddress, denom string, minAmount uint64) Option {
187191
return func(c *Client) {
188192
c.useFaucet = true
@@ -218,7 +222,8 @@ func WithGasAdjustment(gasAdjustment float64) Option {
218222
}
219223
}
220224

221-
// WithFees sets the fees (e.g. 10uatom).
225+
// WithFees sets the fees (e.g. 10uatom) on the client.
226+
// It will be used for all transactions if not overridden on the transaction options.
222227
func WithFees(fees string) Option {
223228
return func(c *Client) {
224229
c.fees = fees
@@ -546,15 +551,17 @@ func (c Client) BroadcastTx(ctx context.Context, account cosmosaccount.Account,
546551
return txService.Broadcast(ctx)
547552
}
548553

549-
func (c Client) CreateTx(goCtx context.Context, account cosmosaccount.Account, msgs ...sdktypes.Msg) (TxService, error) {
554+
// CreateTxWithOptions creates a transaction with the given options.
555+
// Options override global client options.
556+
func (c Client) CreateTxWithOptions(ctx context.Context, account cosmosaccount.Account, options TxOptions, msgs ...sdktypes.Msg) (TxService, error) {
550557
defer c.lockBech32Prefix()()
551558

552559
if c.useFaucet && !c.generateOnly {
553560
addr, err := account.Address(c.addressPrefix)
554561
if err != nil {
555562
return TxService{}, errors.WithStack(err)
556563
}
557-
if err := c.makeSureAccountHasTokens(goCtx, addr); err != nil {
564+
if err := c.makeSureAccountHasTokens(ctx, addr); err != nil {
558565
return TxService{}, err
559566
}
560567
}
@@ -564,36 +571,49 @@ func (c Client) CreateTx(goCtx context.Context, account cosmosaccount.Account, m
564571
return TxService{}, errors.WithStack(err)
565572
}
566573

567-
ctx := c.context.
574+
clientCtx := c.context.
568575
WithFromName(account.Name).
569576
WithFromAddress(sdkaddr)
570577

571-
txf, err := c.prepareFactory(ctx)
578+
txf, err := c.prepareFactory(clientCtx)
572579
if err != nil {
573580
return TxService{}, err
574581
}
575582

576-
if c.gasAdjustment != 0 && c.gasAdjustment != defaultGasAdjustment {
577-
txf = txf.WithGasAdjustment(c.gasAdjustment)
583+
if options.Memo != "" {
584+
txf = txf.WithMemo(options.Memo)
578585
}
579586

580-
var gas uint64
581-
if c.gas != "" && c.gas != GasAuto {
582-
gas, err = strconv.ParseUint(c.gas, 10, 64)
583-
if err != nil {
584-
return TxService{}, errors.WithStack(err)
585-
}
587+
txf = txf.WithFees(c.fees)
588+
if options.Fees != "" {
589+
txf = txf.WithFees(options.Fees)
590+
}
591+
592+
if options.GasLimit != 0 {
593+
txf = txf.WithGas(options.GasLimit)
586594
} else {
587-
_, gas, err = c.gasometer.CalculateGas(ctx, txf, msgs...)
588-
if err != nil {
589-
return TxService{}, errors.WithStack(err)
595+
if c.gasAdjustment != 0 && c.gasAdjustment != defaultGasAdjustment {
596+
txf = txf.WithGasAdjustment(c.gasAdjustment)
590597
}
591-
// the simulated gas can vary from the actual gas needed for a real transaction
592-
// we add an amount to ensure sufficient gas is provided
593-
gas += 20000
598+
599+
var gas uint64
600+
if c.gas != "" && c.gas != GasAuto {
601+
gas, err = strconv.ParseUint(c.gas, 10, 64)
602+
if err != nil {
603+
return TxService{}, errors.WithStack(err)
604+
}
605+
} else {
606+
_, gas, err = c.gasometer.CalculateGas(clientCtx, txf, msgs...)
607+
if err != nil {
608+
return TxService{}, errors.WithStack(err)
609+
}
610+
// the simulated gas can vary from the actual gas needed for a real transaction
611+
// we add an amount to ensure sufficient gas is provided
612+
gas += 20000
613+
}
614+
615+
txf = txf.WithGas(gas)
594616
}
595-
txf = txf.WithGas(gas)
596-
txf = txf.WithFees(c.fees)
597617

598618
if c.gasPrices != "" {
599619
txf = txf.WithGasPrices(c.gasPrices)
@@ -604,16 +624,20 @@ func (c Client) CreateTx(goCtx context.Context, account cosmosaccount.Account, m
604624
return TxService{}, errors.WithStack(err)
605625
}
606626

607-
txUnsigned.SetFeeGranter(ctx.GetFeeGranterAddress())
627+
txUnsigned.SetFeeGranter(clientCtx.GetFeeGranterAddress())
608628

609629
return TxService{
610630
client: c,
611-
clientContext: ctx,
631+
clientContext: clientCtx,
612632
txBuilder: txUnsigned,
613633
txFactory: txf,
614634
}, nil
615635
}
616636

637+
func (c Client) CreateTx(ctx context.Context, account cosmosaccount.Account, msgs ...sdktypes.Msg) (TxService, error) {
638+
return c.CreateTxWithOptions(ctx, account, TxOptions{}, msgs...)
639+
}
640+
617641
// GetBlockTXs returns the transactions in a block.
618642
// The list of transactions can be empty if there are no transactions in the block
619643
// at the moment this method is called.

Diff for: ignite/pkg/cosmosclient/rpc.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
// Useful because the original implementation may return JSON errors when the
1616
// requested node is busy, which is confusing for the user. With rpcWrapper,
1717
// the error is prefixed with 'error while requesting node xxx: JSON error'.
18-
// TODO(tb): we may remove this wrapper once https://github.com/tendermint/tendermint/issues/9312 is fixed.
1918
type rpcWrapper struct {
2019
rpcclient.Client
2120
nodeAddress string

Diff for: ignite/pkg/cosmosclient/signer.go

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/cosmos/cosmos-sdk/client/tx"
88
)
99

10+
var _ Signer = signer{}
11+
1012
// signer implements the Signer interface.
1113
type signer struct{}
1214

Diff for: ignite/pkg/cosmosclient/tx_options.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cosmosclient
2+
3+
// TxOptions contains options for creating a transaction.
4+
// It is used by the CreateTxWithOptions method.
5+
type TxOptions struct {
6+
// Memo is the memo to be used for the transaction.
7+
Memo string
8+
9+
// GasLimit is the gas limit to be used for the transaction.
10+
// If GasLimit is set to 0, the gas limit will be automatically calculated.
11+
GasLimit uint64
12+
13+
// Fees is the fees to be used for the transaction.
14+
Fees string
15+
}

0 commit comments

Comments
 (0)