Skip to content

Commit

Permalink
refactor: decouple base fee fetching as part of quote from simulation (
Browse files Browse the repository at this point in the history
…#550)

* refactor: return base fee in /quote regardless of simulation success.

* separate base fee from simulation. Pre-compute during ingest

* updates

* updates

* Update router/repository/memory_router_repository.go

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* fix

* separate interface

* rename

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
p0mvn and coderabbitai[bot] committed Nov 12, 2024
1 parent 1313ed5 commit f4188d5
Show file tree
Hide file tree
Showing 33 changed files with 404 additions and 168 deletions.
8 changes: 6 additions & 2 deletions app/sidecar_query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/osmosis-labs/sqs/domain/cosmos/auth/types"
ingestrpcdelivry "github.com/osmosis-labs/sqs/ingest/delivery/grpc"
ingestusecase "github.com/osmosis-labs/sqs/ingest/usecase"
"github.com/osmosis-labs/sqs/ingest/usecase/plugins/basefee"
orderbookclaimbot "github.com/osmosis-labs/sqs/ingest/usecase/plugins/orderbook/claimbot"
orderbookfillbot "github.com/osmosis-labs/sqs/ingest/usecase/plugins/orderbook/fillbot"
orderbookrepository "github.com/osmosis-labs/sqs/orderbook/repository"
Expand Down Expand Up @@ -217,11 +218,10 @@ func NewSideCarQueryServer(appCodec codec.Codec, config domain.Config, logger lo
}

grpcClient := passthroughGRPCClient.GetChainGRPCClient()
gasCalculator := tx.NewGasCalculator(grpcClient, tx.CalculateGas)
gasCalculator := tx.NewMsgSimulator(grpcClient, tx.CalculateGas, routerRepository)
quoteSimulator := quotesimulator.NewQuoteSimulator(
gasCalculator,
app.GetEncodingConfig(),
txfeestypes.NewQueryClient(grpcClient),
types.NewQueryClient(grpcClient),
config.ChainID,
)
Expand Down Expand Up @@ -324,6 +324,10 @@ func NewSideCarQueryServer(appCodec codec.Codec, config domain.Config, logger lo
}
}

// Unconditionally register the base fee fetcher.
baseFeeFetcherPlugin := basefee.NewEndBlockUpdatePlugin(routerRepository, txfeestypes.NewQueryClient(grpcClient), logger)
ingestUseCase.RegisterEndBlockProcessPlugin(baseFeeFetcherPlugin)

// Register chain info use case as a listener to the pool liquidity compute worker (healthcheck).
poolLiquidityComputeWorker.RegisterListener(chainInfoUseCase)

Expand Down
6 changes: 6 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ const docTemplate = `{
"description": "Slippage tolerance multiplier for the simulation. If simulatorAddress is provided, this must be provided.",
"name": "simulationSlippageTolerance",
"in": "query"
},
{
"type": "boolean",
"description": "Boolean flag indicating whether to append the base fee to the quote. False by default.",
"name": "appendBaseFee",
"in": "query"
}
],
"responses": {
Expand Down
6 changes: 6 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@
"description": "Slippage tolerance multiplier for the simulation. If simulatorAddress is provided, this must be provided.",
"name": "simulationSlippageTolerance",
"in": "query"
},
{
"type": "boolean",
"description": "Boolean flag indicating whether to append the base fee to the quote. False by default.",
"name": "appendBaseFee",
"in": "query"
}
],
"responses": {
Expand Down
5 changes: 5 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ paths:
in: query
name: simulationSlippageTolerance
type: string
- description: Boolean flag indicating whether to append the base fee to the
quote. False by default.
in: query
name: appendBaseFee
type: boolean
produces:
- application/json
responses:
Expand Down
9 changes: 9 additions & 0 deletions domain/base_fee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package domain

import "github.com/osmosis-labs/osmosis/osmomath"

// BaseFee holds the denom and current base fee
type BaseFee struct {
Denom string
CurrentFee osmomath.Dec
}
58 changes: 35 additions & 23 deletions domain/cosmos/tx/msg_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tx

import (
"context"
"errors"

cosmosclient "github.com/cosmos/cosmos-sdk/client"
txclient "github.com/cosmos/cosmos-sdk/client/tx"
Expand All @@ -12,8 +13,10 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"github.com/osmosis-labs/osmosis/v27/app/params"
txfeestypes "github.com/osmosis-labs/osmosis/v27/x/txfees/types"
"github.com/osmosis-labs/sqs/domain"
"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 @@ -23,7 +26,6 @@ type MsgSimulator interface {
BuildTx(
ctx context.Context,
keyring keyring.Keyring,
txfeesClient txfeestypes.QueryClient,
encodingConfig params.EncodingConfig,
account *authtypes.BaseAccount,
chainID string,
Expand All @@ -44,19 +46,19 @@ type MsgSimulator interface {
// which is the fee amount in the base denomination.
PriceMsgs(
ctx context.Context,
txfeesClient txfeestypes.QueryClient,
encodingConfig cosmosclient.TxConfig,
account *authtypes.BaseAccount,
chainID string,
msg ...sdk.Msg,
) (uint64, sdk.Coin, error)
) domain.TxFeeInfo
}

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

Expand All @@ -65,16 +67,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 gogogrpc.ClientConn
calculateGas CalculateGasFn
clientCtx grpc.ClientConnInterface
calculateGas CalculateGasFn
memoryRouterRepository routerrepo.BaseFeeRepository
}

// 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 @@ -91,13 +93,13 @@ func (c *txGasCalulator) BuildTx(
return nil, err
}

gasAdjusted, feecoin, err := c.PriceMsgs(ctx, txfeesClient, encodingConfig.TxConfig, account, chainID, msg...)
if err != nil {
return nil, err
priceInfo := c.PriceMsgs(ctx, encodingConfig.TxConfig, account, chainID, msg...)
if priceInfo.Err != "" {
return nil, errors.New(priceInfo.Err)
}

txBuilder.SetGasLimit(gasAdjusted)
txBuilder.SetFeeAmount(sdk.Coins{feecoin})
txBuilder.SetGasLimit(priceInfo.AdjustedGasUsed)
txBuilder.SetFeeAmount(sdk.Coins{priceInfo.FeeCoin})

sigV2 := BuildSignatures(privKey.PubKey(), nil, account.Sequence)
err = txBuilder.SetSignatures(sigV2)
Expand Down Expand Up @@ -146,23 +148,33 @@ func (c *txGasCalulator) SimulateMsgs(encodingConfig cosmosclient.TxConfig, acco
}

// PriceMsgs implements MsgSimulator.
func (c *txGasCalulator) PriceMsgs(ctx context.Context, txfeesClient txfeestypes.QueryClient, encodingConfig cosmosclient.TxConfig, account *authtypes.BaseAccount, chainID string, msg ...sdk.Msg) (uint64, sdk.Coin, error) {
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 0, sdk.Coin{}, err
return domain.TxFeeInfo{Err: err.Error(), BaseFee: baseFee.CurrentFee}
}

feeCoin, err := CalculateFeeCoin(ctx, txfeesClient, gasAdjusted)
if err != nil {
return 0, sdk.Coin{}, err
}
feeAmount := CalculateFeeAmount(baseFee.CurrentFee, gasAdjusted)

return gasAdjusted, feeCoin, nil
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.
Expand Down
Loading

0 comments on commit f4188d5

Please sign in to comment.