Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move some methods inside TX Factory #9421

Merged
merged 10 commits into from
Jun 28, 2021
Prev Previous commit
Next Next commit
add prepare
  • Loading branch information
jgimeno committed May 28, 2021
commit bf1b1f1158fb3131ff69f6e261c737c703f9706d
32 changes: 32 additions & 0 deletions client/tx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,35 @@ func (f Factory) BuildSimTx(msgs ...sdk.Msg) ([]byte, error) {

return f.txConfig.TxEncoder()(txb.GetTx())
}

// Prepare ensures the account defined by ctx.GetFromAddress() exists and
// if the account number and/or the account sequence number are zero (not set),
// they will be queried for and set on the provided Factory. A new Factory with
// the updated fields will be returned.
func (f Factory) Prepare(clientCtx client.Context) (Factory, error) {
fc := f

from := clientCtx.GetFromAddress()

if err := fc.accountRetriever.EnsureExists(clientCtx, from); err != nil {
return fc, err
}

initNum, initSeq := fc.accountNumber, fc.sequence
if initNum == 0 || initSeq == 0 {
num, seq, err := fc.accountRetriever.GetAccountNumberSequence(clientCtx, from)
if err != nil {
return fc, err
}

if initNum == 0 {
fc = fc.WithAccountNumber(num)
}

if initSeq == 0 {
fc = fc.WithSequence(seq)
}
}

return fc, nil
}
32 changes: 1 addition & 31 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func GenerateOrBroadcastTxWithFactory(clientCtx client.Context, txf Factory, msg
// given set of messages. It will also simulate gas requirements if necessary.
// It will return an error upon failure.
func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
txf, err := prepareFactory(clientCtx, txf)
txf, err := txf.Prepare(clientCtx)
if err != nil {
return err
}
Expand Down Expand Up @@ -205,36 +205,6 @@ func CalculateGas(
return simRes, uint64(txf.GasAdjustment() * float64(simRes.GasInfo.GasUsed)), nil
}

// prepareFactory ensures the account defined by ctx.GetFromAddress() exists and
// if the account number and/or the account sequence number are zero (not set),
// they will be queried for and set on the provided Factory. A new Factory with
// the updated fields will be returned.
func prepareFactory(clientCtx client.Context, txf Factory) (Factory, error) {
from := clientCtx.GetFromAddress()

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

initNum, initSeq := txf.accountNumber, txf.sequence
if initNum == 0 || initSeq == 0 {
num, seq, err := txf.accountRetriever.GetAccountNumberSequence(clientCtx, from)
if err != nil {
return txf, err
}

if initNum == 0 {
txf = txf.WithAccountNumber(num)
}

if initSeq == 0 {
txf = txf.WithSequence(seq)
}
}

return txf, nil
}

// SignWithPrivKey signs a given tx with the given private key, and returns the
// corresponding SignatureV2 if the signing is successful.
func SignWithPrivKey(
Expand Down