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: Add SimulateXX queries #696

Merged
merged 17 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
86 changes: 84 additions & 2 deletions proto/neutron/dex/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ service Query {
option (google.api.http).get = "/neutron/dex/pool_reserves/{pair_id}/{token_in}/{tick_index}/{fee}";
}

// Queries the simulated result of a multihop swap
// DEPRECATED Queries the simulated result of a multihop swap
rpc EstimateMultiHopSwap(QueryEstimateMultiHopSwapRequest) returns (QueryEstimateMultiHopSwapResponse) {
option (google.api.http).get = "/neutron/dex/estimate_multi_hop_swap";
option deprecated = true;
}

// Queries the simulated result of a PlaceLimit order
// DEPRECATED Queries the simulated result of a PlaceLimit order
rpc EstimatePlaceLimitOrder(QueryEstimatePlaceLimitOrderRequest) returns (QueryEstimatePlaceLimitOrderResponse) {
option (google.api.http).get = "/neutron/dex/estimate_place_limit_order";
option deprecated = true;
}

// Queries a pool by pair, tick and fee
Expand All @@ -126,6 +128,36 @@ service Query {
option (google.api.http).get = "/neutron/dex/pool_metadata";
}

// Simulates MsgDeposit
rpc SimulateDeposit(QuerySimulateDepositRequest) returns (QuerySimulateDepositResponse) {
option (google.api.http).get = "/neutron/dex/simulate_deposit";
}

// Simulates MsgWithdrawal
rpc SimulateWithdrawal(QuerySimulateWithdrawalRequest) returns (QuerySimulateWithdrawalResponse) {
option (google.api.http).get = "/neutron/dex/simulate_withdrawal";
}

// Simulates MsgPlaceLimitOrder
rpc SimulatePlaceLimitOrder(QuerySimulatePlaceLimitOrderRequest) returns (QuerySimulatePlaceLimitOrderResponse) {
option (google.api.http).get = "/neutron/dex/simulate_place_limit_order";
}

// Simulates MsgWithdrawFilledLimitOrder
rpc SimulateWithdrawFilledLimitOrder(QuerySimulateWithdrawFilledLimitOrderRequest) returns (QuerySimulateWithdrawFilledLimitOrderResponse) {
option (google.api.http).get = "/neutron/dex/simulate_withdraw_filled_limit_order";
}

// Simulates MsgCancelLimitOrder
rpc SimulateCancelLimitOrder(QuerySimulateCancelLimitOrderRequest) returns (QuerySimulateCancelLimitOrderResponse) {
option (google.api.http).get = "/neutron/dex/simulate_cancel_limit_order";
}

// Simulates MsgMultiHopSwap
rpc SimulateMultiHopSwap(QuerySimulateMultiHopSwapRequest) returns (QuerySimulateMultiHopSwapResponse) {
option (google.api.http).get = "/neutron/dex/pool_metadata";
}

// this line is used by starport scaffolding # 2
}

Expand Down Expand Up @@ -260,6 +292,7 @@ message QueryGetPoolReservesResponse {
}

message QueryEstimateMultiHopSwapRequest {
// DEPRECATED: Use QuerySimulateMultiHopSwap
string creator = 1;
string receiver = 2;
repeated MultiHopRoute routes = 3;
Expand Down Expand Up @@ -290,6 +323,7 @@ message QueryEstimateMultiHopSwapResponse {
}

message QueryEstimatePlaceLimitOrderRequest {
// DEPRECATED: Use QuerySimulatePlaceLimitOrder
string creator = 1;
string receiver = 2;
string token_in = 3;
Expand Down Expand Up @@ -378,4 +412,52 @@ message QueryAllPoolMetadataResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QuerySimulateDepositRequest {
MsgDeposit msg = 1;
}

message QuerySimulateDepositResponse {
MsgDepositResponse resp = 1;
}

message QuerySimulateWithdrawalRequest {
MsgWithdrawal msg = 1;
}

message QuerySimulateWithdrawalResponse {
MsgWithdrawalResponse resp = 1;
}

message QuerySimulatePlaceLimitOrderRequest {
MsgPlaceLimitOrder msg = 1;
}

message QuerySimulatePlaceLimitOrderResponse {
MsgPlaceLimitOrderResponse resp = 1;
}

message QuerySimulateWithdrawFilledLimitOrderRequest {
MsgWithdrawFilledLimitOrder msg = 1;
}

message QuerySimulateWithdrawFilledLimitOrderResponse {
MsgWithdrawFilledLimitOrderResponse resp = 1;
}

message QuerySimulateCancelLimitOrderRequest {
MsgCancelLimitOrder msg = 1;
}

message QuerySimulateCancelLimitOrderResponse {
MsgCancelLimitOrderResponse resp = 1;
}

message QuerySimulateMultiHopSwapRequest {
MsgMultiHopSwap msg = 1;
}

message QuerySimulateMultiHopSwapResponse {
MsgMultiHopSwapResponse resp = 1;
}

// this line is used by starport scaffolding # 3
77 changes: 74 additions & 3 deletions proto/neutron/dex/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ message MsgDepositResponse {
(gogoproto.jsontag) = "reserve1_deposited"
];
repeated FailedDeposit failed_deposits = 3;
repeated cosmos.base.v1beta1.Coin shares_issued = 4 [
(gogoproto.moretags) = "yaml:\"shares_issued\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "shares_issued"
];
}

message MsgWithdrawal {
Expand All @@ -95,7 +101,26 @@ message MsgWithdrawal {
repeated uint64 fees = 7;
}

message MsgWithdrawalResponse {}
message MsgWithdrawalResponse {
string reserve0_withdrawn = 1 [
(gogoproto.moretags) = "yaml:\"reserve0_withdrawn\"",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "reserve0_withdrawn"
];
string reserve1_withdrawn = 2 [
(gogoproto.moretags) = "yaml:\"reserve1_withdrawn\"",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "reserve1_withdrawn"
];
repeated cosmos.base.v1beta1.Coin shares_burned = 3 [
(gogoproto.moretags) = "yaml:\"shares_burned\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "shares_burned"
];
}

enum LimitOrderType {
GOOD_TIL_CANCELLED = 0;
Expand Down Expand Up @@ -161,6 +186,14 @@ message MsgPlaceLimitOrderResponse {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.jsontag) = "taker_coin_out"
];

// Total amount of the token in that was immediately swapped for takerOutCoin
cosmos.base.v1beta1.Coin taker_coin_in = 4 [
(gogoproto.moretags) = "yaml:\"taker_coin_in\"",
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.jsontag) = "taker_coin_in"
];
}

message MsgWithdrawFilledLimitOrder {
Expand All @@ -171,7 +204,22 @@ message MsgWithdrawFilledLimitOrder {
string tranche_key = 2;
}

message MsgWithdrawFilledLimitOrderResponse {}
message MsgWithdrawFilledLimitOrderResponse {
// Total amount of taker reserves that were withdrawn
cosmos.base.v1beta1.Coin taker_coin_out = 1 [
(gogoproto.moretags) = "yaml:\"taker_coin_out\"",
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.jsontag) = "taker_coin_out"
];
// Total amount of maker reserves that were withdrawn --only applies to inactive LimitOrders
cosmos.base.v1beta1.Coin maker_coin_out = 2 [
(gogoproto.moretags) = "yaml:\"maker_coin_out\"",
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.jsontag) = "maker_coin_out"
];
}

message MsgCancelLimitOrder {
option (amino.name) = "dex/MsgCancelLimitOrder";
Expand All @@ -181,7 +229,22 @@ message MsgCancelLimitOrder {
string tranche_key = 2;
}

message MsgCancelLimitOrderResponse {}
message MsgCancelLimitOrderResponse {
// Total amount of taker reserves that were withdrawn
cosmos.base.v1beta1.Coin taker_coin_out = 1 [
(gogoproto.moretags) = "yaml:\"taker_coin_out\"",
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.jsontag) = "taker_coin_out"
];
// Total amount of maker reserves that were canceled
cosmos.base.v1beta1.Coin maker_coin_out = 2 [
(gogoproto.moretags) = "yaml:\"maker_coin_out\"",
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.jsontag) = "maker_coin_out"
];
}

message MultiHopRoute {
repeated string hops = 1;
Expand Down Expand Up @@ -213,10 +276,18 @@ message MsgMultiHopSwap {

message MsgMultiHopSwapResponse {
cosmos.base.v1beta1.Coin coin_out = 1 [
(gogoproto.moretags) = "yaml:\"coin_out\"",
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.jsontag) = "coin_out"
];
MultiHopRoute route = 2;
repeated cosmos.base.v1beta1.Coin dust = 3 [
(gogoproto.moretags) = "yaml:\"dust\"",
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin",
(gogoproto.jsontag) = "dust"
];
}

message MsgUpdateParams {
Expand Down
16 changes: 16 additions & 0 deletions wasmbinding/bindings/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,18 @@ type DexQuery struct {
PoolMetadata *dextypes.QueryGetPoolMetadataRequest `json:"pool_metadata"`
// Queries a list of PoolMetadata items.
PoolMetadataAll *dextypes.QueryAllPoolMetadataRequest `json:"pool_metadata_all"`
/// Simulates MsgDeposit
SimulateDeposit *dextypes.QuerySimulateDepositRequest `json:"simulate_deposit"`
/// Simulates MsgWithdrawal
SimulateWithdrawal *dextypes.QuerySimulateWithdrawalRequest `json:"simulate_withdrawal"`
/// Simulates MsgPlaceLimitOrder
SimulatePlaceLimitOrder *QuerySimulatePlaceLimitOrderRequest `json:"simulate_place_limit_order"`
/// Simulates MsgWithdrawFilledLimitOrder
SimulateWithdrawFilledLimitOrder *dextypes.QuerySimulateWithdrawFilledLimitOrderRequest `json:"simulate_withdraw_filled_limit_order"`
/// Simulates MsgCancelLimitOrder
SimulateCancelLimitOrder *dextypes.QuerySimulateCancelLimitOrderRequest `json:"simulate_cancel_limit_order"`
/// Simulates MsgMultiHopSwap
SimulateMultiHopSwap *dextypes.QuerySimulateMultiHopSwapRequest `json:"simulate_multi_hop_swap"`
}

// QueryEstimatePlaceLimitOrderRequest is a copy dextypes.QueryEstimatePlaceLimitOrderRequest with altered ExpirationTime field,
Expand All @@ -289,3 +301,7 @@ type QueryEstimatePlaceLimitOrderRequest struct {
ExpirationTime *uint64 `json:"expiration_time,omitempty"`
MaxAmountOut *math.Int `json:"max_amount_out"`
}

type QuerySimulatePlaceLimitOrderRequest struct {
Msg *MsgPlaceLimitOrder `json:"msg"`
}
44 changes: 44 additions & 0 deletions wasmbinding/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
sdkquery "github.com/cosmos/cosmos-sdk/types/query"

dextypes "github.com/neutron-org/neutron/v4/x/dex/types"
dexutils "github.com/neutron-org/neutron/v4/x/dex/utils"

contractmanagertypes "github.com/neutron-org/neutron/v4/x/contractmanager/types"

Expand Down Expand Up @@ -201,6 +202,49 @@ func (qp *QueryPlugin) DexQuery(ctx sdk.Context, query bindings.DexQuery) (data
data, err = dexQuery(ctx, query.TickLiquidityAll, qp.dexKeeper.TickLiquidityAll)
case query.UserDepositsAll != nil:
data, err = dexQuery(ctx, query.UserDepositsAll, qp.dexKeeper.UserDepositsAll)
case query.SimulateDeposit != nil:
data, err = dexQuery(ctx, query.SimulateDeposit, qp.dexKeeper.SimulateDeposit)
case query.SimulateWithdrawal != nil:
data, err = dexQuery(ctx, query.SimulateWithdrawal, qp.dexKeeper.SimulateWithdrawal)
case query.SimulatePlaceLimitOrder != nil:
q := dextypes.QuerySimulatePlaceLimitOrderRequest{
Msg: &dextypes.MsgPlaceLimitOrder{
TokenIn: query.SimulatePlaceLimitOrder.Msg.TokenIn,
TokenOut: query.SimulatePlaceLimitOrder.Msg.TokenOut,
//nolint: staticcheck // TODO: remove in next release
TickIndexInToOut: query.SimulatePlaceLimitOrder.Msg.TickIndexInToOut,
AmountIn: query.SimulatePlaceLimitOrder.Msg.AmountIn,
MaxAmountOut: query.SimulatePlaceLimitOrder.Msg.MaxAmountOut,
},
}
orderTypeInt, ok := dextypes.LimitOrderType_value[query.SimulatePlaceLimitOrder.Msg.OrderType]
if !ok {
return nil, errors.Wrap(dextypes.ErrInvalidOrderType,
fmt.Sprintf(
"got \"%s\", expected one of %s",
query.SimulatePlaceLimitOrder.Msg.OrderType,
strings.Join(maps.Keys(dextypes.LimitOrderType_value), ", ")),
)
}
q.Msg.OrderType = dextypes.LimitOrderType(orderTypeInt)
if query.SimulatePlaceLimitOrder.Msg.ExpirationTime != nil {
t := time.Unix(int64(*query.SimulatePlaceLimitOrder.Msg.ExpirationTime), 0)
q.Msg.ExpirationTime = &t
}
if limitPriceStr := query.SimulatePlaceLimitOrder.Msg.LimitSellPrice; limitPriceStr != "" {
limitPriceDec, err := dexutils.ParsePrecDecScientificNotation(limitPriceStr)
if err != nil {
return nil, errors.Wrapf(err, "cannot parse string %s for limit price", limitPriceStr)
}
q.Msg.LimitSellPrice = &limitPriceDec
}
data, err = dexQuery(ctx, &q, qp.dexKeeper.SimulatePlaceLimitOrder)
case query.SimulateWithdrawFilledLimitOrder != nil:
data, err = dexQuery(ctx, query.SimulateWithdrawFilledLimitOrder, qp.dexKeeper.SimulateWithdrawFilledLimitOrder)
case query.SimulateCancelLimitOrder != nil:
data, err = dexQuery(ctx, query.SimulateCancelLimitOrder, qp.dexKeeper.SimulateCancelLimitOrder)
case query.SimulateMultiHopSwap != nil:
data, err = dexQuery(ctx, query.SimulateMultiHopSwap, qp.dexKeeper.SimulateMultiHopSwap)

default:
return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown neutron.dex query type"}
Expand Down
6 changes: 6 additions & 0 deletions wasmbinding/stargate_allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ func AcceptedStargateQueries() wasmkeeper.AcceptedQueries {
"/neutron.dex.Query/PoolByID": &dextypes.QueryPoolResponse{},
"/neutron.dex.Query/PoolMetadata": &dextypes.QueryGetPoolMetadataResponse{},
"/neutron.dex.Query/PoolMetadataAll": &dextypes.QueryAllPoolMetadataResponse{},
"/neutron.dex.Query/SimulateDeposit": &dextypes.QuerySimulateDepositResponse{},
"/neutron.dex.Query/SimulateWithdrawal": &dextypes.QuerySimulateWithdrawalResponse{},
"/neutron.dex.Query/SimulatePlaceLimitOrder": &dextypes.QuerySimulatePlaceLimitOrderResponse{},
"/neutron.dex.Query/SimulateWithdrawFilledLimitOrder": &dextypes.QuerySimulateWithdrawFilledLimitOrderResponse{},
"/neutron.dex.Query/SimulateCancelLimitOrder": &dextypes.QuerySimulateCancelLimitOrderResponse{},
"/neutron.dex.Query/SimulateMultiHopSwap": &dextypes.QuerySimulateMultiHopSwapResponse{},

// oracle
"/slinky.oracle.v1.Query/GetAllCurrencyPairs": &oracletypes.GetAllCurrencyPairsResponse{},
Expand Down
Loading
Loading