@@ -136,6 +136,8 @@ type Client struct {
136
136
}
137
137
138
138
// 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.
139
141
type Option func (* Client )
140
142
141
143
// 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 {
177
179
}
178
180
}
179
181
182
+ // WithAddressPrefix sets the address prefix on the client.
180
183
func WithAddressPrefix (prefix string ) Option {
181
184
return func (c * Client ) {
182
185
c .addressPrefix = prefix
183
186
}
184
187
}
185
188
189
+ // WithUseFaucet sets the faucet address on the client.
186
190
func WithUseFaucet (faucetAddress , denom string , minAmount uint64 ) Option {
187
191
return func (c * Client ) {
188
192
c .useFaucet = true
@@ -218,7 +222,8 @@ func WithGasAdjustment(gasAdjustment float64) Option {
218
222
}
219
223
}
220
224
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.
222
227
func WithFees (fees string ) Option {
223
228
return func (c * Client ) {
224
229
c .fees = fees
@@ -546,15 +551,17 @@ func (c Client) BroadcastTx(ctx context.Context, account cosmosaccount.Account,
546
551
return txService .Broadcast (ctx )
547
552
}
548
553
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 ) {
550
557
defer c .lockBech32Prefix ()()
551
558
552
559
if c .useFaucet && ! c .generateOnly {
553
560
addr , err := account .Address (c .addressPrefix )
554
561
if err != nil {
555
562
return TxService {}, errors .WithStack (err )
556
563
}
557
- if err := c .makeSureAccountHasTokens (goCtx , addr ); err != nil {
564
+ if err := c .makeSureAccountHasTokens (ctx , addr ); err != nil {
558
565
return TxService {}, err
559
566
}
560
567
}
@@ -564,36 +571,49 @@ func (c Client) CreateTx(goCtx context.Context, account cosmosaccount.Account, m
564
571
return TxService {}, errors .WithStack (err )
565
572
}
566
573
567
- ctx := c .context .
574
+ clientCtx := c .context .
568
575
WithFromName (account .Name ).
569
576
WithFromAddress (sdkaddr )
570
577
571
- txf , err := c .prepareFactory (ctx )
578
+ txf , err := c .prepareFactory (clientCtx )
572
579
if err != nil {
573
580
return TxService {}, err
574
581
}
575
582
576
- if c . gasAdjustment != 0 && c . gasAdjustment != defaultGasAdjustment {
577
- txf = txf .WithGasAdjustment ( c . gasAdjustment )
583
+ if options . Memo != "" {
584
+ txf = txf .WithMemo ( options . Memo )
578
585
}
579
586
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 )
586
594
} 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 )
590
597
}
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 )
594
616
}
595
- txf = txf .WithGas (gas )
596
- txf = txf .WithFees (c .fees )
597
617
598
618
if c .gasPrices != "" {
599
619
txf = txf .WithGasPrices (c .gasPrices )
@@ -604,16 +624,20 @@ func (c Client) CreateTx(goCtx context.Context, account cosmosaccount.Account, m
604
624
return TxService {}, errors .WithStack (err )
605
625
}
606
626
607
- txUnsigned .SetFeeGranter (ctx .GetFeeGranterAddress ())
627
+ txUnsigned .SetFeeGranter (clientCtx .GetFeeGranterAddress ())
608
628
609
629
return TxService {
610
630
client : c ,
611
- clientContext : ctx ,
631
+ clientContext : clientCtx ,
612
632
txBuilder : txUnsigned ,
613
633
txFactory : txf ,
614
634
}, nil
615
635
}
616
636
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
+
617
641
// GetBlockTXs returns the transactions in a block.
618
642
// The list of transactions can be empty if there are no transactions in the block
619
643
// at the moment this method is called.
0 commit comments