syntax = "proto3";

import "transaction.proto";

package types;

// trade 交易部分
//
message Trade {
    oneof value {
        TradeForSell       sellLimit  = 1;
        TradeForBuy        buyMarket  = 2;
        TradeForRevokeSell revokeSell = 3;
        TradeForBuyLimit   buyLimit   = 5;
        TradeForSellMarket sellMarket = 6;
        TradeForRevokeBuy  revokeBuy  = 7;
    }
    int32 ty = 4;
}

// 创建众筹交易,确定一手交易的token的数量，单价以及总共有多少手token可以进行众筹
message TradeForSell {
    string tokenSymbol = 1;
    // 每一手出售的token的数量
    int64 amountPerBoardlot = 2;
    // 起卖手数,必须达到这个门槛才允许进行交易
    int64 minBoardlot = 3;
    // 每一手token的价格
    int64 pricePerBoardlot = 4;
    int64 totalBoardlot    = 5;
    // 此次出售的起始时间，如果非众筹则可以忽略此时间
    int64 starttime = 6;
    int64 stoptime  = 7;
    bool  crowdfund = 8;
    // 资产来源
    string assetExec = 9;
}

// 购买者发起交易用来购买token持有者之前挂单出售的token
// 其中的hash为token出售者发起出售交易的hash
message TradeForBuy {
    string sellID      = 1;
    int64  boardlotCnt = 2;
}

// 允许token的持有者撤销之前未成交出售token的挂单
message TradeForRevokeSell {
    string sellID = 1;
}

// 限价买单构造请求
message TradeForBuyLimit {
    string tokenSymbol       = 1;
    int64  amountPerBoardlot = 2;
    int64  minBoardlot       = 3;
    int64  pricePerBoardlot  = 4;
    int64  totalBoardlot     = 5;
    string assetExec         = 6;
}

// 现价卖单
message TradeForSellMarket {
    string buyID       = 1;
    int64  boardlotCnt = 2;
}

// 撤销买单
message TradeForRevokeBuy {
    string buyID = 1;
}

// 数据库部分
message SellOrder {
    string tokenSymbol = 1;
    string address     = 2;
    //每一手出售的token的数量
    int64 amountPerBoardlot = 3;
    int64 minBoardlot       = 4;
    //每一手token的价格
    int64 pricePerBoardlot = 5;
    int64 totalBoardlot    = 6;
    int64 soldBoardlot     = 7;
    //此次出售的起始时间，如果非众筹则可以忽略此时间
    int64 starttime = 8;
    int64 stoptime  = 9;
    bool  crowdfund = 10;
    //此处使用tx的hash来指定
    string sellID    = 11;
    int32  status    = 12;
    int64  height    = 13;
    string assetExec = 14;
}

// 限价买单数据库记录
message BuyLimitOrder {
    string tokenSymbol       = 1;
    string address           = 2;
    int64  amountPerBoardlot = 3;
    int64  minBoardlot       = 4;
    int64  pricePerBoardlot  = 5;
    int64  totalBoardlot     = 6;
    int64  boughtBoardlot    = 7;
    string buyID             = 8;
    int32  status            = 9;
    int64  height            = 10;
    string assetExec         = 11;
}

// 执行器日志部分
message ReceiptBuyBase {
    string tokenSymbol       = 1;
    string owner             = 2;
    string amountPerBoardlot = 3;
    int64  minBoardlot       = 4;
    string pricePerBoardlot  = 5;
    int64  totalBoardlot     = 6;
    int64  boughtBoardlot    = 7;
    string buyID             = 8;
    string status            = 9;
    string sellID            = 10;
    string txHash            = 11;
    int64  height            = 12;
    string assetExec         = 13;
}

message ReceiptSellBase {
    string tokenSymbol = 1;
    string owner       = 2;
    //每一手出售的token的数量
    string amountPerBoardlot = 3;
    int64  minBoardlot       = 4;
    //每一手token的价格
    string pricePerBoardlot = 5;
    int64  totalBoardlot    = 6;
    int64  soldBoardlot     = 7;
    //此次出售的起始时间，如果非众筹则可以忽略此时间
    int64 starttime = 8;
    int64 stoptime  = 9;
    bool  crowdfund = 10;
    //此处使用tx的hash来指定
    string sellID = 11;
    string status = 12;
    // buyid
    string buyID     = 13;
    string txHash    = 14;
    int64  height    = 15;
    string assetExec = 16;
}

message ReceiptTradeBuyMarket {
    ReceiptBuyBase base = 1;
}

message ReceiptTradeBuyLimit {
    ReceiptBuyBase base = 1;
}

message ReceiptTradeBuyRevoke {
    ReceiptBuyBase base = 1;
}

message ReceiptTradeSellLimit {
    ReceiptSellBase base = 1;
}

message ReceiptSellMarket {
    ReceiptSellBase base = 1;
}

message ReceiptTradeSellRevoke {
    ReceiptSellBase base = 1;
}

// 查询部分

message ReqAddrAssets {
    string addr   = 1;
    int32  status = 2;
    // ??
    repeated string token     = 3;
    int32           direction = 4;
    int32           count     = 5;
    string          fromKey   = 6;
}

// 获取Token未完成卖单的交易列表
// 	 fromKey : 第一次传参为空，获取卖单单价最低的列表。 当要获得下一页时，
// 传当前页最后一个；当要获得上一页时， 传当前页第一个。 	 count
// :获取交易列表的个数。 	 direction :查找方式；0，上一页；1，下一页。
// 越靠后的也单价越贵
message ReqTokenSellOrder {
    string tokenSymbol = 1;
    string fromKey     = 2;
    int32  count       = 3;
    int32  direction   = 4;
    int32  status      = 5;
}

message ReqTokenBuyOrder {
    string tokenSymbol = 1;
    string fromKey     = 2;
    int32  count       = 3;
    int32  direction   = 4;
    int32  status      = 5;
}

message ReplyBuyOrder {
    string tokenSymbol       = 1;
    string owner             = 2;
    int64  amountPerBoardlot = 3;
    int64  minBoardlot       = 4;
    int64  pricePerBoardlot  = 5;
    int64  totalBoardlot     = 6;
    int64  boughtBoardlot    = 7;
    string buyID             = 8;
    int32  status            = 9;
    string sellID            = 10;
    string txHash            = 11;
    int64  height            = 12;
    string key               = 13;
    string assetExec         = 14;
}

message ReplySellOrder {
    string tokenSymbol       = 1;
    string owner             = 2;
    int64  amountPerBoardlot = 3;
    int64  minBoardlot       = 4;
    int64  pricePerBoardlot  = 5;
    int64  totalBoardlot     = 6;
    int64  soldBoardlot      = 7;
    string buyID             = 8;
    int32  status            = 9;
    string sellID            = 10;
    string txHash            = 11;
    int64  height            = 12;
    string key               = 13;
    string assetExec         = 14;
}

message ReplySellOrders {
    repeated ReplySellOrder sellOrders = 1;
}

message ReplyBuyOrders {
    repeated ReplyBuyOrder buyOrders = 1;
}

message ReplyTradeOrder {
    string tokenSymbol       = 1;
    string owner             = 2;
    int64  amountPerBoardlot = 3;
    int64  minBoardlot       = 4;
    int64  pricePerBoardlot  = 5;
    int64  totalBoardlot     = 6;
    int64  tradedBoardlot    = 7;
    string buyID             = 8;
    int32  status            = 9;
    string sellID            = 10;
    string txHash            = 11;
    int64  height            = 12;
    string key               = 13;
    int64  blockTime         = 14;
    bool   isSellOrder       = 15;
    string assetExec         = 16;
}

message ReplyTradeOrders {
    repeated ReplyTradeOrder orders = 1;
}

message ReqSellToken {
    TradeForSell sell  = 1;
    string       owner = 2;
}

message ReqRevokeSell {
    TradeForRevokeSell revoke = 1;
    string             owner  = 2;
}

message ReqBuyToken {
    TradeForBuy buy   = 1;
    string      buyer = 2;
}

service trade {
    rpc CreateRawTradeSellTx(TradeForSell) returns (UnsignTx) {}
    rpc CreateRawTradeBuyTx(TradeForBuy) returns (UnsignTx) {}
    rpc CreateRawTradeRevokeTx(TradeForRevokeSell) returns (UnsignTx) {}
    rpc CreateRawTradeBuyLimitTx(TradeForBuyLimit) returns (UnsignTx) {}
    rpc CreateRawTradeSellMarketTx(TradeForSellMarket) returns (UnsignTx) {}
    rpc CreateRawTradeRevokeBuyTx(TradeForRevokeBuy) returns (UnsignTx) {}
}