Skip to content

Commit

Permalink
refactor: gas estimation APIs (#546)
Browse files Browse the repository at this point in the history
* refactor: gas estimation APIs

* changelog

* lint
  • Loading branch information
p0mvn authored and deividaspetraitis committed Nov 25, 2024
1 parent 8f2df57 commit 863d154
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 307 deletions.
5 changes: 1 addition & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

# Changelog

## v26.2.0
## Unreleased

- #554 - [FIX] Add missing checks for transmuter's limiter
- #548 - Return base fee in /quote regardless of simulation success.
- #547 - Add /quote simulation for "out given in" single routes.
- #526 - Refactor gas estimation APIs
- #524 - Claimbot

Expand Down
84 changes: 25 additions & 59 deletions domain/cosmos/tx/msg_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tx

import (
"context"
"errors"

cosmosclient "github.com/cosmos/cosmos-sdk/client"
txclient "github.com/cosmos/cosmos-sdk/client/tx"
Expand All @@ -11,12 +10,9 @@ import (
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/osmosis-labs/osmosis/v27/app/params"
"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/osmosis/v26/app/params"
txfeestypes "github.com/osmosis-labs/osmosis/v26/x/txfees/types"
"github.com/osmosis-labs/sqs/domain/keyring"
routerrepo "github.com/osmosis-labs/sqs/router/repository"
"google.golang.org/grpc"

gogogrpc "github.com/cosmos/gogoproto/grpc"
)
Expand All @@ -26,6 +22,7 @@ type MsgSimulator interface {
BuildTx(
ctx context.Context,
keyring keyring.Keyring,
txfeesClient txfeestypes.QueryClient,
encodingConfig params.EncodingConfig,
account *authtypes.BaseAccount,
chainID string,
Expand All @@ -41,24 +38,13 @@ type MsgSimulator interface {
chainID string,
msgs []sdk.Msg,
) (*txtypes.SimulateResponse, uint64, error)

// PriceMsgs simulates the execution of the given messages and returns the gas used and the fee coin,
// which is the fee amount in the base denomination.
PriceMsgs(
ctx context.Context,
encodingConfig cosmosclient.TxConfig,
account *authtypes.BaseAccount,
chainID string,
msg ...sdk.Msg,
) domain.TxFeeInfo
}

// NewMsgSimulator creates a new GasCalculator instance.
func NewMsgSimulator(clientCtx gogogrpc.ClientConn, calculateGas CalculateGasFn, memoryRouterRepository routerrepo.RouterRepository) MsgSimulator {
// NewGasCalculator creates a new GasCalculator instance.
func NewGasCalculator(clientCtx gogogrpc.ClientConn, calculateGas CalculateGasFn) MsgSimulator {
return &txGasCalulator{
clientCtx: clientCtx,
calculateGas: calculateGas,
memoryRouterRepository: memoryRouterRepository,
clientCtx: clientCtx,
calculateGas: calculateGas,
}
}

Expand All @@ -67,16 +53,16 @@ type CalculateGasFn func(clientCtx gogogrpc.ClientConn, txf txclient.Factory, ms

// txGasCalulator is a GasCalculator implementation that uses simulated transactions to calculate gas.
type txGasCalulator struct {
clientCtx grpc.ClientConnInterface
calculateGas CalculateGasFn
memoryRouterRepository routerrepo.BaseFeeRepository
clientCtx gogogrpc.ClientConn
calculateGas CalculateGasFn
}

// BuildTx constructs a transaction using the provided parameters and messages.
// Returns a TxBuilder and any error encountered.
func (c *txGasCalulator) BuildTx(
ctx context.Context,
keyring keyring.Keyring,
txfeesClient txfeestypes.QueryClient,
encodingConfig params.EncodingConfig,
account *authtypes.BaseAccount,
chainID string,
Expand All @@ -93,13 +79,23 @@ func (c *txGasCalulator) BuildTx(
return nil, err
}

priceInfo := c.PriceMsgs(ctx, encodingConfig.TxConfig, account, chainID, msg...)
if priceInfo.Err != "" {
return nil, errors.New(priceInfo.Err)
_, gas, err := c.SimulateMsgs(
encodingConfig.TxConfig,
account,
chainID,
msg,
)
if err != nil {
return nil, err
}
txBuilder.SetGasLimit(gas)

txBuilder.SetGasLimit(priceInfo.AdjustedGasUsed)
txBuilder.SetFeeAmount(sdk.Coins{priceInfo.FeeCoin})
feecoin, err := CalculateFeeCoin(ctx, txfeesClient, gas)
if err != nil {
return nil, err
}

txBuilder.SetFeeAmount(sdk.NewCoins(feecoin))

sigV2 := BuildSignatures(privKey.PubKey(), nil, account.Sequence)
err = txBuilder.SetSignatures(sigV2)
Expand Down Expand Up @@ -147,36 +143,6 @@ func (c *txGasCalulator) SimulateMsgs(encodingConfig cosmosclient.TxConfig, acco
return gasResult, adjustedGasUsed, nil
}

// PriceMsgs implements MsgSimulator.
func (c *txGasCalulator) PriceMsgs(ctx context.Context, encodingConfig cosmosclient.TxConfig, account *authtypes.BaseAccount, chainID string, msg ...sdk.Msg) domain.TxFeeInfo {
baseFee := c.memoryRouterRepository.GetBaseFee()
if baseFee.CurrentFee.IsNil() || baseFee.CurrentFee.IsZero() {
return domain.TxFeeInfo{Err: "base fee is zero or nil"}
}
if baseFee.Denom == "" {
return domain.TxFeeInfo{Err: "base fee denom is empty"}
}

_, gasAdjusted, err := c.SimulateMsgs(
encodingConfig,
account,
chainID,
msg,
)
if err != nil {
return domain.TxFeeInfo{Err: err.Error(), BaseFee: baseFee.CurrentFee}
}

feeAmount := CalculateFeeAmount(baseFee.CurrentFee, gasAdjusted)

return domain.TxFeeInfo{
AdjustedGasUsed: gasAdjusted,
FeeCoin: sdk.Coin{Denom: baseFee.Denom, Amount: feeAmount},
BaseFee: baseFee.CurrentFee,
Err: "",
}
}

// CalculateGas calculates the gas required for a transaction using the provided transaction factory and messages.
func CalculateGas(
clientCtx gogogrpc.ClientConn,
Expand Down
Loading

0 comments on commit 863d154

Please sign in to comment.