Skip to content

Commit

Permalink
Fix issue celestiaorg#4282
Browse files Browse the repository at this point in the history
  • Loading branch information
VolodymyrBg authored Feb 6, 2025
1 parent b94f036 commit a5a1f09
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions pkg/user/tx_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,66 +310,70 @@ func (client *TxClient) SubmitTx(ctx context.Context, msgs []sdktypes.Msg, opts
return client.ConfirmTx(ctx, resp.TxHash)
}

func (client *TxClient) BroadcastTx(ctx context.Context, msgs []sdktypes.Msg, opts ...TxOption) (*sdktypes.TxResponse, error) {
client.mtx.Lock()
defer client.mtx.Unlock()

// prune transactions that are older than 10 minutes
// pruning has to be done in broadcast, since users
// might not always call ConfirmTx().
client.pruneTxTracker()
// estimateAndSetGas estimates the gas for a transaction and sets it in the builder
func (client *TxClient) estimateAndSetGas(ctx context.Context, txBuilder client.TxBuilder, opts *TxOptions) error {
// If gas is already set in options, skip estimation
if opts != nil && opts.Gas != 0 {
txBuilder.SetGasLimit(opts.Gas)
return nil
}

account, err := client.getAccountNameFromMsgs(msgs)
// Estimate gas
gasLimit, err := client.estimateGas(ctx, txBuilder)
if err != nil {
return nil, err
return fmt.Errorf("estimating gas: %w", err)
}

if err := client.checkAccountLoaded(ctx, account); err != nil {
return nil, err
}
// Set the estimated gas limit
txBuilder.SetGasLimit(gasLimit)
return nil
}

func (client *TxClient) BroadcastTx(ctx context.Context, msgs []sdktypes.Msg, opts ...TxOption) (*sdktypes.TxResponse, error) {
client.mtx.Lock()
defer client.mtx.Unlock()

txBuilder, err := client.signer.txBuilder(msgs, opts...)
if err != nil {
return nil, err
}

hasUserSetFee := false
for _, coin := range txBuilder.GetTx().GetFee() {
if coin.Denom == appconsts.BondDenom {
hasUserSetFee = true
break
}
// Get options for gas estimation
txOpts := DefaultTxOptions()
for _, opt := range opts {
opt(txOpts)
}

gasLimit := txBuilder.GetTx().GetGas()
if gasLimit == 0 {
if !hasUserSetFee {
// add at least 1utia as fee to builder as it affects gas calculation.
txBuilder.SetFeeAmount(sdktypes.NewCoins(sdktypes.NewCoin(appconsts.BondDenom, sdktypes.NewInt(1))))
}
gasLimit, err = client.estimateGas(ctx, txBuilder)
if err != nil {
return nil, err
}
txBuilder.SetGasLimit(gasLimit)
// Estimate and set gas if not explicitly provided
if err := client.estimateAndSetGas(ctx, txBuilder, txOpts); err != nil {
return nil, err
}

if !hasUserSetFee {
fee := int64(math.Ceil(appconsts.DefaultMinGasPrice * float64(gasLimit)))
txBuilder.SetFeeAmount(sdktypes.NewCoins(sdktypes.NewCoin(appconsts.BondDenom, sdktypes.NewInt(fee))))
// Sign the transaction
signer, sequence, err := client.signer.signTransaction(txBuilder)
if err != nil {
return nil, err
}

account, _, err = client.signer.signTransaction(txBuilder)
txBytes, err := client.signer.EncodeTx(txBuilder.GetTx())
if err != nil {
return nil, err
}

txBytes, err := client.signer.EncodeTx(txBuilder.GetTx())
resp, err := client.broadcastTx(ctx, txBytes, signer)
if err != nil {
return nil, err
}

return client.broadcastTx(ctx, txBytes, account)
if resp.TxHash != "" {
client.txTracker[resp.TxHash] = txInfo{
sequence: sequence,
signer: signer,
timestamp: time.Now(),
}
}

return resp, nil
}

func (client *TxClient) broadcastTx(ctx context.Context, txBytes []byte, signer string) (*sdktypes.TxResponse, error) {
Expand Down

0 comments on commit a5a1f09

Please sign in to comment.