Skip to content

Commit

Permalink
Merge pull request #696 from neutron-org/feat/add_simulateXXQueries
Browse files Browse the repository at this point in the history
Feat: Add SimulateXX queries
  • Loading branch information
pr0n00gler authored Sep 24, 2024
2 parents 922d62a + 6bef62c commit 0ff43e4
Show file tree
Hide file tree
Showing 29 changed files with 7,747 additions and 3,344 deletions.
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
1 change: 0 additions & 1 deletion wasmbinding/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ 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)

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 @@ -91,6 +91,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
30 changes: 16 additions & 14 deletions x/dex/keeper/cancel_limit_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func (k Keeper) CancelLimitOrderCore(
goCtx context.Context,
trancheKey string,
callerAddr sdk.AccAddress,
) error {
) (makerCoinOut, takerCoinOut sdk.Coin, err error) {
ctx := sdk.UnwrapSDKContext(goCtx)

makerCoinOut, takerCoinOut, tradePairID, err := k.ExecuteCancelLimitOrder(ctx, trancheKey, callerAddr)
makerCoinOut, takerCoinOut, err = k.ExecuteCancelLimitOrder(ctx, trancheKey, callerAddr)
if err != nil {
return err
return sdk.Coin{}, sdk.Coin{}, err
}

coinsOut := sdk.NewCoins(makerCoinOut, takerCoinOut)
Expand All @@ -31,23 +31,25 @@ func (k Keeper) CancelLimitOrderCore(
coinsOut,
)
if err != nil {
return err
return sdk.Coin{}, sdk.Coin{}, err
}

// This will never panic since TradePairID has already been successfully constructed by ExecuteCancelLimitOrder
pairID := tradePairID.MustPairID()
makerDenom := makerCoinOut.Denom
takerDenom := takerCoinOut.Denom
// This will never panic since PairID has already been successfully constructed during tranche creation
pairID := types.MustNewPairID(makerDenom, takerDenom)
ctx.EventManager().EmitEvent(types.CancelLimitOrderEvent(
callerAddr,
pairID.Token0,
pairID.Token1,
tradePairID.MakerDenom,
tradePairID.TakerDenom,
makerDenom,
takerDenom,
makerCoinOut.Amount,
takerCoinOut.Amount,
trancheKey,
))

return nil
return makerCoinOut, takerCoinOut, nil
}

// ExecuteCancelLimitOrder handles the core logic for CancelLimitOrder -- removing remaining TokenIn from the
Expand All @@ -57,10 +59,10 @@ func (k Keeper) ExecuteCancelLimitOrder(
ctx sdk.Context,
trancheKey string,
callerAddr sdk.AccAddress,
) (makerCoinOut, takerCoinOut sdk.Coin, tradePairID *types.TradePairID, error error) {
) (makerCoinOut, takerCoinOut sdk.Coin, error error) {
trancheUser, found := k.GetLimitOrderTrancheUser(ctx, callerAddr.String(), trancheKey)
if !found {
return sdk.Coin{}, sdk.Coin{}, nil, types.ErrActiveLimitOrderNotFound
return sdk.Coin{}, sdk.Coin{}, types.ErrActiveLimitOrderNotFound
}

tradePairID, tickIndex := trancheUser.TradePairId, trancheUser.TickIndexTakerToMaker
Expand All @@ -73,7 +75,7 @@ func (k Keeper) ExecuteCancelLimitOrder(
},
)
if tranche == nil {
return sdk.Coin{}, sdk.Coin{}, nil, types.ErrActiveLimitOrderNotFound
return sdk.Coin{}, sdk.Coin{}, types.ErrActiveLimitOrderNotFound
}

makerAmountToReturn := tranche.RemoveTokenIn(trancheUser)
Expand All @@ -97,7 +99,7 @@ func (k Keeper) ExecuteCancelLimitOrder(
trancheUser.SharesWithdrawn = trancheUser.SharesOwned

if !makerAmountToReturn.IsPositive() && !takerAmountOut.IsPositive() {
return sdk.Coin{}, sdk.Coin{}, nil, sdkerrors.Wrapf(types.ErrCancelEmptyLimitOrder, "%s", tranche.Key.TrancheKey)
return sdk.Coin{}, sdk.Coin{}, sdkerrors.Wrapf(types.ErrCancelEmptyLimitOrder, "%s", tranche.Key.TrancheKey)
}

k.SaveTrancheUser(ctx, trancheUser)
Expand All @@ -110,5 +112,5 @@ func (k Keeper) ExecuteCancelLimitOrder(
makerCoinOut = sdk.NewCoin(tradePairID.MakerDenom, makerAmountToReturn)
takerCoinOut = sdk.NewCoin(tradePairID.TakerDenom, takerAmountOut)

return makerCoinOut, takerCoinOut, tradePairID, nil
return makerCoinOut, takerCoinOut, nil
}
2 changes: 1 addition & 1 deletion x/dex/keeper/grpc_query_estimate_multi_hop_swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (k Keeper) EstimateMultiHopSwap(
callerAddr := sdk.MustAccAddressFromBech32(req.Creator)
receiverAddr := sdk.MustAccAddressFromBech32(req.Receiver)

coinOut, err := k.MultiHopSwapCore(
coinOut, _, _, err := k.MultiHopSwapCore(
cacheCtx,
req.AmountIn,
req.Routes,
Expand Down
Loading

0 comments on commit 0ff43e4

Please sign in to comment.