syntax = "proto3";

package elys.perpetual;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "elys/perpetual/params.proto";
import "elys/perpetual/types.proto";
import "elys/perpetual/pool.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/elys-network/elys/x/perpetual/types";

// Query defines the gRPC querier service.
service Query {

  // Parameters queries the parameters of the module.
  rpc Params(ParamsRequest) returns (ParamsResponse) {
    option (google.api.http).get = "/elys-network/elys/perpetual/params";
  }

  // Queries a list of positions.
  rpc GetPositions(PositionsRequest) returns (PositionsResponse) {
    option (google.api.http).get =
        "/elys-network/elys/perpetual/positions/{pagination.key}";
  }

  // Queries a list of mtp positions by pool.
  rpc GetPositionsByPool(PositionsByPoolRequest)
      returns (PositionsByPoolResponse) {
    option (google.api.http).get = "/elys-network/elys/perpetual/mtps-by-pool/"
                                   "{amm_pool_id}/{pagination.key}";
  }

  // Retuns the total number of open and lifetime mtps.
  rpc GetStatus(StatusRequest) returns (StatusResponse) {
    option (google.api.http).get = "/elys-network/elys/perpetual/status";
  }

  // Queries a list of mtp positions for a given address.
  rpc GetPositionsForAddress(PositionsForAddressRequest)
      returns (PositionsForAddressResponse) {
    option (google.api.http).get =
        "/elys-network/elys/perpetual/mtps-for-address/{address}/"
        "{pagination.key}";
  }

  // Queries a list of whitelisted addresses.
  rpc GetWhitelist(WhitelistRequest) returns (WhitelistResponse) {
    option (google.api.http).get =
        "/elys-network/elys/perpetual/whitelist/{pagination.key}";
  }

  // Is an address whitelisted?
  rpc IsWhitelisted(IsWhitelistedRequest) returns (IsWhitelistedResponse) {
    option (google.api.http).get =
        "/elys-network/elys/perpetual/is-whitelisted";
  }

  // Queries a single pool given its index.
  rpc Pool(QueryGetPoolRequest) returns (QueryGetPoolResponse) {
    option (google.api.http).get = "/elys-network/elys/perpetual/pool/{index}";
  }

  // Queries a list of all pools.
  rpc Pools(QueryAllPoolRequest) returns (QueryAllPoolResponse) {
    option (google.api.http).get =
        "/elys-network/elys/perpetual/pool/{pagination.key}";
  }

  // Queries a single mtp position given its address and id.
  rpc MTP(MTPRequest) returns (MTPResponse) {
    option (google.api.http).get =
        "/elys-network/elys/perpetual/mtp/{address}/{id}";
  }

  // Queries an estimation of a new open position details.
  rpc OpenEstimation(QueryOpenEstimationRequest)
      returns (QueryOpenEstimationResponse) {
    option (google.api.http).get =
        "/elys-network/elys/perpetual/open-estimation";
  }

  // Queries a list of CloseEstimation items.
  rpc CloseEstimation(QueryCloseEstimationRequest)
      returns (QueryCloseEstimationResponse) {
    option (google.api.http).get =
        "/elys-network/elys/perpetual/close-estimation/{position_id}";
  }
}

message Fees {
  string total_fees_base_currency = 1 [
    (cosmos_proto.scalar) = "cosmos.Int",
    (gogoproto.customtype) = "cosmossdk.io/math.Int",
    (gogoproto.nullable) = false
  ];
  string borrow_interest_fees_liability_asset = 2 [
    (cosmos_proto.scalar) = "cosmos.Int",
    (gogoproto.customtype) = "cosmossdk.io/math.Int",
    (gogoproto.nullable) = false
  ];
  string borrow_interest_fees_base_currency = 3 [
    (cosmos_proto.scalar) = "cosmos.Int",
    (gogoproto.customtype) = "cosmossdk.io/math.Int",
    (gogoproto.nullable) = false
  ];
  string funding_fees_liquidity_asset = 4 [
    (cosmos_proto.scalar) = "cosmos.Int",
    (gogoproto.customtype) = "cosmossdk.io/math.Int",
    (gogoproto.nullable) = false
  ];
  string funding_fees_base_currency = 5 [
    (cosmos_proto.scalar) = "cosmos.Int",
    (gogoproto.customtype) = "cosmossdk.io/math.Int",
    (gogoproto.nullable) = false
  ];
}

message MtpAndPrice {
  MTP mtp = 1;
  string trading_asset_price = 2 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  cosmos.base.v1beta1.Coin pnl = 3 [ (gogoproto.nullable) = false ];
  string effective_leverage = 4 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string liquidation_price = 5 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  Fees fees = 6;
}

// ParamsRequest is request type for the Query/Params RPC method.
message ParamsRequest {}

// ParamsResponse is response type for the Query/Params RPC method.
message ParamsResponse {

  // params holds all the parameters of this module.
  Params params = 1 [ (gogoproto.nullable) = false ];
}

message PositionsRequest {
  cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message PositionsResponse {
  repeated MtpAndPrice mtps = 1;
  cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message PositionsByPoolRequest {
  uint64 amm_pool_id = 1;
  cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

message PositionsByPoolResponse {
  repeated MtpAndPrice mtps = 1;
  cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message StatusRequest {}

message StatusResponse {
  uint64 open_mtp_count = 1;
  uint64 lifetime_mtp_count = 2;
}

message PositionsForAddressRequest {
  string address = 1;
  cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

message PositionsForAddressResponse {
  repeated MtpAndPrice mtps = 1;
  cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message WhitelistRequest {
  cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message WhitelistResponse {
  repeated string whitelist = 1;
  cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message IsWhitelistedRequest { string address = 1; }

message IsWhitelistedResponse {
  string address = 1;
  bool is_whitelisted = 2;
}

message QueryGetPoolRequest { uint64 index = 1; }

message QueryGetPoolResponse {
  PoolResponse pool = 1 [ (gogoproto.nullable) = false ];
}

message QueryAllPoolRequest {
  cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryAllPoolResponse {
  repeated PoolResponse pool = 1 [ (gogoproto.nullable) = false ];
  cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message MTPRequest {
  string address = 1;
  uint64 id = 2;
}

message MTPResponse { MtpAndPrice mtp = 1; }

message QueryOpenEstimationRequest {
  Position position = 1;
  string leverage = 2 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string trading_asset = 3;
  cosmos.base.v1beta1.Coin collateral = 4 [ (gogoproto.nullable) = false ];
  string address = 5;
  string take_profit_price = 6 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  uint64 poolId = 7;
  string limit_price = 8 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
}

message QueryOpenEstimationResponse {
  Position position = 1;
  string effective_leverage = 2 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string trading_asset = 3;
  cosmos.base.v1beta1.Coin collateral = 4 [ (gogoproto.nullable) = false ];
  string hourly_interest_rate = 5 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  cosmos.base.v1beta1.Coin position_size = 6 [ (gogoproto.nullable) = false ];
  string open_price = 7 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string take_profit_price = 8 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string liquidation_price = 9 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  cosmos.base.v1beta1.Coin estimated_pnl = 10 [ (gogoproto.nullable) = false ];
  cosmos.base.v1beta1.Coin available_liquidity = 11
      [ (gogoproto.nullable) = false ];
  string slippage = 12 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string borrow_interest_rate = 13 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string funding_rate = 14 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string price_impact = 15 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  cosmos.base.v1beta1.Coin custody = 16 [ (gogoproto.nullable) = false ];
  cosmos.base.v1beta1.Coin liabilities = 17 [ (gogoproto.nullable) = false ];
  string limit_price = 18 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string weight_breaking_fee = 19 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
}

message PoolResponse {
  uint64 amm_pool_id = 1;
  string health = 2 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string borrow_interest_rate = 3 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  repeated PoolAsset pool_assets_long = 4 [ (gogoproto.nullable) = false ];
  repeated PoolAsset pool_assets_short = 5 [ (gogoproto.nullable) = false ];
  int64 last_height_borrow_interest_rate_computed = 6;
  // funding rate, if positive longs pay shorts, if negative shorts pay longs
  string funding_rate = 7 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string net_open_interest = 8 [
    (cosmos_proto.scalar) = "cosmos.Int",
    (gogoproto.customtype) = "cosmossdk.io/math.Int",
    (gogoproto.nullable) = false
  ];
  // Rate at which long pays/receive from short
  // if negative longs pay, if positive long receives
  string long_rate = 9 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string short_rate = 10 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  cosmos.base.v1beta1.Coin total_liabilities = 11
      [ (gogoproto.nullable) = false ];
  string total_long_open_interest = 12 [
    (cosmos_proto.scalar) = "cosmos.Int",
    (gogoproto.customtype) = "cosmossdk.io/math.Int",
    (gogoproto.nullable) = false
  ];
  string total_short_open_interest = 13 [
    (cosmos_proto.scalar) = "cosmos.Int",
    (gogoproto.customtype) = "cosmossdk.io/math.Int",
    (gogoproto.nullable) = false
  ];
  string leverage_max = 14 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
}

message QueryCloseEstimationRequest {
  string address = 1;
  uint64 position_id = 2;
  string close_amount = 3 [
    (cosmos_proto.scalar) = "cosmos.Int",
    (gogoproto.customtype) = "cosmossdk.io/math.Int",
    (gogoproto.nullable) = false
  ];
}

message QueryCloseEstimationResponse {
  Position position = 1;
  cosmos.base.v1beta1.Coin position_size = 2 [ (gogoproto.nullable) = false ];
  cosmos.base.v1beta1.Coin liabilities = 3 [ (gogoproto.nullable) = false ];
  string price_impact = 4 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string liquidation_price = 5 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string max_close_amount = 6 [
    (cosmos_proto.scalar) = "cosmos.Int",
    (gogoproto.customtype) = "cosmossdk.io/math.Int",
    (gogoproto.nullable) = false
  ];
  cosmos.base.v1beta1.Coin borrow_interest_unpaid_liability = 7
      [ (gogoproto.nullable) = false ];
  cosmos.base.v1beta1.Coin returning_amount = 8
      [ (gogoproto.nullable) = false ];
  cosmos.base.v1beta1.Coin paying_liabilities = 9
      [ (gogoproto.nullable) = false ];

  string closing_price = 10 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  cosmos.base.v1beta1.Coin custody = 11 [ (gogoproto.nullable) = false ];
  cosmos.base.v1beta1.Coin collateral = 12 [ (gogoproto.nullable) = false ];
  string weight_breaking_fee = 13 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
  string slippage = 14 [
    (cosmos_proto.scalar) = "cosmos.Dec",
    (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
    (gogoproto.nullable) = false
  ];
}