Skip to content

Commit

Permalink
chore: refactor repetitive code by extracting common functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
kakysha committed Mar 19, 2024
1 parent 2a7da1e commit b4bd99b
Showing 1 changed file with 9 additions and 46 deletions.
55 changes: 9 additions & 46 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func (c *chainClient) syncTimeoutHeight() {
// 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 (c *chainClient) prepareFactory(clientCtx client.Context, txf tx.Factory) (tx.Factory, error) {
func prepareFactory(clientCtx client.Context, txf tx.Factory) (tx.Factory, error) {

Check warning on line 426 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L426

Added line #L426 was not covered by tests
from := clientCtx.GetFromAddress()

if err := txf.AccountRetriever().EnsureExists(clientCtx, from); err != nil {
Expand Down Expand Up @@ -633,7 +633,7 @@ func (c *chainClient) GetFeeDiscountInfo(ctx context.Context, account string) (*
func (c *chainClient) SimulateMsg(clientCtx client.Context, msgs ...sdk.Msg) (*txtypes.SimulateResponse, error) {
c.txFactory = c.txFactory.WithSequence(c.accSeq)
c.txFactory = c.txFactory.WithAccountNumber(c.accNum)
txf, err := c.prepareFactory(clientCtx, c.txFactory)
txf, err := prepareFactory(clientCtx, c.txFactory)

Check warning on line 636 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L636

Added line #L636 was not covered by tests
if err != nil {
err = errors.Wrap(err, "failed to prepareFactory")
return nil, err
Expand Down Expand Up @@ -684,10 +684,12 @@ func (c *chainClient) AsyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxRe
}
return res, nil
}

func (c *chainClient) BuildSignedTx(clientCtx client.Context, accNum, accSeq, initialGas uint64, msgs ...sdk.Msg) ([]byte, error) {
txf := NewTxFactory(clientCtx).WithSequence(accSeq).WithAccountNumber(accNum).WithGas(initialGas)
return c.buildSignedTx(clientCtx, txf, msgs...)

Check warning on line 689 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L689

Added line #L689 was not covered by tests
}

func (c *chainClient) buildSignedTx(clientCtx client.Context, txf tx.Factory, msgs ...sdk.Msg) ([]byte, error) {

Check warning on line 692 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L692

Added line #L692 was not covered by tests
if clientCtx.Simulate {
simTxBytes, err := txf.BuildSimTx(msgs...)
if err != nil {
Expand All @@ -708,7 +710,7 @@ func (c *chainClient) BuildSignedTx(clientCtx client.Context, accNum, accSeq, in
c.gasWanted = adjustedGas
}

txf, err := c.prepareFactory(clientCtx, txf)
txf, err := prepareFactory(clientCtx, txf)

Check warning on line 713 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L713

Added line #L713 was not covered by tests
if err != nil {
return nil, errors.Wrap(err, "failed to prepareFactory")
}
Expand Down Expand Up @@ -799,48 +801,9 @@ func (c *chainClient) broadcastTx(
await bool,
msgs ...sdk.Msg,
) (*txtypes.BroadcastTxResponse, error) {
txf, err := c.prepareFactory(clientCtx, txf)
txBytes, err := c.buildSignedTx(clientCtx, txf, msgs...)

Check warning on line 804 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L804

Added line #L804 was not covered by tests
if err != nil {
err = errors.Wrap(err, "failed to prepareFactory")
return nil, err
}
ctx := context.Background()
if clientCtx.Simulate {
simTxBytes, err := txf.BuildSimTx(msgs...)
if err != nil {
err = errors.Wrap(err, "failed to build sim tx bytes")
return nil, err
}
ctx := c.getCookie(ctx)
simRes, err := c.txClient.Simulate(ctx, &txtypes.SimulateRequest{TxBytes: simTxBytes})
if err != nil {
err = errors.Wrap(err, "failed to CalculateGas")
return nil, err
}

adjustedGas := uint64(txf.GasAdjustment() * float64(simRes.GasInfo.GasUsed))
txf = txf.WithGas(adjustedGas)

c.gasWanted = adjustedGas
}

txn, err := txf.BuildUnsignedTx(msgs...)

if err != nil {
err = errors.Wrap(err, "failed to BuildUnsignedTx")
return nil, err
}

txn.SetFeeGranter(clientCtx.GetFeeGranterAddress())
err = tx.Sign(txf, clientCtx.GetFromName(), txn, true)
if err != nil {
err = errors.Wrap(err, "failed to Sign Tx")
return nil, err
}

txBytes, err := clientCtx.TxConfig.TxEncoder()(txn.GetTx())
if err != nil {
err = errors.Wrap(err, "failed TxEncoder to encode Tx")
err = errors.Wrap(err, "failed to build signed Tx")

Check warning on line 806 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L806

Added line #L806 was not covered by tests
return nil, err
}

Expand All @@ -849,7 +812,7 @@ func (c *chainClient) broadcastTx(
Mode: txtypes.BroadcastMode_BROADCAST_MODE_SYNC,
}
// use our own client to broadcast tx
ctx = c.getCookie(ctx)
ctx := c.getCookie(context.Background())

Check warning on line 815 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L815

Added line #L815 was not covered by tests
res, err := c.txClient.BroadcastTx(ctx, &req)
if !await || err != nil {
return res, err
Expand Down

0 comments on commit b4bd99b

Please sign in to comment.