Skip to content

Commit

Permalink
rpcserver: add matches slice to myorders route reply
Browse files Browse the repository at this point in the history
This adds new []match to the myOrder struct returned by the rpcserver's myorders route..
  • Loading branch information
amass01 committed Jan 11, 2021
1 parent 8e3d3b4 commit 3bef6ba
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 29 deletions.
2 changes: 1 addition & 1 deletion client/core/types.go
Expand Up @@ -139,7 +139,7 @@ type Match struct {
IsCancel bool `json:"isCancel"`
}

// A coin encodes both the coin ID and the asset-dependent string representation
// Coin encodes both the coin ID and the asset-dependent string representation
// of the coin ID.
type Coin struct {
ID dex.Bytes `json:"id"`
Expand Down
70 changes: 60 additions & 10 deletions client/rpcserver/handlers.go
Expand Up @@ -448,15 +448,43 @@ func handleOrderBook(s *RPCServer, params *RawParams) *msgjson.ResponsePayload {

// parseCoreOrder converts a *core.Order into a *myOrder.
func parseCoreOrder(co *core.Order, b, q uint32) *myOrder {
// settled calculates how much of the order has been finalized.
settled := func(qty uint64, matches []*core.Match) (settled uint64) {
for _, match := range matches {
if (match.Side == order.Maker && match.Status >= order.MakerRedeemed) ||
(match.Side == order.Taker && match.Status >= order.MatchComplete) {
settled += match.Qty
// matchesParser parses core.Match slice & calculates how much of the order
// has been settled/finalized.
parseMatches := func(matches []*core.Match) (ms []*match, settled uint64) {
ms = make([]*match, 0, len(matches))
// coinSafeString gets the Coin's StringID safely.
coinSafeString := func(c *core.Coin) string {
if c == nil {
return ""
}
return c.StringID
}
return settled
for _, m := range matches {
// Sum up settled value.
if (m.Side == order.Maker && m.Status >= order.MakerRedeemed) ||
(m.Side == order.Taker && m.Status >= order.MatchComplete) {
settled += m.Qty
}
match := &match{
MatchID: m.MatchID.String(),
Status: m.Status.String(),
Revoked: m.Revoked,
Rate: m.Rate,
Qty: m.Qty,
Side: m.Side.String(),
FeeRate: m.FeeRate,
Stamp: m.Stamp,
IsCancel: m.IsCancel,
}

match.Swap = coinSafeString(m.Swap)
match.CounterSwap = coinSafeString(m.CounterSwap)
match.Redeem = coinSafeString(m.Redeem)
match.CounterRedeem = coinSafeString(m.CounterRedeem)
match.Refund = coinSafeString(m.Refund)
ms = append(ms, match)
}
return ms, settled
}
srvTime := encode.UnixTimeMilli(int64(co.Stamp))
age := time.Since(srvTime).Round(time.Millisecond)
Expand All @@ -465,7 +493,7 @@ func parseCoreOrder(co *core.Order, b, q uint32) *myOrder {
if co.Status >= order.OrderStatusExecuted {
cancelling = false
}
return &myOrder{
o := &myOrder{
Host: co.Host,
MarketName: co.MarketID,
BaseID: b,
Expand All @@ -478,12 +506,16 @@ func parseCoreOrder(co *core.Order, b, q uint32) *myOrder {
Rate: co.Rate,
Quantity: co.Qty,
Filled: co.Filled,
Settled: settled(co.Qty, co.Matches),
Status: co.Status.String(),
Cancelling: cancelling,
Canceled: co.Canceled,
TimeInForce: co.TimeInForce.String(),
}

// Parese matches & calculate settled value
o.Matches, o.Settled = parseMatches(co.Matches)

return o
}

// handleMyOrders handles requests for myorders. *msgjson.ResponsePayload.Error
Expand Down Expand Up @@ -794,7 +826,6 @@ Registration is complete after the fee transaction has been confirmed.`,
quote (int): The BIP-44 coin index for the market's quote asset.
qty (int): The number of units to buy/sell. Must be a multiple of the lot size.
rate (int): The atoms quote asset to pay/accept per unit base asset. e.g.
156000 satoshi/DCR for the DCR(base)_BTC(quote).
immediate (bool): Require immediate match. Do not book the order.`,
returns: `Returns:
obj: The order details.
Expand Down Expand Up @@ -914,6 +945,25 @@ Registration is complete after the fee transaction has been confirmed.`,
"canceled" (bool): Whether this order has been canceled.
"tif" (string): "immediate" if this limit order will only match for one epoch.
"standing" if the order can continue matching until filled or cancelled.
"matches": (array): An array of matches associated with the order.
[
{
"matchID (string): The match's ID."
"status" (string): The match's status."
"revoked" (bool): Indicates if match was revoked.
"rate" (int): The match's rate.
"qty" (int): The match's amount.
"side" (string): The match's side, "maker" or "taker".
"feerate" (int): The match's fee rate.
"swap" (string): The match's swap transaction.
"counterSwap" (string): The match's counter swap transaction.
"redeem" (string): The match's redeem transaction.
"counterRedeem" (string): The match's counter redeem transaction.
"refund" (string): The match's refund transaction.
"stamp" (int): The match's stamp.
"isCancel" (bool): Indicates if match is canceled.
},...
]
},...
]`,
},
Expand Down
36 changes: 35 additions & 1 deletion client/rpcserver/handlers_test.go
Expand Up @@ -992,7 +992,41 @@ func TestParseCoreOrder(t *testing.T) {
"filled": 300000000,
"settled": 100000000,
"status": "booked",
"tif": "standing"
"tif": "standing",
"matches": [
{
"matchID": "992f15e89bbd670663b690b4da4a859609d83866e200f3c4cd5c916442b8ea46",
"status": "MatchComplete",
"revoked": false,
"rate": 200000000,
"qty": 100000000,
"side": "Maker",
"feeRate": 0,
"swap": "",
"counterSwap":"",
"redeem": "",
"counterRedeem": "",
"refund": "",
"stamp": 0,
"isCancel": false
},
{
"matchID": "69d7453d8ad3b52851c2c9925499a1b158301e8a08da594428ef0ad4cd6fd3a5",
"status": "MakerSwapCast",
"revoked": false,
"rate": 200000000,
"qty": 200000000,
"side": "Maker",
"feeRate": 0,
"swap": "",
"counterSwap": "",
"redeem": "",
"counterRedeem": "",
"refund": "",
"tamp": 0,
"isCancel": false
}
]
}`
coreOrder := new(core.Order)
if err := json.Unmarshal([]byte(co), coreOrder); err != nil {
Expand Down
54 changes: 37 additions & 17 deletions client/rpcserver/types.go
Expand Up @@ -58,24 +58,44 @@ type tradeResponse struct {
// myOrdersResponse is used when responding to the myorders route.
type myOrdersResponse []*myOrder

// myOrder represents an order when responding to the myorders route.
type myOrder struct {
Host string `json:"host"`
MarketName string `json:"marketName"`
BaseID uint32 `json:"baseID"`
QuoteID uint32 `json:"quoteID"`
ID string `json:"id"`
Type string `json:"type"`
Sell bool `json:"sell"`
Stamp uint64 `json:"stamp"`
Age string `json:"age"`
Rate uint64 `json:"rate,omitempty"`
Quantity uint64 `json:"quantity"`
Filled uint64 `json:"filled"`
Settled uint64 `json:"settled"`
Status string `json:"status"`
Cancelling bool `json:"cancelling,omitempty"`
Canceled bool `json:"canceled,omitempty"`
TimeInForce string `json:"tif,omitempty"`
Host string `json:"host"`
MarketName string `json:"marketName"`
BaseID uint32 `json:"baseID"`
QuoteID uint32 `json:"quoteID"`
ID string `json:"id"`
Type string `json:"type"`
Sell bool `json:"sell"`
Stamp uint64 `json:"stamp"`
Age string `json:"age"`
Rate uint64 `json:"rate,omitempty"`
Quantity uint64 `json:"quantity"`
Filled uint64 `json:"filled"`
Settled uint64 `json:"settled"`
Status string `json:"status"`
Cancelling bool `json:"cancelling,omitempty"`
Canceled bool `json:"canceled,omitempty"`
TimeInForce string `json:"tif,omitempty"`
Matches []*match `json:"matches,omitempty"`
}

// match represents a match on an order. An order may have many matches.
type match struct {
MatchID string `json:"matchID"`
Status string `json:"status"`
Revoked bool `json:"revoked"`
Rate uint64 `json:"rate"`
Qty uint64 `json:"qty"`
Side string `json:"side"`
FeeRate uint64 `json:"feeRate"`
Swap string `json:"swap,omitempty"`
CounterSwap string `json:"counterSwap,omitempty"`
Redeem string `json:"redeem,omitempty"`
CounterRedeem string `json:"counterRedeem,omitempty"`
Refund string `json:"refund,omitempty"`
Stamp uint64 `json:"stamp"`
IsCancel bool `json:"isCancel"`
}

// openWalletForm is information necessary to open a wallet.
Expand Down

0 comments on commit 3bef6ba

Please sign in to comment.