Skip to content

Commit e7cf02e

Browse files
tbruyellelumtis
andauthored
fix(cosmosclient): call the faucet prior to create the tx (ignite#2781)
Fix ignite#2775 This change restores the behavior we used to have in 0.23, and which has changed with the refac of cosmosclient with the `node` command feat. Calling the faucet (when it is specifically enabled) will ensure the account that broadcasts the tx exists in the target chain. Because the `node` command has a `--generate-only` flag, and because we don't want to create a new account when it's set, this flag is now propagated to the cosmosclient struct. Therefore, the faucet isn't called when the flag is set. This doesn't affect the `network` command because it doesn't use that flag. * test link issue in CL * gh doesn't autolink issues in repo's file * Update changelog.md Co-authored-by: Lucas Btd <lucas.bertrand.22@gmail.com> * Solve TODO: validate tx msgs Co-authored-by: Lucas Btd <lucas.bertrand.22@gmail.com>
1 parent c17e044 commit e7cf02e

File tree

4 files changed

+29
-33
lines changed

4 files changed

+29
-33
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
- Add `--skip-proto` flag to `build`, `init` and `serve` commands to build the chain without building proto files
1717

18+
### Fixes
19+
20+
- Fix `pkg/cosmosclient` to call the faucet prior to creating the tx.
1821

1922
## [`v0.23.0`](https://github.com/ignite/cli/releases/tag/v0.23.0)
2023

ignite/cmd/node.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func newNodeCosmosClient(cmd *cobra.Command) (cosmosclient.Client, error) {
3838
gasPrices = getGasPrices(cmd)
3939
fees = getFees(cmd)
4040
broadcastMode = getBroadcastMode(cmd)
41+
generateOnly = getGenerateOnly(cmd)
4142
)
4243

4344
options := []cosmosclient.Option{
@@ -47,6 +48,7 @@ func newNodeCosmosClient(cmd *cobra.Command) (cosmosclient.Client, error) {
4748
cosmosclient.WithKeyringDir(keyringDir),
4849
cosmosclient.WithNodeAddress(xurl.HTTPEnsurePort(node)),
4950
cosmosclient.WithBroadcastMode(broadcastMode),
51+
cosmosclient.WithGenerateOnly(generateOnly),
5052
}
5153

5254
if gas != "" {

ignite/pkg/cosmosclient/cosmosclient.go

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type Client struct {
8888
gasPrices string
8989
fees string
9090
broadcastMode string
91+
generateOnly bool
9192
}
9293

9394
// Option configures your client.
@@ -180,6 +181,12 @@ func WithBroadcastMode(broadcastMode string) Option {
180181
}
181182
}
182183

184+
func WithGenerateOnly(generateOnly bool) Option {
185+
return func(c *Client) {
186+
c.generateOnly = generateOnly
187+
}
188+
}
189+
183190
// New creates a new client with given options.
184191
func New(ctx context.Context, options ...Option) (Client, error) {
185192
c := Client{
@@ -412,6 +419,16 @@ func (c Client) BroadcastTx(account cosmosaccount.Account, msgs ...sdktypes.Msg)
412419
func (c Client) CreateTx(account cosmosaccount.Account, msgs ...sdktypes.Msg) (TxService, error) {
413420
defer c.lockBech32Prefix()()
414421

422+
if c.useFaucet && !c.generateOnly {
423+
addr, err := account.Address(c.addressPrefix)
424+
if err != nil {
425+
return TxService{}, err
426+
}
427+
if err := c.makeSureAccountHasTokens(context.Background(), addr); err != nil {
428+
return TxService{}, err
429+
}
430+
}
431+
415432
sdkaddr, err := account.Record.GetAddress()
416433
if err != nil {
417434
return TxService{}, err
@@ -463,35 +480,6 @@ func (c Client) CreateTx(account cosmosaccount.Account, msgs ...sdktypes.Msg) (T
463480
}, nil
464481
}
465482

466-
// prepareBroadcast performs checks and operations before broadcasting messages
467-
func (c *Client) prepareBroadcast(ctx context.Context, accountName string, _ []sdktypes.Msg) error {
468-
// TODO uncomment after https://github.com/tendermint/spn/issues/363
469-
// validate msgs.
470-
// for _, msg := range msgs {
471-
// if err := msg.ValidateBasic(); err != nil {
472-
// return err
473-
// }
474-
// }
475-
476-
account, err := c.account(accountName)
477-
if err != nil {
478-
return err
479-
}
480-
481-
// make sure that account has enough balances before broadcasting.
482-
if c.useFaucet {
483-
addr, err := account.Address(c.addressPrefix)
484-
if err != nil {
485-
return err
486-
}
487-
if err := c.makeSureAccountHasTokens(ctx, addr); err != nil {
488-
return err
489-
}
490-
}
491-
492-
return nil
493-
}
494-
495483
// makeSureAccountHasTokens makes sure the address has a positive balance
496484
// it requests funds from the faucet if the address has an empty balance
497485
func (c *Client) makeSureAccountHasTokens(ctx context.Context, address string) error {
@@ -604,7 +592,8 @@ func (c Client) newContext() client.Context {
604592
WithHomeDir(c.homePath).
605593
WithClient(c.RPC).
606594
WithSkipConfirmation(true).
607-
WithKeyring(c.AccountRegistry.Keyring)
595+
WithKeyring(c.AccountRegistry.Keyring).
596+
WithGenerateOnly(c.generateOnly)
608597
}
609598

610599
func newFactory(clientCtx client.Context) tx.Factory {

ignite/pkg/cosmosclient/txservice.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/cosmos/cosmos-sdk/client"
77
"github.com/cosmos/cosmos-sdk/client/tx"
8-
sdktypes "github.com/cosmos/cosmos-sdk/types"
98
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
109
)
1110

@@ -29,8 +28,11 @@ func (s TxService) Broadcast() (Response, error) {
2928
accountName := s.clientContext.GetFromName()
3029
accountAddress := s.clientContext.GetFromAddress()
3130

32-
if err := s.client.prepareBroadcast(context.Background(), accountName, []sdktypes.Msg{}); err != nil {
33-
return Response{}, err
31+
// validate msgs.
32+
for _, msg := range s.txBuilder.GetTx().GetMsgs() {
33+
if err := msg.ValidateBasic(); err != nil {
34+
return Response{}, err
35+
}
3436
}
3537

3638
if err := tx.Sign(s.txFactory, accountName, s.txBuilder, true); err != nil {

0 commit comments

Comments
 (0)