diff --git a/client/context/context.go b/client/context/context.go index 311386ae966a..89a1b25f09ba 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -88,7 +88,7 @@ func (ctx CLIContext) WithAccountStore(accountStore string) CLIContext { return ctx } -// WithFrom returns a copy of the context with an updated from address orname. +// WithFrom returns a copy of the context with an updated from address or name. func (ctx CLIContext) WithFrom(from string) CLIContext { ctx.From = from return ctx diff --git a/client/utils/utils.go b/client/utils/utils.go index f61e9a0bd132..ed038c21b898 100644 --- a/client/utils/utils.go +++ b/client/utils/utils.go @@ -32,27 +32,11 @@ func SendTx(txCtx authctx.TxContext, cliCtx context.CLIContext, msgs []sdk.Msg) return err } - // TODO: (ref #1903) Allow for user supplied account number without - // automatically doing a manual lookup. - if txCtx.AccountNumber == 0 { - accNum, err := cliCtx.GetAccountNumber(from) - if err != nil { - return err - } - - txCtx = txCtx.WithAccountNumber(accNum) - } - - // TODO: (ref #1903) Allow for user supplied account sequence without - // automatically doing a manual lookup. - if txCtx.Sequence == 0 { - accSeq, err := cliCtx.GetAccountSequence(from) - if err != nil { - return err - } - - txCtx = txCtx.WithSequence(accSeq) + txCtxPtr, err := lookupTxCtx(txCtx, cliCtx, from) + if err != nil { + return err } + txCtx = *txCtxPtr name, err := cliCtx.GetFromName() if err != nil { @@ -79,6 +63,32 @@ func SendTx(txCtx authctx.TxContext, cliCtx context.CLIContext, msgs []sdk.Msg) return cliCtx.EnsureBroadcastTx(txBytes) } +func lookupTxCtx(txCtx authctx.TxContext, cliCtx context.CLIContext, from []byte) (*authctx.TxContext, error) { + // TODO: (ref #1903) Allow for user supplied account number without + // automatically doing a manual lookup. + if txCtx.AccountNumber == 0 { + accNum, err := cliCtx.GetAccountNumber(from) + if err != nil { + return nil, err + } + + txCtx = txCtx.WithAccountNumber(accNum) + } + + // TODO: (ref #1903) Allow for user supplied account sequence without + // automatically doing a manual lookup. + if txCtx.Sequence == 0 { + accSeq, err := cliCtx.GetAccountSequence(from) + if err != nil { + return nil, err + } + + txCtx = txCtx.WithSequence(accSeq) + } + + return &txCtx, nil +} + // EnrichCtxWithGas calculates the gas estimate that would be consumed by the // transaction and set the transaction's respective value accordingly. func EnrichCtxWithGas(txCtx authctx.TxContext, cliCtx context.CLIContext, name, passphrase string, msgs []sdk.Msg) (authctx.TxContext, error) {