Skip to content

Commit

Permalink
feat: simulate swap as part of quotes (#547) (#549)
Browse files Browse the repository at this point in the history
* feat: simulate swap as part of quotes

* try adding e2e test

* updates

* lint

* fix test

* swagger

* attempt fix grpc wiring

* fix e2e sim test

* updates

(cherry picked from commit f2931d8)

# Conflicts:
#	domain/cosmos/tx/msg_simulator.go
#	domain/cosmos/tx/msg_simulator_test.go
#	domain/mocks/msg_simulator_mock.go
#	domain/passthrough/passthrough_grpc_client.go

Co-authored-by: Roman <[email protected]>
  • Loading branch information
mergify[bot] and p0mvn authored Nov 12, 2024
1 parent e12f7d4 commit 829a871
Show file tree
Hide file tree
Showing 28 changed files with 927 additions and 94 deletions.
17 changes: 16 additions & 1 deletion app/sidecar_query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/osmosis-labs/osmosis/v27/app"
txfeestypes "github.com/osmosis-labs/osmosis/v27/x/txfees/types"
"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"
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"
orderbookusecase "github.com/osmosis-labs/sqs/orderbook/usecase"
"github.com/osmosis-labs/sqs/quotesimulator"
"github.com/osmosis-labs/sqs/sqsutil/datafetchers"

chaininforepo "github.com/osmosis-labs/sqs/chaininfo/repository"
Expand All @@ -43,6 +47,7 @@ import (

"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqs/domain/cache"
"github.com/osmosis-labs/sqs/domain/cosmos/tx"
"github.com/osmosis-labs/sqs/domain/keyring"
"github.com/osmosis-labs/sqs/domain/mvc"
orderbookgrpcclientdomain "github.com/osmosis-labs/sqs/domain/orderbook/grpcclient"
Expand Down Expand Up @@ -210,7 +215,17 @@ func NewSideCarQueryServer(appCodec codec.Codec, config domain.Config, logger lo
if err := tokenshttpdelivery.NewTokensHandler(e, *config.Pricing, tokensUseCase, pricingSimpleRouterUsecase, logger); err != nil {
return nil, err
}
routerHttpDelivery.NewRouterHandler(e, routerUsecase, tokensUseCase, logger)

grpcClient := passthroughGRPCClient.GetChainGRPCClient()
gasCalculator := tx.NewGasCalculator(grpcClient, tx.CalculateGas)
quoteSimulator := quotesimulator.NewQuoteSimulator(
gasCalculator,
app.GetEncodingConfig(),
txfeestypes.NewQueryClient(grpcClient),
types.NewQueryClient(grpcClient),
config.ChainID,
)
routerHttpDelivery.NewRouterHandler(e, routerUsecase, tokensUseCase, quoteSimulator, logger)

// Create a Numia HTTP client
passthroughConfig := config.Passthrough
Expand Down
20 changes: 16 additions & 4 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,18 @@ const docTemplate = `{
"description": "Boolean flag indicating whether to apply exponents to the spot price. False by default.",
"name": "applyExponents",
"in": "query"
},
{
"type": "string",
"description": "Address of the simulator to simulate the quote. If provided, the quote will be simulated.",
"name": "simulatorAddress",
"in": "query"
},
{
"type": "string",
"description": "Slippage tolerance multiplier for the simulation. If simulatorAddress is provided, this must be provided.",
"name": "simulationSlippageTolerance",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -514,7 +526,7 @@ const docTemplate = `{
"type": "object",
"properties": {
"amount": {
"$ref": "#/definitions/types.Int"
"$ref": "#/definitions/math.Int"
},
"denom": {
"type": "string"
Expand Down Expand Up @@ -663,6 +675,9 @@ const docTemplate = `{
}
}
},
"math.Int": {
"type": "object"
},
"sqsdomain.CandidatePool": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -707,9 +722,6 @@ const docTemplate = `{
}
}
}
},
"types.Int": {
"type": "object"
}
}
}`
Expand Down
20 changes: 16 additions & 4 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@
"description": "Boolean flag indicating whether to apply exponents to the spot price. False by default.",
"name": "applyExponents",
"in": "query"
},
{
"type": "string",
"description": "Address of the simulator to simulate the quote. If provided, the quote will be simulated.",
"name": "simulatorAddress",
"in": "query"
},
{
"type": "string",
"description": "Slippage tolerance multiplier for the simulation. If simulatorAddress is provided, this must be provided.",
"name": "simulationSlippageTolerance",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -505,7 +517,7 @@
"type": "object",
"properties": {
"amount": {
"$ref": "#/definitions/types.Int"
"$ref": "#/definitions/math.Int"
},
"denom": {
"type": "string"
Expand Down Expand Up @@ -654,6 +666,9 @@
}
}
},
"math.Int": {
"type": "object"
},
"sqsdomain.CandidatePool": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -698,9 +713,6 @@
}
}
}
},
"types.Int": {
"type": "object"
}
}
}
16 changes: 13 additions & 3 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ definitions:
github_com_cosmos_cosmos-sdk_types.Coin:
properties:
amount:
$ref: '#/definitions/types.Int'
$ref: '#/definitions/math.Int'
denom:
type: string
type: object
Expand Down Expand Up @@ -142,6 +142,8 @@ definitions:
$ref: '#/definitions/github_com_osmosis-labs_sqs_domain_orderbook.LimitOrder'
type: array
type: object
math.Int:
type: object
sqsdomain.CandidatePool:
properties:
id:
Expand Down Expand Up @@ -171,8 +173,6 @@ definitions:
type: object
type: object
type: object
types.Int:
type: object
info:
contact: {}
title: Osmosis Sidecar Query Server Example API
Expand Down Expand Up @@ -407,6 +407,16 @@ paths:
in: query
name: applyExponents
type: boolean
- description: Address of the simulator to simulate the quote. If provided,
the quote will be simulated.
in: query
name: simulatorAddress
type: string
- description: Slippage tolerance multiplier for the simulation. If simulatorAddress
is provided, this must be provided.
in: query
name: simulationSlippageTolerance
type: string
produces:
- application/json
responses:
Expand Down
48 changes: 35 additions & 13 deletions domain/cosmos/tx/msg_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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"
txfeestypes "github.com/osmosis-labs/osmosis/v27/x/txfees/types"
"github.com/osmosis-labs/sqs/domain/keyring"
Expand Down Expand Up @@ -38,6 +39,17 @@ 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,
txfeesClient txfeestypes.QueryClient,
encodingConfig cosmosclient.TxConfig,
account *authtypes.BaseAccount,
chainID string,
msg ...sdk.Msg,
) (uint64, sdk.Coin, error)
}

// NewGasCalculator creates a new GasCalculator instance.
Expand Down Expand Up @@ -79,23 +91,13 @@ func (c *txGasCalulator) BuildTx(
return nil, err
}

_, gas, err := c.SimulateMsgs(
encodingConfig.TxConfig,
account,
chainID,
msg,
)
if err != nil {
return nil, err
}
txBuilder.SetGasLimit(gas)

feecoin, err := CalculateFeeCoin(ctx, txfeesClient, gas)
gasAdjusted, feecoin, err := c.PriceMsgs(ctx, txfeesClient, encodingConfig.TxConfig, account, chainID, msg...)
if err != nil {
return nil, err
}

txBuilder.SetFeeAmount(sdk.NewCoins(feecoin))
txBuilder.SetGasLimit(gasAdjusted)
txBuilder.SetFeeAmount(sdk.Coins{feecoin})

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

// 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) {
_, gasAdjusted, err := c.SimulateMsgs(
encodingConfig,
account,
chainID,
msg,
)
if err != nil {
return 0, sdk.Coin{}, err
}

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

return gasAdjusted, feeCoin, nil
}

// 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 829a871

Please sign in to comment.