Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simulate swap as part of quotes #547

Merged
merged 9 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/v26/app"
txfeestypes "github.com/osmosis-labs/osmosis/v26/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,
)
p0mvn marked this conversation as resolved.
Show resolved Hide resolved
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
Comment on lines +145 to +146
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance the math.Int type definition with proper schema.

The current definition as an empty object type lacks proper schema definition for validation and documentation purposes. Consider adding proper type constraints and description.

   math.Int:
-    type: object
+    type: object
+    description: Represents a arbitrary-precision integer using math/big package
+    properties:
+      i:
+        type: string
+        description: String representation of the arbitrary-precision integer
+    required:
+      - i
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
math.Int:
type: object
math.Int:
type: object
description: Represents a arbitrary-precision integer using math/big package
properties:
i:
type: string
description: String representation of the arbitrary-precision integer
required:
- i

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
47 changes: 34 additions & 13 deletions domain/cosmos/tx/msg_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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 +90,13 @@ func (c *txGasCalulator) BuildTx(
return nil, err
}

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

feecoin, err := CalculateFeeCoin(ctx, txfeesClient, gas)
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 +144,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
Loading