syntax = "proto3";

package sf.solana.serumhist.v1;
option go_package = "github.com/streamingfast/firehose-solana/types/pb/sf/solana/serumhist/v1;pbserumhist";

import "google/protobuf/timestamp.proto";

service SerumOrderTracker {
  rpc TrackOrder(TrackOrderRequest) returns (stream OrderTransition);
}

service SerumHistory {
  rpc GetFills(GetFillsRequest) returns (FillsResponse);
}

message GetFillsRequest {
  bytes trader = 1;
  bytes market = 2;
}

message FillsResponse {
  repeated Fill fill = 1;
  bool has_more = 2;
}

message TrackOrderRequest {
  bytes market = 1;
  string order_id = 2;
}

message OrderTransition {
  State previous_state = 1;
  State current_state = 2;
  Transition transition = 3;
  Order order = 4;
  Fill added_fill = 5;
  InstructionRef cancellation = 6;

 enum State {
     STATE_UNKNOWN = 0;
     STATE_APPROVED = 1;
     STATE_CANCEL_PENDING = 2;
     STATE_CANCELLED = 3;
     STATE_PARTIAL = 4;
     STATE_EXECUTED = 5;
 }

 enum Transition {
     TRANS_INIT = 0;
     TRANS_ACCEPTED = 1;
     TRANS_CANCELLED = 2;
     TRANS_FILLED = 3;
     TRANS_EXECUTED = 4;
 }
}

message InstructionRef {
  uint64 block_num = 1;
  bytes trx_id = 2;
  uint32 trx_idx = 3;
  uint32 inst_idx = 4;
  bytes block_id = 5;
  google.protobuf.Timestamp timestamp = 6;
}

message Checkpoint {
  uint64 last_written_block_num = 1;
  bytes last_written_block_id = 2;
}

message Fill {
  bytes trader = 1;
  bytes market = 2;
  string order_id = 3;
  Side side = 4;
  bool maker = 5;
  uint64 native_qty_paid = 6;
  uint64 native_qty_received = 7;
  uint64 native_fee_or_rebate = 8;
  FeeTier fee_tier = 9;
  google.protobuf.Timestamp timestamp = 10;

  uint64 block_num = 20;
  bytes block_id = 24;
  bytes trx_id = 25;
  uint32 trx_idx = 21;
  uint32 inst_idx = 22;
  uint64 order_seq_num = 23;
}

enum Side {
  BID = 0;
  ASK = 1;
}

enum FeeTier {
    Base = 0;
    SRM2 = 1;
    SRM3 = 2;
    SRM4 = 3;
    SRM5 = 4;
    SRM6 = 5;
    MSRM = 6;
}

message Order {
  uint64 num = 1;
  bytes market = 2;
  bytes trader = 3;

  Side side = 4;
  uint64 limit_price = 5;
  uint64 max_quantity = 6;

  OrderType type = 7;

  repeated Fill fills = 10; // this only gets populated for transport

  uint64 block_num = 20;
  bytes block_id = 24;
  bytes trx_id = 25;
  uint32 trx_idx = 21;
  uint32 inst_idx = 22;
}

enum OrderType {
  LIMIT = 0;
  IMMEDIATE_OR_CANCEL = 1;
  POST_ONLY = 2;
}