/*
Protocol buffer definition for exchanges-wrapper exch_srv.py
https://grpc.io/docs/languages/python/quickstart/
For compile use next command
$ python3 -m grpc_tools.protoc -I proto --python_out=. --grpc_python_out=. proto/exchanges_wrapper/api.proto
*/

syntax = "proto3";

package martin;

service Martin {
  rpc OpenClientConnection (OpenClientConnectionRequest) returns (OpenClientConnectionId) {}
  rpc FetchServerTime (OpenClientConnectionId) returns (FetchServerTimeResponse) {}
  rpc FetchOpenOrders (MarketRequest) returns (FetchOpenOrdersResponse) {}
  rpc CancelAllOrders (MarketRequest) returns (FetchOpenOrdersResponse) {}
  rpc FetchExchangeInfoSymbol (MarketRequest) returns (FetchExchangeInfoSymbolResponse) {}
  rpc FetchAccountInformation (OpenClientConnectionId) returns (FetchAccountBalanceResponse) {}
  rpc FetchOrderBook (MarketRequest) returns (FetchOrderBookResponse) {}
  rpc FetchSymbolPriceTicker (MarketRequest) returns (FetchSymbolPriceTickerResponse) {}
  rpc FetchTickerPriceChangeStatistics (MarketRequest) returns (FetchTickerPriceChangeStatisticsResponse) {}
  rpc FetchKlines (FetchKlinesRequest) returns (FetchKlinesResponse) {}
  rpc FetchAccountTradeList (AccountTradeListRequest) returns (AccountTradeListResponse) {}
  rpc OnTickerUpdate (MarketRequest) returns (stream OnTickerUpdateResponse) {}
  rpc OnOrderBookUpdate (MarketRequest) returns (stream FetchOrderBookResponse) {}
  rpc StopStream (MarketRequest) returns (SimpleResponse) {}
  rpc StartStream (StartStreamRequest) returns (SimpleResponse) {}
  rpc OnFundsUpdate (OnFundsUpdateRequest) returns (stream OnFundsUpdateResponse) {}
  rpc OnOrderUpdate (MarketRequest) returns (stream OnOrderUpdateResponse) {}
  rpc CreateLimitOrder (CreateLimitOrderRequest) returns (CreateLimitOrderResponse) {}
  rpc CancelOrder (CancelOrderRequest) returns (CancelOrderResponse) {}
  rpc FetchOrder (FetchOrderRequest) returns (FetchOrderResponse) {}
  rpc ResetRateLimit (OpenClientConnectionId) returns (SimpleResponse) {}
  rpc OnKlinesUpdate(FetchKlinesRequest) returns (stream OnKlinesUpdateResponse) {}
  rpc FetchFundingWallet(FetchFundingWalletRequest) returns (FetchFundingWalletResponse) {}
}

message FetchFundingWalletRequest{
  int64 client_id = 1;
  string trade_id = 2;
  string asset = 3;
  bool need_btc_valuation = 4;
  int64 receive_window = 5;
}

message FetchFundingWalletResponse {
  message Balances {
    string asset = 1;
    string free = 2;
    string locked = 3;
    string freeze = 4;
    string withdrawing = 5;
    string btc_valuation = 6;
  }
  repeated Balances balances = 1;
}

message CancelOrderResponse{
    string symbol = 1;
    string origClientOrderId = 2;
    uint64 orderId = 3;
    int32 orderListId = 4;
    string clientOrderId = 5;
    uint64 transactTime = 6;
    string price = 7;
    string origQty = 8;
    string executedQty = 9;
    string cummulativeQuoteQty = 10;
    string status = 11;
    string timeInForce = 12;
    string type = 13;
    string side = 14;
}

message CancelOrderRequest{
  int64 client_id = 1;
  string trade_id = 2;
  string symbol = 3;
  int64 order_id = 4;
}

message CreateLimitOrderResponse{
    string symbol = 1;
    uint64 orderId = 2;
    sint32 orderListId = 3;
    string clientOrderId = 4;
    uint64 transactTime = 5;
    string price = 6;
    string origQty = 7;
    string executedQty = 8;
    string cummulativeQuoteQty = 9;
    string status = 10;
    string timeInForce = 11;
    string type = 12;
    string side = 13;
}

message CreateLimitOrderRequest{
  int64 client_id = 1;
  string trade_id = 2;
  string symbol = 3;
  bool buy_side = 4;
  string quantity = 5;
  string price = 6;
  int64 new_client_order_id = 7;
}

message OnOrderUpdateResponse{
  string symbol = 1;
  string client_order_id = 2;
  string side = 3;
  string order_type = 4;
  string time_in_force = 5;
  string order_quantity = 6;
  string order_price = 7;
  string stop_price = 8;
  string iceberg_quantity = 9;
  sint32 order_list_id = 10;
  string original_client_id = 11;
  string execution_type = 12;
  string order_status = 13;
  string order_reject_reason = 14;
  uint64 order_id = 15;
  string last_executed_quantity = 16;
  string cumulative_filled_quantity = 17;
  string last_executed_price = 18;
  string commission_amount = 19;
  string commission_asset = 20;
  uint64 transaction_time = 21;
  sint64 trade_id = 22;
  uint64 ignore_a = 23;
  bool in_order_book = 24;
  bool is_maker_side = 25;
  bool ignore_b = 26;
  uint64 order_creation_time = 27;
  string quote_asset_transacted = 28;
  string last_quote_asset_transacted = 29;
  string quote_order_quantity = 30;
}

message OnFundsUpdateResponse {
  string funds = 1;
}

message OnFundsUpdateRequest {
  int64 client_id = 1;
  string trade_id = 2;
  string symbol = 3;
  string base_asset = 4;
  string quote_asset = 5;
}

message SimpleResponse {
  bool success = 1;
}

message OnTickerUpdateResponse {
  string symbol = 1;
  string open_price = 2;
  string close_price = 3;
  uint64 event_time = 4;
}

message AccountTradeListResponse {
  message Trade {
    string symbol = 1;
    uint64 id = 2;
    uint64 orderId = 3;
    sint32 orderListId = 4;
    string price = 5;
    string qty = 6;
    string quoteQty = 7;
    string commission = 8;
    string commissionAsset = 9;
    uint64 time = 10;
    bool isBuyer = 11;
    bool isMaker = 12;
    bool isBestMatch = 13;
  }
  repeated Trade items = 1;
}

message AccountTradeListRequest {
  int64 client_id = 1;
  string trade_id = 2;
  string symbol = 3;
  uint32 limit = 4;
  int64 start_time = 5;
}

message FetchKlinesResponse {
  repeated string klines = 1;
}

message OnKlinesUpdateResponse {
  string symbol = 1;
  string interval =2;
  string candle = 3;
}

message FetchKlinesRequest {
  int64 client_id = 1;
  string trade_id = 2;
  string symbol = 3;
  string interval = 4;
  uint32 limit = 5;
}

message FetchTickerPriceChangeStatisticsResponse {
  string symbol = 1;
  string priceChange = 2;
  string priceChangePercent = 3;
  string weightedAvgPrice = 4;
  string prevClosePrice = 5;
  string lastPrice = 6;
  string lastQty = 7;
  string bidPrice = 8;
  string bidQty = 9;
  string askPrice = 10;
  string askQty = 11;
  string openPrice = 12;
  string highPrice = 13;
  string lowPrice = 14;
  string volume = 15;
  string quoteVolume = 16;
  uint64 openTime = 17;
  uint64 closeTime = 18;
  uint64 firstId = 19;
  uint64 lastId = 20;
  uint64 count = 21;
}

message FetchSymbolPriceTickerResponse {
  string symbol = 1;
  string price = 2;
}

message FetchOrderBookResponse {
  uint64 lastUpdateId = 1;
  repeated string bids = 2;
  repeated string asks = 3;
}

message FetchAccountBalanceResponse {
  message Balances {
    string asset = 1;
    string free = 2;
    string locked = 3;
  }
  repeated Balances balances = 1;
}

message FetchExchangeInfoSymbolResponse {
  message Filters {
    message PriceFilter {
      string filterType = 1;
      string minPrice = 2;
      string maxPrice = 3;
      string tickSize = 4;
    }
    optional PriceFilter price_filter = 1;

    message PercentPrice {
      string filterType = 1;
      string multiplierUp = 2;
      string multiplierDown = 3;
      uint32 avgPriceMins = 4;
    }
    optional PercentPrice percent_price = 2;

    message LotSize {
      string filterType = 1;
      string minQty = 2;
      string maxQty = 3;
      string stepSize = 4;
    }
    optional LotSize lot_size = 3;

    message MinNotional {
      string filterType = 1;
      string minNotional = 2;
      bool applyToMarket = 3;
      uint32 avgPriceMins = 4;
    }
    optional MinNotional min_notional = 4;

    message IcebergParts {
      string filterType = 1;
      uint32 limit = 2;
    }
    optional IcebergParts iceberg_parts = 5;

    message MarketLotSize {
      string filterType = 1;
      string minQty = 2;
      string maxQty = 3;
      string stepSize = 4;
    }
    optional MarketLotSize market_lot_size = 6;

    message MaxNumOrders {
      string filterType = 1;
      uint32 maxNumOrders = 2;
    }
    optional MaxNumOrders max_num_orders = 7;

    message MaxNumAlgoOrders {
      string filterType = 1;
      uint32 maxNumAlgoOrders = 2;
    }
    optional MaxNumAlgoOrders max_num_algo_orders = 8;

    message MaxNumIcebergOrders {
      string filterType = 1;
      uint32 maxNumIcebergOrders = 2;
    }
    optional MaxNumIcebergOrders max_num_iceberg_orders = 9;

    message MaxPosition {
      string filterType = 1;
      string maxPosition = 2;
    }
    optional MaxPosition max_position = 10;
}

  string symbol = 1;
  string status = 2;
  string baseAsset = 3;
  uint32 baseAssetPrecision = 4;
  string quoteAsset = 5;
  uint32 quotePrecision = 6;
  uint32 quoteAssetPrecision = 7;
  uint32 baseCommissionPrecision = 8;
  uint32 quoteCommissionPrecision = 9;
  repeated string orderTypes = 10;
  bool icebergAllowed = 11;
  bool ocoAllowed = 12;
  bool quoteOrderQtyMarketAllowed = 13;
  bool allowTrailingStop = 14;
  bool cancelReplaceAllowed = 15;
  bool isSpotTradingAllowed = 16;
  bool isMarginTradingAllowed = 17;
  Filters filters = 18;
  repeated string permissions = 19;
}

message CancelAllOrdersResponse {
  message CancelOrder {
    string symbol = 1;
    string origClientOrderId = 2;
    uint64 orderId = 3;
    int32 orderListId = 4;
    string clientOrderId = 5;
    uint64 transactTime = 6;
    string price = 7;
    string origQty = 8;
    string executedQty = 9;
    string cummulativeQuoteQty = 10;
    string status = 11;
    string timeInForce = 12;
    string type = 13;
    string side = 14;
  }
  repeated CancelOrder items = 1;
}

message FetchOrderRequest{
  int64 client_id = 1;
  string trade_id = 2;
  string symbol = 3;
  int64 order_id = 4;
  bool filled_update_call = 5;
}

message FetchOrderResponse {
  string symbol = 1;
  uint64 orderId = 2;
  sint32 orderListId = 3;
  string clientOrderId = 4;
  string price = 5;
  string origQty = 6;
  string executedQty = 7;
  string cummulativeQuoteQty = 8;
  string status = 9;
  string timeInForce = 10;
  string type = 11;
  string side = 12;
  string stopPrice = 13;
  string icebergQty = 14;
  uint64 time = 15;
  uint64 updateTime = 16;
  bool isWorking = 17;
  string origQuoteOrderQty = 18;
}

message FetchOpenOrdersResponse {
  int32 rate_limiter = 1;
  message Order {
    string symbol = 1;
    uint64 orderId = 2;
    sint32 orderListId = 3;
    string clientOrderId = 4;
    string price = 5;
    string origQty = 6;
    string executedQty = 7;
    string cummulativeQuoteQty = 8;
    string status = 9;
    string timeInForce = 10;
    string type = 11;
    string side = 12;
    string stopPrice = 13;
    string icebergQty = 14;
    uint64 time = 15;
    uint64 updateTime = 16;
    bool isWorking = 17;
    string origQuoteOrderQty = 18;
  }
  repeated Order items = 2;
}

message MarketRequest {
  int64 client_id = 1;
  string trade_id = 2;
  string symbol = 3;
}

message StartStreamRequest {
  int64 client_id = 1;
  string trade_id = 2;
  string symbol = 3;
  int32 market_stream_count = 4;
  int32 user_stream_count = 5;
}

message OpenClientConnectionRequest {
  string trade_id = 1;
  string account_name = 2;
  int32 rate_limiter = 3;
}

message OpenClientConnectionId {
  // if connection successfully open, return it id for next use
  int64 client_id = 1;
  string trade_id = 2;
  string srv_version = 3;
  int32 rate_limiter = 4;
  string exchange = 5;
}

message FetchServerTimeRequest {
  int64 client_id = 1;
  string trade_id = 2;
}

message FetchServerTimeResponse {
  uint64 server_time = 1;
}
