decred.org/dcrdex@v1.0.3/spec/orders.mediawiki (about)

     1  =Client Order Management=
     2  
     3  __TOC__
     4  
     5  This section describes the steps required of the client to place an order, and
     6  the interactions between the client and server to execute an order once a match
     7  has been found.
     8  See the [[atomic.mediawiki/#Atomic_Settlement|atomic settlement]] section for a high-level
     9  overview of the settlement process.
    10  
    11  There are three available types of order.
    12  
    13  * [[#limit-order|Limit orders]] are used to buy or sell a specified amount of an asset at a rate no higher (buy) or lower (sell) than a specified price.
    14  * [[#market-order|Market orders]] are used to buy or sell a specified amount of an asset at the best price available.
    15  * [[#cancel-order|Cancel orders]] are used to remove standing limit orders from the order book.
    16  
    17  The order book holds only limit orders with time in force ''standing'' that have
    18  not completely filled or been canceled. All other orders are only valid for one
    19  [[fundamentals.mediawiki/#epoch-based-order-matching|epoch match cycle]].
    20  
    21  ==Connection Persistence==
    22  
    23  Regardless of connection status, if a client does not respond to their
    24  <code>preimage</code> request, they are in violation of
    25  [[community.mediawiki/#rule-1-clients-must-respond-to-all-preimage-requests|rule 1]] and subject to
    26  penalty.
    27  
    28  If a client's connection is lost during match negotiation, the client is
    29  expected to reconnect and complete settlement.
    30  Once a match is made, a client is always subject to violation of
    31  [[community.mediawiki/#rule-2-every-match-must-be-fully-settled|rule 2]] via the
    32  [[fundamentals.mediawiki/#global-variables|broadcast timeout]].
    33  
    34  ==Order Book Subscriptions==
    35  
    36  An order book can be viewed and tracked by subscribing to a market.
    37  
    38  '''Request route:''' <code>orderbook</code>, '''originator: ''' client
    39  
    40  <code>payload</code>
    41  {|
    42  ! field !! type   !! description
    43  |-
    44  | base  || int || the base asset ID
    45  |-
    46  | quote || int || the quote asset ID
    47  |}
    48  
    49  The response will contain the complete market order book.
    50  The order book and all updates include a '''sequence ID''', which increments by
    51  +1 whenever the DEX accepts, removes, or modifies an order.
    52  The client is responsible for tracking the sequence ID to ensure all order
    53  updates are received. If an update appears to be missing, the client should
    54  re-subscribe to the market to synchronize the order book from scratch.
    55  
    56  '''Response'''
    57  
    58  <code>payload</code>
    59  {|
    60  ! field    !! type !! description
    61  |-
    62  | marketid || string || A unique market identifier, the market ID.
    63  |-
    64  | seq      || int    || A sequence ID
    65  |-
    66  | epoch    || int  || the current epoch
    67  |-
    68  | orders   || &#91;object&#93; || A list of '''Order''' objects (described below)
    69  |}
    70  
    71  '''JSON Order object'''
    72  
    73  {|
    74  ! field   !! type   !! description !! notes
    75  |-
    76  | seq      || int   || A sequence ID || superceded in <code>orderbook</code> response
    77  |-
    78  | marketid || string || the market ID ||
    79  |-
    80  | oid     || string || the order ID ||
    81  |-
    82  | side    || string || "b" for ''buy'', "s" for ''sell'' || epoch_order, book_order
    83  |-
    84  | qty     || int      || order size (atoms) || epoch_order, book_order
    85  |-
    86  | rate    || int    || price rate. [[comm.mediawiki/#rate-encoding|message-rate encoding]] || only set on limit orders
    87  |-
    88  | tif     || string || time in force. one of "i" for ''immediate'' or "s" for ''standing'' || only set on limit orders
    89  |-
    90  | time    || int    || the order's UNIX timestamp || epoch_order, book_order
    91  |-
    92  | com     || string || the order commitment || epoch_order only
    93  |-
    94  | otype   || string || "l" for ''limit'', "m" for ''market'', "c" for ''cancel'' || epoch_order only
    95  |-
    96  | epoch   || int    || the order's epoch index || epoch_order only
    97  |-
    98  | target  || string || target order ID || only set on cancel orders (epoch_order only)
    99  |}
   100  
   101  '''Changes to the order book''' will be received from the DEX as a stream of
   102  notifications. The action to be taken depends on the route according to the
   103  following table. The payload for all three routes is an '''Order''' object.
   104  {|
   105  ! route        !! action
   106  |-
   107  | book_order   || add (or update) the order to the order book
   108  |-
   109  | epoch_order  || add the order to the epoch queue
   110  |-
   111  | unbook_order || remove the order from the order book
   112  |}
   113  
   114  Additionally, there is a notification for updating the remaining quantity of an
   115  order. This notification is sent when a booked order partially fills and remains
   116  on the book with a reduced matchable quantity.
   117  
   118  '''Notification route:''' <code>update_remaining</code>, '''originator: ''' DEX
   119  
   120  <code>payload</code>
   121  {|
   122  ! field   !! type   !! description
   123  |-
   124  | seq       || int   || A sequence ID
   125  |-
   126  | marketid  || string || The market identifier
   127  |-
   128  | oid       || string || The order ID
   129  |-
   130  | remaining || int    || remaining quantity (atoms)
   131  |}
   132  
   133  At the beginning of the matching cycle, the DEX will publish a list of order
   134  preimages, the seed hash used for
   135  [[fundamentals.mediawiki/#pseudorandom-order-matching|order sequencing]], and the
   136  [[#preimage-reveal|commitment checksum]], which together can be used to
   137  independently verify matching.
   138  
   139  '''Notification route:''' <code>match_proof</code>, '''originator: ''' DEX
   140  
   141  <code>payload</code>
   142  {|
   143  ! field     !! type !! description
   144  |-
   145  | marketid  || string || the market ID
   146  |-
   147  | epoch     || int || the epoch index for which the cycle occurs
   148  |-
   149  | preimages || &#91;string&#93; || list of order preimages for the epoch
   150  |-
   151  | misses    || &#91;string&#93; || list of order IDs for which preimages were not received, so were not included in sorting or matching
   152  |-
   153  | csum      || string || the commitment checksum
   154  |-
   155  | seed      || string || epoch queue shuffling seed
   156  |}
   157  
   158  A client can '''unsubscribe''' from order book updates without closing the
   159  WebSocket connection.
   160  
   161  '''Request route:''' <code>unsub_orderbook</code>, '''originator: ''' client
   162  
   163  <code>payload</code>
   164  {|
   165  ! field  !! type !! description
   166  |-
   167  | marketid || string || the market ID
   168  |}
   169  
   170  <code>result</code>: boolean <code>true</code> on success.
   171  
   172  ==Order Preparation==
   173  
   174  As part of the order, the client must demonstrate control of funds.
   175  This is accomplished by supplying information and a signature for each
   176  [[comm.mediawiki/#coin-id|coin]] that will be spent.
   177  The client covers the ''backing fees'' associated with the inputs spending their
   178  own coins.
   179  
   180  In addition, the client must show the ability to cover ''base fees'' for any
   181  initialization transactions that will be generated. The client must show that
   182  they have the funds to cover all fees for the worst-case scenario, which is
   183  single-lot matches for the entire order.
   184  In practice, large orders will rarely pay the total of the base fees because
   185  many of the matches will be more than a single-lot.
   186  
   187  ===Calculating Transaction Fees===
   188  
   189  The '''base fees''' cover transaction fees associated with making
   190  initialization transactions for every match in the order.
   191  
   192  For asset '''Z''', a ''base fee ratio'', '''''R<sub>z</sub>''''' is calculated
   193  based on the ''lot size'', '''''l''''' (units ''Z''), a ''fee rate'',
   194  '''''r''''' (''Z/byte''), and a ''transaction size'', '''''s''''' (''bytes'').
   195  '''''s''''' is pre-calculated based on a standard initialization transaction.
   196  
   197  <!--R_z = \frac{ s r }{ l }-->
   198  [[File:images/fee_max_ratio.png]]
   199  
   200  The ''base fee ratio'' is a constant until the DEX operator changes one of its
   201  factors.
   202  
   203  The ''base fees'', '''''f<sub>base</sub>''''' (units ''Z'') can be calculated
   204  from '''''R<sub>z</sub>''''' and the ''order quantity'', '''''Q'''''.
   205  
   206  <!--f_{base} = Q R_z-->
   207  [[File:images/base_fees.png]]
   208  
   209  The base fees scale linearly with order size, but the actual realized portion of
   210  the base fees, '''''f<sub>fin</sub>''''', can only be known to fall within a
   211  range '''''r s &#8804; f<sub>fin</sub> &#8804; f<sub>base</sub> '''''.
   212  
   213  The client also covers the '''backing fees''' associated with spending their
   214  backing coins, '''''f<sub>coin</sub>'''''.
   215  The client must know how to calculate the script sizes to assess fees.
   216  The DEX will verify the coin sum before accepting the order.
   217  
   218  ===Coin Preparation===
   219  
   220  With the exception of market buy orders, which are detailed below, for an order
   221  of quantity '''''Q''''', the sum value of the selected coins, '''''V''''',
   222  must satisfy the relation
   223  (with [[#calculating-transaction-fees|fees]])
   224  
   225  <!--V \ge Q + f_{base} + f_{coin}-->
   226  [[File:images/coin-sum.png]]
   227  
   228  There may be types of coins which are not supported by the asset's DEX
   229  implementation. Asset developers should make coin-spending limitations clear
   230  to wallet users.
   231  
   232  As part of the order, the client will submit a list of ''Coin objects''.
   233  
   234  '''JSON Coin object'''
   235  
   236  {|
   237  ! field     !! type   !! description
   238  |-
   239  | coinid    || string || hex-encoded coin ID
   240  |-
   241  | pubkeys   || &#91;string&#93; || array of hex-encoded pubkeys which spend the coin
   242  |-
   243  | sigs      || &#91;string&#93; || array of signatures of Blake-256 hashes of the serialized coin IDs
   244  |-
   245  | redeem    || string || hex-encoded redeem script for P2SH. empty for P2PKH
   246  |}
   247  
   248  In order to enable multi-signature support, more than one pubkey can be
   249  submitted. If more than one pubkey is submitted, there should be a signature
   250  for each one.
   251  The data is signed with the private key(s) corresponding to the
   252  <code>pubkeys</code>.
   253  The <code>pubkeys</code> themselves must correspond with addresses payable by
   254  the coin's pubkey script (or corresponding redeem script).
   255  
   256  ===Order Commitment===
   257  
   258  As part of every submitted order, the client should submit a cryptographic
   259  '''commitment'''.
   260  To generate a commitment, the client creates a random 32-byte sequence,
   261  the ''preimage''. The commitment is the Blake-256 hash of the
   262  preimage. Every order must be assigned a unique commitment, therefore preimages
   263  cannot be reused. They should be generated with a cryptographically-secure
   264  pseudo-random number generator.
   265  
   266  The server will reject any zero-valued commitment as well as the specific
   267  commitment generated from hashing a zero-valued preimage. The server can also
   268  reject a commitment that has already been used for any previous order. Depending
   269  on server policy, the historical commitment duplicate search may be limited to
   270  only recent commitments and/or commitments received for orders on a specific
   271  market. Assuming that a cryptographically-secure PRNG is used to generate
   272  preimages, rejection should realistically be impossible.
   273  
   274  At the expiration of the epoch, the server sends a <code>preimage</code> request
   275  for each order that is eligible for matching.
   276  The client responds with their preimage(s). If the client fails to respond to
   277  their <code>preimage</code> requests, or if their <code>preimage</code> response
   278  does not hash to their order commitment, the order is not matched and the client
   279  is considered in violation of
   280  [[community.mediawiki/#rule-1-clients-must-respond-to-all-preimage-requests|rule 1]].
   281  
   282  The preimages are used as the inputs to
   283  [[fundamentals.mediawiki/#pseudorandom-order-matching|the shuffling algorithm]] to determine
   284  matching order. Before matching commences, the preimages are broadcast
   285  in the <code>match_proof</code> message.
   286  
   287  ===Order Signing===
   288  
   289  All orders must be signed by the client and the server.
   290  The basic signing procedure will involve serializing order data into a byte array
   291  following a specific procedure that can be replicated on the server.
   292  The serialized data is then signed using the client's
   293  [[fundamentals.mediawiki/#identities-based-on-public-key-infrastructure-pki-key-pairs|private account key]].
   294  
   295  All integer encoding for all serialized structures is big endian.
   296  
   297  All order serializations have common '''prefix''' fields.
   298  
   299  '''Prefix fields and serialization'''
   300  {|
   301  ! field      !! size (bytes) !! JSON type !! description
   302  |-
   303  | accountid  || 32 || string || hex-encoded client account ID
   304  |-
   305  | base       || 4  || int || the base asset ID
   306  |-
   307  | quote      || 4  || int || the quote asset ID
   308  |-
   309  | ordertype  || 1  || int || the type of order. limit = 1, market=2, cancel=3
   310  |-
   311  | tclient    || 8  || int || the client's UNIX timestamp (milliseconds)
   312  |-
   313  | tserver    || 8  || int || the server's UNIX timestamp (milliseconds). zero for client signature
   314  |-
   315  | com || 32 || string || hex-encoded cryptographic commitment
   316  |}
   317  
   318  ===Order ID===
   319  
   320  The order serialization is used to create a unique order ID.
   321  The ID is defined as the Blake-256 hash of the serialized order, including the
   322  non-zero server's timestamp. The client does not know the order ID when
   323  submitting, but should independently verify the ID after parsing the server's
   324  response.
   325  
   326  Because the order ID includes the server's timestamp, the order ID itself
   327  provides a checksum to ensure that order information is properly transmitted.
   328  The response to all order submissions is an '''order receipt''', which includes
   329  the timestamp.
   330  
   331  '''Order receipt'''
   332  
   333  <code>result</code>
   334  {|
   335  ! field     !! type   !! description
   336  |-
   337  | sig       || string || server hex-encoded signature of the serialized order, after adding the DEX timestamp
   338  |-
   339  | orderid   || string || the order ID
   340  |-
   341  | tserver   || int  || the server's UNIX timestamp (milliseconds)
   342  |}
   343  
   344  The client should use the server's timestamp to create a serialized order and
   345  independently verify the order ID. The serialized order is also the message for
   346  the server's signature.
   347  
   348  ==Order Types==
   349  
   350  ===Limit Order===
   351  
   352  Limit orders are for the trade of assets at a rate no higher (buy) or lower
   353  (sell) than a specified price.
   354  The client may specify the ''time in force'' of a limit order as one of: (a)
   355  ''standing'', which remains on the books until filled or canceled, or (b)
   356  ''immediate'', which can complete execution wholly or partially unfilled. As
   357  such, the ''immediate'' option is intended for limit orders with a price that
   358  crosses the spread (i.e. a taker rather than a maker). The
   359  <code>ordersize</code> must be an integer multiple of the asset's
   360  [[fundamentals.mediawiki/#global-variabless|lot size]].
   361  
   362  '''Request route:''' <code>limit</code>, '''originator:''' client
   363  
   364  <code>payload</code>
   365  {|
   366  ! field       !! type   !! description
   367  |-
   368  | colspan="3" align="center" | 9 prefix fields
   369  |-
   370  | side        || int || buy = 1, sell = 2
   371  |-
   372  | ordersize   || int || order size (atoms)
   373  |-
   374  | rate        || int || price rate. [[comm.mediawiki/#rate-encoding|message-rate encoding]]
   375  |-
   376  | timeinforce || int || standing = 1, immediate = 2
   377  |-
   378  | coins       ||  &#91;[[#Coin_Preparation|Coin]]&#93; || array of funding coins
   379  |-
   380  | address     || string || address where the matched client will send funds
   381  |-
   382  | sig         || string || client hex-encoded signature of the serialized order, with tserver = 0
   383  |}
   384  
   385  '''Limit order serialization'''
   386  
   387  {|
   388  ! field      !! size (bytes)  !! description
   389  |-
   390  | prefix     || 99 || [[#order-signing|the order prefix]]
   391  |-
   392  | coin count || 1  || The number of funding coins
   393  |-
   394  | coin data  || coin length x count || [[#coin-preparation|sequence of coin IDs]]
   395  |-
   396  | side       || 1 || 1 for buy, 2 for sell
   397  |-
   398  | quantity   || 8 || quantity to buy or sell (atoms)
   399  |-
   400  | rate       || 8 || price rate. [[comm.mediawiki/#rate-encoding|message-rate encoding]]
   401  |-
   402  | time in force || 1 || 1 for ''standing'', 2 for ''immediate''
   403  |-
   404  | address    || varies || client's receiving address
   405  |}
   406  
   407  <code>result</code>
   408  {|
   409  ! field     !! type   !! description
   410  |-
   411  | sig       || string || server hex-encoded signature of the serialized order, after adding the DEX timestamp
   412  |-
   413  | server time || int  || the server's UNIX timestamp (milliseconds)
   414  |}
   415  
   416  ===Market Order===
   417  
   418  A market order is an order to buy or sell an asset at the best available
   419  market price. The request payload fields are similar to a limit order, but
   420  without the <code>rate</code> field or <code>timeinforce</code> fields.
   421  
   422  Market orders cannot be canceled.
   423  Any portion of the requested quantity that does not match immediately (during
   424  the epoch match cycle) is left unfilled.
   425  
   426  '''Request route:''' <code>market</code>, '''originator: ''' client
   427  
   428  <code>payload</code>
   429  {|
   430  ! field       !! type   !! description
   431  |-
   432  | colspan="3" align="center" | 9 prefix fields
   433  |-
   434  | side        || int || buy = 1, sell = 2
   435  |-
   436  | ordersize   || int || order size (atoms)
   437  |-
   438  | coins       ||  &#91;[[#coin-preparation|Coin]]&#93; || array of funding coins
   439  |-
   440  | address     || string || address where the matched client will send funds
   441  |-
   442  | sig         || string || client hex-encoded signature of the serialized order, with tserver = 0
   443  |}
   444  
   445  '''Market order serialization'''
   446  
   447  {|
   448  ! field      !! size (bytes)  !! description
   449  |-
   450  | prefix     || 99 || [[#order-signing|the order prefix]]
   451  |-
   452  | coin count || 1  || The number of funding coins
   453  |-
   454  | coin data  || coin length x count || [[#coin-preparation|sequence of coin IDs]]
   455  |-
   456  | side       || 1 || 1 for buy, 2 for sell
   457  |-
   458  | quantity   || 8 || quantity to buy or sell (atoms)
   459  |-
   460  | address    || varies || client's receiving address
   461  |}
   462  
   463  <code>result</code>
   464  {|
   465  ! field     !! type   !! description
   466  |-
   467  | sig       || string || server hex-encoded signature of the order by server, after adding the DEX timestamp
   468  |-
   469  | server time || int  || the server's UNIX timestamp (milliseconds)
   470  |}
   471  
   472  ====Market Buy Orders====
   473  
   474  Market buy orders have a slightly different ruleset than market sell orders or
   475  limit orders.
   476  First, the <code>ordersize</code> is not denominated in the base asset, but in
   477  the quote asset.
   478  As an example, on the DCR/BTC market, where DCR is the base asset, market sell
   479  orders and both types of limit orders' <code>ordersize</code> are quantified in
   480  the base asset, DCR, but the market buy order's <code>ordersize</code> is in BTC.
   481  The order is essentially a statement of "buy as much DCR as you can with this
   482  much BTC".
   483  
   484  The <code>ordersize</code> is also not bound to the integral lot size
   485  constraints of other types of orders.
   486  
   487  Since the market may move before the order is matched, at the time of submission
   488  it is not known with certainty how many lots will match.
   489  For orders that are nearly 1 lot, it is possible for no matching to occur
   490  because by the time the order is matched it cannot afford a single lot.
   491  The DEX server maintains an interest in ensuring that only valid orders that
   492  can match are accepted, so market buy orders must be handled carefully to make
   493  sure they remain valid.
   494  
   495  To reduce the possibility of market buy orders becoming invalid (too small to
   496  match) due to a price increase, the DEX operator sets a ''market buy buffer'',
   497  '''''b<sub>m</sub> > 1''''' for each market.
   498  For a market where the base asset has lot size '''''l''''', and for which there
   499  is a best known standing sell order price rate, '''''r''''', the
   500  <code>ordersize</code>, '''''Q''''', must satisfy the relation
   501  '''''Q > b<sub>m</sub> l r'''''.
   502  If the best rate increases before the order is matched, the order will still
   503  result in a fill as long as the price does not surpass
   504  ~'''''b<sub>m</sub> r'''''.
   505  If the ''market buy buffer'' is set too low or the market is particularly
   506  volatile and the price exceeds '''''b<sub>m</sub> r''''', an order that was
   507  accepted but is now too small to match is considered executed but unfilled and
   508  there is no change to the account's
   509  [[community.mediawiki/#rules-of-community-conduct|cancellation statistics]].
   510  
   511  ===Cancel Order===
   512  
   513  Cancel orders remove standing limit orders from the order book.
   514  A client cannot cancel a market order or a limit order with time in force
   515  ''immediate''. Further, due to the epoch-based pseudorandom matching process, a
   516  cancel order submitted in the same epoch as it's corresponding limit order has
   517  a 50% chance of being processed before the order it cancels, resulting in an
   518  error.
   519  This is by design and discourages certain types of spoofing.
   520  
   521  '''Request route:''' <code>cancel</code>, '''originator:''' client
   522  
   523  <code>payload</code>
   524  {|
   525  ! field     !! type   !! description
   526  |-
   527  | colspan="3" align="center" | 9 prefix fields
   528  |-
   529  | targetid  || string || hex-encoded order ID
   530  |-
   531  | sig       || string || client hex-encoded signature of the serialized order data. serialization described below
   532  |}
   533  
   534  '''Cancel order serialization'''
   535  
   536  {|
   537  ! field      !! size (bytes)  !! description
   538  |-
   539  | prefix     || 99 || [[#order-signing|the order prefix]]
   540  |-
   541  | orderid    || 32 || the order ID
   542  |}
   543  
   544  <code>result</code>
   545  {|
   546  ! field   !! type   !! description
   547  |-
   548  | sig     || string || server hex-encoded signature of the serialize order data, after adding the DEX timestamp
   549  |-
   550  | tserver || int    || the server's UNIX timestamp (milliseconds)
   551  |}
   552  
   553  ==Preimage Reveal==
   554  
   555  At the expiration of the epoch, the DEX sends out a <code>preimage</code>
   556  request for each order in the epoch queue. The match cycle begins 5 seconds
   557  after the last <code>preimage</code> request is sent by the server, so clients
   558  must respond before then.
   559  
   560  A '''''commitment checksum''''' is included as part of the
   561  <code>preimage</code> request.
   562  The checksum is the Blake-256 hash of the concatenated, lexicographically-sorted
   563  commitments for every order in the epoch. For clients subscribed to the order
   564  book for the entire duration of the epoch, the checksum can be validated against
   565  the checksum generated from their local copy of the epoch queue.
   566  
   567  '''Request route:''' <code>preimage</code>, '''originator:''' DEX
   568  
   569  <code>payload</code>
   570  {|
   571  ! field   !! type   !! description
   572  |-
   573  | orderid || string || order ID
   574  |-
   575  | csum    || string || the commitment checksum
   576  |}
   577  
   578  '''Preimage response'''
   579  
   580  <code>result</code>
   581  {|
   582  ! field   !! size (bytes)  !! description
   583  |-
   584  | pimg    || string || hex-encoded preimage for the order's commitment
   585  |}
   586  
   587  ==Match negotiation==
   588  
   589  Swap negotiation details will be relayed through the DEX with a series of
   590  notifications.
   591  Both the DEX and the clients will need to serialize and sign the notification
   592  data. The originator includes their signature with the request, while the
   593  recipient will return an '''acknowledgement''', or a list of
   594  acknowledgements, as the <code>result</code> of their response payload.
   595  
   596  '''Acknowledgement'''
   597  
   598  {|
   599  ! field     !! type   !! description
   600  |-
   601  | matchid   ||  string  || the match ID
   602  |-
   603  | sig       ||  string || hex-encoded signature of the notification data
   604  |}
   605  
   606  If the client's order has one or more matches at the end of a match cycle, the
   607  DEX will send a list of '''match objects'''. The maker is the first to act, so
   608  after sending their acknowledgement, they should broadcast their initialization
   609  transaction and inform the server with an <code>init</code> notification
   610  (described after).
   611  
   612  '''Request route:''' <code>match</code>, '''originator:''' DEX
   613  
   614  <code>payload</code> (array)
   615  {|
   616  ! field     !! type   !! description
   617  |-
   618  | orderid   || string || order ID
   619  |-
   620  | matchid   || string    || the match ID to use for progress notifications
   621  |-
   622  | qty       || int    || the matched amount, in atoms of the base asset
   623  |-
   624  | rate      || int    || the matched price rate. [[comm.mediawiki/#rate-encoding|message-rate encoding]]
   625  |-
   626  | tserver   || int    || server's UNIX timestamp (milliseconds)
   627  |-
   628  | address   || string || the counterparty's receiving address
   629  |-
   630  | side      || int    || the client's side in the match. 0 = maker, 1 = taker
   631  |-
   632  | status    || int    || only provided in 'connect' response. For 'match' requests, status is 0 = 'MakerSwapCast'. See [[https://github.com/decred/dcrdex/blob/master/dex/order/match.go|match.go]] for codes.
   633  |-
   634  | sig       || string || DEX's hex-encoded signature of the serialized notification data. serialization described below
   635  |}
   636  
   637  '''Match serialization'''
   638  
   639  {|
   640  ! field      !! size (bytes)  !! description
   641  |-
   642  | orderid    || 32 || the order ID
   643  |-
   644  | matchid    || 32  || the ID assigned to this match
   645  |-
   646  | quantity   || 8  || the matched amount, in atoms of the base asset
   647  |-
   648  | rate       || 8  || the matched price rate. [[comm.mediawiki/#rate-encoding|message-rate encoding]]
   649  |-
   650  | tserver  || 8  || server's UNIX timestamp (milliseconds)
   651  |-
   652  | address    || varies || UTF-8 encoded receiving address for the match
   653  |}
   654  
   655  '''The <code>tserver</code> value is used as the basis for the the locktimes.'''
   656  If it is necessary to convert the time to seconds, the value should be rounded
   657  down.
   658  
   659  The client will respond with a list of signed match acknowledgements.
   660  
   661  After a client broadcasts their initialization transaction, they are
   662  expected to report the transaction details to the server for verification and
   663  relay to the matching party.
   664  
   665  '''Request route:''' <code>init</code>, '''originator:''' client
   666  
   667  <code>payload</code>
   668  {|
   669  ! field      !! type   !! description
   670  |-
   671  | orderid    || string || the order ID
   672  |-
   673  | matchid    || string    || the matchid, retrieved from the [[#match-negotiation|match notification]]
   674  |-
   675  | coinid    || string || hex-encoded coin ID
   676  |-
   677  | contract   || string || hex-encoded swap redeem script
   678  |-
   679  | sig        || string || client signature of the serialized notification. serialization described below
   680  |}
   681  
   682  '''Init serialization'''
   683  
   684  {|
   685  ! field      !! size (bytes)  !! description
   686  |-
   687  | orderid    || 32 || the order ID
   688  |-
   689  | matchid    || 32  || the ID assigned to this match
   690  |-
   691  | coinid     || asset-dependent  || the coin ID
   692  |-
   693  | contract   || asset-dependent || swap redeem script
   694  |}
   695  
   696  The DEX will respond with an acknowledgement.
   697  
   698  The DEX will send each client a notification when the counterparty has broadcast
   699  their initialization transaction.
   700  When the taker receives the <code>audit</code> notification, they will audit the
   701  maker's contract and after that contract asset's configured swap confirmation
   702  requirement is reached, broadcast their own initialization.
   703  When the maker receives the <code>audit</code> notification, they will audit the
   704  taker's contract and after the required confirmations, issue their redemption.
   705  
   706  '''Request route:''' <code>audit</code>, '''originator:''' DEX
   707  
   708  <code>payload</code>
   709  {|
   710  ! field     !! type   !! description
   711  |-
   712  | orderid   || string || the order ID
   713  |-
   714  | matchid   || string || the match ID
   715  |-
   716  | timestamp || int  || server's UNIX timestamp (milliseconds)
   717  |-
   718  | coinid    || string || hex-encoded coin ID
   719  |-
   720  | contract  || string || hex-encoded swap redeem script
   721  |-
   722  | sig       || string || DEX's signature of the serialized notification. serialization described below
   723  |}
   724  
   725  '''Audit serialization'''
   726  
   727  {|
   728  ! field      !! size (bytes)  !! description
   729  |-
   730  | orderid    || 32 || the order ID
   731  |-
   732  | matchid    || 32  || the match ID
   733  |-
   734  | timestamp  || 8  || server's UNIX timestamp (milliseconds)
   735  |-
   736  | coin ID    || asset-dependent  || the coin ID
   737  |-
   738  | contract   || asset-dependent || swap redeem script
   739  |}
   740  
   741  The client responds with an acknowledgement.
   742  
   743  When a client has redeemed their contract, they will notify the server.
   744  
   745  '''Request route:''' <code>redeem</code>, '''originator:''' client
   746  
   747  <code>payload</code>
   748  {|
   749  ! field      !! type   !! description
   750  |-
   751  | orderid    || string || the order ID
   752  |-
   753  | matchid    || string    || the match ID
   754  |-
   755  | coinid     || string || hex-encoded coin ID
   756  |-
   757  | secret     || string || the hex-encoded swap contract secret
   758  |-
   759  | sig        || string || client signature of the serialized notification. serialization described below
   760  |}
   761  
   762  '''Redeem serialization'''
   763  {|
   764  ! field      !! size (bytes)  !! description
   765  |-
   766  | orderid    || 32 || the order ID
   767  |-
   768  | matchid    || 32  || the match ID
   769  |-
   770  | coin ID    || asset-dependent  || the coin ID
   771  |-
   772  | secret     || 32  || the swap contract secret
   773  |}
   774  
   775  The DEX responds with an acknowledgement.
   776  
   777  The DEX informs the taker when the maker has redeemed.
   778  
   779  '''Request route:''' <code>redemption</code>, '''originator:''' DEX
   780  
   781  <code>payload</code>
   782  {|
   783  ! field      !! size (bytes)  !! description
   784  |-
   785  | orderid    || 32 || the order ID
   786  |-
   787  | matchid    || 32  || the match ID
   788  |-
   789  | coinid     || string || hex-encoded coin ID
   790  |-
   791  | secret     || string || the hex-encoded swap contract secret
   792  |-
   793  | timestamp  || int    || server's UNIX timestamp (milliseconds)
   794  |-
   795  | sig        || string || DEX's signature of the serialized notification. serialization described below
   796  |}
   797  
   798  '''Redemption serialization'''
   799  
   800  {|
   801  ! field      !! size (bytes)  !! description
   802  |-
   803  | orderid    || 32 || the order ID
   804  |-
   805  | matchid    || 32  || the match ID
   806  |-
   807  | coin ID    || asset-dependent  || the coin ID
   808  |-
   809  | secret     || 32  || the swap contract secret
   810  |-
   811  | timestamp  || 8  || server's UNIX timestamp (milliseconds)
   812  |}
   813  
   814  The client will respond with an acknowledgement.
   815  
   816  The taker will get the key from the maker's redemption and broadcast their own
   817  redemption transaction.
   818  
   819  It is also possible for an epoch order to go through the matching cycle without
   820  generating a match. This will be common for limit orders, but can also occur for
   821  market orders if there are no booked orders to match with. When the server fails
   822  to find any matches, a <code>nomatch</code> notification will be sent to the
   823  client.
   824  
   825  '''Notification route:''' <code>nomatch</code>, '''originator:''' DEX
   826  
   827  <code>payload</code>
   828  {|
   829  ! field   !! type   !! description
   830  |-
   831  | orderid || string || order ID
   832  |}
   833  
   834  A client can request the current match status using the
   835  <code>match_status</code> request. Note that the route is provided as a
   836  convenience, and persistence of match data may be subject to the operator's
   837  archiving policy. <code>match_status</code> can be used to recover after a
   838  temporary disconnection, but should not be relied on as a source of historical
   839  match data.
   840  
   841  '''Notification route:''' <code>match_status</code>, '''originator:''' client
   842  
   843  <code>payload</code>: an array of <code>MatchRequest</code> objects
   844  
   845  '''<code>MatchRequest</code> object'''
   846  
   847  {|
   848  ! field   !! type   !! description
   849  |-
   850  | base    || int    || the base asset ID
   851  |-
   852  | quote   || int    || the quote asset ID
   853  |-
   854  | matchid || string || hex-encoded Match ID
   855  |}
   856  
   857  <code>result</code>: an array of <code>MatchStatus</code> objects
   858  
   859  '''<code>MatchStatus</code> object'''
   860  
   861  {|
   862  ! field         !! type   !! description
   863  |-
   864  | matchid       || string || the match ID
   865  |-
   866  | status        || int    || See [[https://github.com/decred/dcrdex/blob/master/dex/order/match.go|match.go]] for codes.
   867  |-
   868  | makercontract || string || the swap contract broadcast by the maker
   869  |-
   870  | takercontract || string || the swap contract broadcast by the taker
   871  |-
   872  | makerswap     || string || the coin ID for the swap broadcast by the maker
   873  |-
   874  | takerswap     || string || the coin ID for the swap broadcast by the taker
   875  |-
   876  | makerredeem   || string || the coin ID for the maker's redemption
   877  |-
   878  | takerredeem   || string || the coin ID for the taker's redemption
   879  |-
   880  | secret        || string || the swap contract secret
   881  |-
   882  | active        || bool   || whether the match is still active
   883  |}
   884  
   885  Empty fields may be omitted from the encoded <code>MatchStatus</code> object.
   886  Results are only returned for matches that could be found in the server
   887  records. No error is set in the case of unfound matches.
   888  
   889  ==Match Revocation==
   890  
   891  A match can be revoked by the server if a client fails to act within the
   892  [[fundamentals.mediawiki/#global-variables|broadcast timeout]]. A match revocation will result in
   893  penalties for the violating party only.
   894  The revoked match quantity is not added back to the order book in any form.
   895  
   896  '''Request route:''' <code>revoke_match</code>, '''originator:''' DEX
   897  
   898  <code>payload</code>
   899  {|
   900  ! field    !! type   !! description
   901  |-
   902  | orderid  || string || the order ID
   903  |-
   904  | matchid  ||  string  || the match ID
   905  |-
   906  | sig      || string || DEX's hex-encoded signature of serialized revocation. serialization described below
   907  |}
   908  
   909  '''Revocation serialization'''
   910  
   911  {|
   912  ! field      !! size (bytes)  !! description
   913  |-
   914  | orderid    || 32  || the order ID
   915  |-
   916  | matchid    || 32  || the match ID
   917  |}
   918  
   919  The client will respond with an acknowledgement.
   920  
   921  ==Trade Suspension==
   922  
   923  There are a number of scenarios where the server may suspend operations,
   924  intentionally or not.
   925  During trade suspension, standing limit orders are not necessarily revoked.
   926  
   927  If the server intentionally suspends operations, they should provide a
   928  notification to connected clients as early as possible, ideally with several
   929  epochs for the client to get their orders situated before matching ceases.
   930  
   931  Clients should be prepared to lose connection during suspension. Clients will
   932  need to reconnect and complete settlement when the server comes back online.
   933  
   934  If the server disconnects without notice, it is expected that orders placed
   935  during the current epoch are revoked at no penalty to the client and that
   936  standing limit orders are persisted.
   937  
   938  The suspension notification may indicate that standing limit orders will not be
   939  persisted.
   940  This would be the case if the DEX needs to change an asset variable such as
   941  the lot size or minimum transaction fee rate.
   942  
   943  If standing limit orders are persisted, they will be auto-revoked if the client
   944  does not reconnect before the next [[comm.mediawiki/#session-authentication|start epoch]].
   945  
   946  '''Request route: ''' <code>suspension</code>, '''originator:''' DEX
   947  
   948  <code>payload</code>
   949  {|
   950  ! field       !! type   !! description
   951  |-
   952  | marketid    || string || the market ID
   953  |-
   954  | finalepoch  || uint64 || the last epoch during which orders will be collected and matched
   955  |-
   956  | suspendtime || uint64 || the UNIX timestamp corresponding to the end of the final epoch (milliseconds)
   957  |-
   958  | persistbook || bool   || whether standing limit orders will persist through the suspension
   959  |}
   960  
   961  Clients will also be informed when trading is resumed.
   962  
   963  '''Request route: ''' <code>resumption</code>, '''originator:''' DEX
   964  <code>payload</code>
   965  {|
   966  ! field       !! type   !! description
   967  |-
   968  | marketid    || string || the market ID
   969  |-
   970  | startepoch  || uint64 || the epoch number at which trading did or will commence. May be in the future
   971  |-
   972  | epochlen || uint64 || the [[#epoch-based-order-matching|epoch duration]] (milliseconds)
   973  |}