diff --git a/reporter/client/broadcast_message.go b/reporter/client/broadcast_message.go index c54db6c..5aa9824 100644 --- a/reporter/client/broadcast_message.go +++ b/reporter/client/broadcast_message.go @@ -143,7 +143,7 @@ func (c *Client) GenerateAndBroadcastSpotPriceReport(ctx context.Context, qd []b } func (c *Client) HandleBridgeDepositTxInChannel(ctx context.Context, data TxChannelInfo) { - resp, err := c.sendTx(ctx, 0, data.Msg) // 0 = no queryMeta tracking for bridge transactions + resp, err := c.sendTx(ctx, 0, true, data.Msg) // 0 = no queryMeta tracking for bridge transactions if err != nil { c.logger.Error("submitting deposit report transaction", "error", err, @@ -200,7 +200,7 @@ func (c *Client) BroadcastTxMsgToChain() { defer cancel() if !txInfo.isBridge { - _, err := c.sendTx(ctx, txInfo.QueryMetaId, txInfo.Msg) + _, err := c.sendTx(ctx, txInfo.QueryMetaId, false, txInfo.Msg) if err != nil { c.logger.Error(fmt.Sprintf("Error sending tx: %v", err)) } diff --git a/reporter/client/client.go b/reporter/client/client.go index 8edc775..b537ffd 100644 --- a/reporter/client/client.go +++ b/reporter/client/client.go @@ -31,7 +31,7 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) -const defaultGas = uint64(300000) +const defaultGas = uint64(240000) var ( commitedIds = make(map[uint64]bool) @@ -175,9 +175,7 @@ func (c *Client) Start( if !viper.IsSet("price-guard-update-on-blocked") { return fmt.Errorf("price-guard-enabled is true but price-guard-update-on-blocked is not set") } - } else - // If price guard is disabled, error if any other price guard flags are set - if viper.IsSet("price-guard-threshold") || viper.IsSet("price-guard-max-age") || viper.IsSet("price-guard-update-on-blocked") { + } else if !priceGuardEnabled && (viper.IsSet("price-guard-threshold") || viper.IsSet("price-guard-max-age") || viper.IsSet("price-guard-update-on-blocked")) { return fmt.Errorf("price-guard flags are set but price-guard-enabled is false") } diff --git a/reporter/client/tx_handler.go b/reporter/client/tx_handler.go index 80d7c12..af223b3 100644 --- a/reporter/client/tx_handler.go +++ b/reporter/client/tx_handler.go @@ -4,6 +4,7 @@ import ( "context" "encoding/hex" "fmt" + "reflect" "strings" "time" @@ -21,11 +22,16 @@ import ( "github.com/cosmos/cosmos-sdk/types/tx/signing" ) +var ( + gasEstimateMap = make(map[reflect.Type]uint64) + BRIDGE_REPORT_TYPE = reflect.TypeOf("bridge_deposit_report") +) + func newFactory(clientCtx client.Context) tx.Factory { return tx.Factory{}. WithChainID(clientCtx.ChainID). WithKeybase(clientCtx.Keyring). - WithGasAdjustment(1.1). + WithGasAdjustment(1.25). WithGas(defaultGas). WithSignMode(signing.SignMode_SIGN_MODE_DIRECT). WithAccountRetriever(clientCtx.AccountRetriever). @@ -120,7 +126,34 @@ func (c *Client) WaitForBlockHeight(ctx context.Context, h int64) error { } } -func (c *Client) sendTx(ctx context.Context, queryMetaId uint64, msg ...sdk.Msg) (*cmttypes.ResultTx, error) { +func (c *Client) EstimateGas(ctx context.Context, isBridge bool, txf tx.Factory, msg ...sdk.Msg) (uint64, error) { + if isBridge { + if gasEstimate, ok := gasEstimateMap[BRIDGE_REPORT_TYPE]; ok { + return gasEstimate, nil + } + } else { + msgType := reflect.TypeOf(msg[0]) + if gasEstimate, ok := gasEstimateMap[msgType]; ok { + return gasEstimate, nil + } + } + if isBridge { + txf = txf.WithGasAdjustment(1.75) + } + _, gasEstimate, err := tx.CalculateGas(c.cosmosCtx, txf, msg...) + if err != nil { + return 0, fmt.Errorf("error calculating gas: %w", err) + } + if isBridge { + gasEstimateMap[BRIDGE_REPORT_TYPE] = gasEstimate + } else { + msgType := reflect.TypeOf(msg[0]) + gasEstimateMap[msgType] = gasEstimate + } + return gasEstimate, nil +} + +func (c *Client) sendTx(ctx context.Context, queryMetaId uint64, isBridge bool, msg ...sdk.Msg) (*cmttypes.ResultTx, error) { telemetry.IncrCounter(1, "daemon_sending_txs", "called") // Track success status for defer cleanup @@ -153,7 +186,10 @@ func (c *Client) sendTx(ctx context.Context, queryMetaId uint64, msg ...sdk.Msg) if err != nil { return nil, fmt.Errorf("error preparing transaction factory: %w", err) } - + gasEstimate, err := c.EstimateGas(ctx, isBridge, txf, msg...) + if err == nil { + txf = txf.WithGas(gasEstimate) + } txn, err := txf.BuildUnsignedTx(msg...) if err != nil { return nil, fmt.Errorf("error building unsigned transaction: %w", err)