Skip to content

Instantly share code, notes, and snippets.

@chappjc

chappjc/dcrdex-swap.md

Last active Apr 7, 2020
Embed
What would you like to do?
dcrdex testnet DCR-BTC swap

The Trade

Maker sell 42 DCR for 0.42 BTC (0.01 BTC/DCR):

Taker sell, perfect match:

Server (order matcher and swap coordinator)

Initial Match Negotiation

[maker order placed and booked]
2020-03-31 15:04:00.000 [TRC] MKT: Preimage request sent for order 66a1069cfac24794ed3680521be3521027ac6e9a15e132e6c07d69276009e3a7
2020-03-31 15:04:00.028 [TRC] MKT: Good preimage received for order 66a1069cfac24794ed3680521be3521027ac6e9a15e132e6c07d69276009e3a7: 7ffe14c91c10264a9842a5d668d98a3f275b580827cdc8bcfefdb11fc5f34f31
2020-03-31 15:04:00.029 [DBG] MKT: Collected 1 valid order preimages, missed 0. Commit checksum: c13a31c63e0bbdd632bf83a7b71ae47a46787804a69022c3ce18a0553cae7dc4
2020-03-31 15:04:00.030 [DBG] MKT: Matching complete for epoch 158566703: 0 matches (0 partial fills), 0 completed OK (not booked), 1 booked, 0 unbooked, 0 failed
...
[taker order placed and matched, both filled completely]
2020-03-31 15:25:50.000 [TRC] MKT: Preimage request sent for order 4155935274c32afacd25ba56605c4bfa061b4981c4fd19b2bb615f379c620f0d
2020-03-31 15:25:50.064 [TRC] MKT: Good preimage received for order 4155935274c32afacd25ba56605c4bfa061b4981c4fd19b2bb615f379c620f0d: 3284eae46af77c8c11d475d156b6f9d76cd1ad7f6d5bd1205c12cbab97a25d04
2020-03-31 15:25:50.064 [DBG] MKT: Collected 1 valid order preimages, missed 0. Commit checksum: 8c76a02556fdb9e9359aacfb0c6b267bb459f19fd51362f19f0ea2bd473b1d93
2020-03-31 15:25:50.064 [DBG] MKT: Matching complete for epoch 158566834: 1 matches (0 partial fills), 1 completed OK (not booked), 0 booked, 1 unbooked, 0 failed
[match negotiation begins]
2020-03-31 15:25:50.064 [DBG] MKT: Negotiating 1 matches for epoch 158566834:10000
2020-03-31 15:25:50.065 [DBG] SWAP: Negotiate: sending 'match' ack request to user e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4 for 1 matches
2020-03-31 15:25:50.065 [DBG] SWAP: Negotiate: sending 'match' ack request to user 8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f for 1 matches
2020-03-31 15:25:50.094 [DBG] SWAP: processMatchAcks: 'match' ack received from e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4 for 1 matches
2020-03-31 15:25:50.095 [DBG] SWAP: processMatchAcks: storing valid 'match' ack signature from e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4 (maker=true) for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d (status NewlyMatched)

Maker starts the swap with DCR contract

[maker initializes the swap, broadcasts DCR contract]
2020-03-31 15:25:50.095 [DBG] SWAP: handleInit: 'init' received from e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4 for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d, order 66a1069cfac24794ed3680521be3521027ac6e9a15e132e6c07d69276009e3a7
2020-03-31 15:25:50.095 [DBG] SWAP: step(user=e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4, match=28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d): ready for user 8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f action, status NewlyMatched, next MakerSwapCast
[meanwhile, taker acknowledges the match]
2020-03-31 15:25:50.139 [DBG] SWAP: processMatchAcks: 'match' ack received from 8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f for 1 matches
2020-03-31 15:25:50.139 [DBG] SWAP: processMatchAcks: storing valid 'match' ack signature from 8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f (maker=false) for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d (status NewlyMatched)
...
[DCR network latency, server observes and validates the maker's DCR contract, and requests an audit from the taker]
2020-03-31 15:25:55.510 [DBG] SWAP: processInit: valid contract received at 2020-03-31 15:25:55.51 +0000 UTC from e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4 for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d, swapStatus NewlyMatched => MakerSwapCast
2020-03-31 15:25:55.510 [DBG] SWAP: processInit: sending contract 'audit' ack request to counterparty 8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d
[taker waits for confirmation (2) of maker's DCR contract]
2020-03-31 15:33:00.464 [DBG] ASSET[btc]: Run: Processing new block 0000000000000a928d90994d0358f3371842408b6cc5299ce091fcea578fcd59
2020-03-31 15:33:00.464 [DBG] ASSET[btc]: Notifying 1 btc asset consumers of new block at height 1692752
2020-03-31 15:35:55.868 [DBG] ASSET[dcr]: Run: Processing new block 00000065b1ca3c495de63fe61eea70c277d656958f3d949042db0fc3680bdb98
...
2020-03-31 15:46:40.463 [DBG] ASSET[btc]: Run: Processing new block 000000000000029e3a288d7aa8cecd1879b508ea89096b472193fc59b28be8fe
2020-03-31 15:46:40.463 [DBG] ASSET[btc]: Notifying 1 btc asset consumers of new block at height 1692758

Taker broadcasts their BTC contract

[DCR contract confirmed, taker broadcasts BTC contract]
2020-03-31 15:47:49.246 [DBG] ASSET[dcr]: Run: Processing new block 0000006f8d20645c80a1ab78b1e94adb5678ec39882953ecd85dc1b079ebab0e
2020-03-31 15:47:50.245 [DBG] SWAP: handleInit: 'init' received from 8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d, order 4155935274c32afacd25ba56605c4bfa061b4981c4fd19b2bb615f379c620f0d
2020-03-31 15:47:50.245 [DBG] SWAP: step(user=8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f, match=28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d): ready for user e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4 action, status MakerSwapCast, next TakerSwapCast
...
[BTC network broadcast latency, server observes and validates the taker's BTC contract, and requests maker audit]
2020-03-31 15:47:55.511 [DBG] SWAP: processInit: valid contract received at 2020-03-31 15:47:55.511 +0000 UTC from 8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d, swapStatus MakerSwapCast => TakerSwapCast
2020-03-31 15:47:55.511 [DBG] SWAP: processInit: sending contract 'audit' ack request to counterparty e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4 for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d

Maker redeems

[maker waits for confirmation (2) of taker's BTC contract @ height 1692760, and broadcasts redemption]
2020-03-31 15:55:18.278 [DBG] SWAP: handleRedeem: 'redeem' received from e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4 for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d, order 66a1069cfac24794ed3680521be3521027ac6e9a15e132e6c07d69276009e3a7
2020-03-31 15:55:18.278 [DBG] SWAP: step(user=e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4, match=28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d): ready for user 8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f action, status TakerSwapCast, next MakerRedeemed
[BTC network latency, server observes and validates maker BTC redemption, and requests acknowledgement from taker]
2020-03-31 15:55:20.463 [DBG] ASSET[btc]: Run: Processing new block 00000000000008dc01d22a491e7a6374c3399db398ab77e328764df66f47761f
2020-03-31 15:55:20.463 [DBG] ASSET[btc]: Notifying 1 btc asset consumers of new block at height 1692760
...
2020-03-31 15:55:25.511 [DBG] SWAP: processRedeem: valid redemption received at 2020-03-31 15:55:25.51 +0000 UTC from e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4 for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d, swapStatus TakerSwapCast => MakerRedeemed
2020-03-31 15:55:25.511 [DBG] SWAP: processRedeem: sending contract 'redeem' ack request to counterparty 8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d

Taker redeems

[taker acks the maker's BTC redeem, broadcasts their DCR redeem, and informs the server]
2020-03-31 15:55:25.639 [DBG] SWAP: handleRedeem: 'redeem' received from 8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d, order 4155935274c32afacd25ba56605c4bfa061b4981c4fd19b2bb615f379c620f0d
2020-03-31 15:55:25.639 [DBG] SWAP: step(user=8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f, match=28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d): ready for user e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4 action, status MakerRedeemed, next MatchComplete
...
[DCR network broadcast latency, server observes and validates the taker's DCR redeem]
2020-03-31 15:55:30.510 [DBG] SWAP: processRedeem: valid redemption received at 2020-03-31 15:55:30.51 +0000 UTC from 8168ff41662bc25dd849475ec1898f346abca3c16b065d13a49c46204eb8842f for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d, swapStatus MakerRedeemed => MatchComplete
2020-03-31 15:55:30.510 [DBG] SWAP: processRedeem: sending contract 'redeem' ack request to counterparty e3c000c2ae03fd58fda359bb9cbd8d4cdbc62064def8b016fe152d2fa5dcf6c4 for match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d

Both parties have completed the required on-chain actions to complete the swap, and the necessary communications to be credited for a successfully completed order.

Maker

dexc (client)

2020-03-31 10:55:18.259 [INF] CORE: match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d complete: sell 4200000000 dcr

dcrwallet (DCR wallet)

2020-03-31 10:25:50.074 [INF] WLLT: Inserting unconfirmed transaction 4a14a2d79c1374d286ebd68d2c104343bcf8be44ed54045b5963fbf73667cecc
2020-03-31 10:35:55.851 [DBG] WLLT: Marking unconfirmed transaction 4a14a2d79c1374d286ebd68d2c104343bcf8be44ed54045b5963fbf73667cecc mined in block 396615
2020-03-31 10:35:55.851 [DBG] WLLT: Marked address TsnuLiVxWBPMdNd2fG7kWhMQTj8FXSWJ9ab used
2020-03-31 10:35:55.851 [DBG] WLLT: Updating processed txs block marker to 00000065b1ca3c495de63fe61eea70c277d656958f3d949042db0fc3680bdb98
2020-03-31 10:35:55.854 [INF] SYNC: Connected block 00000065b1ca3c495de63fe61eea70c277d656958f3d949042db0fc3680bdb98, height 396615, 1 wallet transaction(s)

bitcoind (BTC wallet)

2020-03-31T15:55:18Z [default wallet] AddToWallet f67e0fe0aec03be8aa686aec0bf8b7f345334e00c73adb1cbd2ecc526a0382dc  new
2020-03-31T15:56:33Z UpdateTip: new best=00000000000002772fdd21335a0c45ac34fb5c8dbc48fd0e0b2f052ae86bcf15 height=1692761 version=0x3fffe000 log2_work=72.364334 tx=55361407 date='2020-03-31T15:56:40Z' progress=1.000000 cache=1.1MiB(7889txo) warning='77 of last 100 blocks have unexpected version'
2020-03-31T15:56:33Z [default wallet] AddToWallet f67e0fe0aec03be8aa686aec0bf8b7f345334e00c73adb1cbd2ecc526a0382dc  update

Verifying received funds after 2 confirmations:

$  bitcoin-cli -testnet listunspent
[
  {
    "txid": "f67e0fe0aec03be8aa686aec0bf8b7f345334e00c73adb1cbd2ecc526a0382dc",
    "vout": 0,
    "address": "tb1qspdc8e4cdlr4z8686n8etgcy392dx2pwx3km7w",
    "scriptPubKey": "0014805b83e6b86fc7511f47d4cf95a3048954d3282e",
    "amount": 0.41967800,
    "confirmations": 2,
    "spendable": true,
    "solvable": true,
    "desc": "wpkh([29f1ba01/0'/1'/3']02328f2d9dafcd9ce3c4b1729c7177620ea9bdfe11bde41c4782f827046fc9e308)#y83svppg",
    "safe": true
  },
  ...
]

0.41967800 received after BTC network fees

Taker

dexc (client)

2020-03-31 10:55:25.607 [INF] CORE: match 28f328483c1bc331d18245f064b3c15d58fed325654dbc6fff359c2bec67fb1d complete: buy 4200000000 dcr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.