4️⃣-Swap Protocol

Overview

Once an order match is found in the taker’s order book, the swap protocol should be initiated. The current swap protocol assumes that the taker and maker are connected via a payment channel network (e.g. Lightning or Connext) with sufficient balance available for the swap. The following is the swap protocol's "happy" flow:

  1. Taker finds a match, e.g. buying 1 BTC for 10k DAI

  2. Taker creates the private r_preimage and the public r_hash for the atomic swap

  3. Taker sends the SwapRequest message to the maker, which includes r_hash

  4. Maker confirms full or partial quantity in the SwapAccepted message

  5. Taker starts the swap by dispatching the first-leg HTLCs on the DAI payment channel to the maker end, using r_hash

  6. Maker listens for an incoming HTLC on the DAI payment channel. Once it arrives he verifies price and quantity and then dispatches the second-leg HTLCs on the BTC payment channel to the taker end.

  7. Taker listens for an incoming HTLC on the BTC payment channel. Once it arrives he releases r_preimage. This allows both the taker and the maker payments to finalize.

  8. Both nodes locally mark the swap as completed once the respective HTLC is resolved and the payment is finalized.

Possible misbehaviors and their outcome:

Swap Protocol

SwapRequest Message (0x0c)

`string id = 1`
The message's globally unique identifier, generated by the sender 

`uint64 proposed_quantity = 2`
The proposed quantity

`string pair_id = 3`
The trading pair for the swap

`string order_id = 4`
The unique identifier of the maker order

`string r_hash = 5`
The taker preimage hash (in hex)

`uint32 taker_cltv_delta = 6`
The CLTV delta from the current height that should be used to set the timelock for the final hop when sending to the taker

The SwapRequest message is sent by the taker to the maker to start the swap negotiation.

SwapAccepted Message (0x0d)

`string id = 1`
The message's globally unique identifier, generated by the sender 

`uint64 req_id = 2`
The id from the received SwapRequest message

`string r_hash = 3`
The taker’s preimage hash (in hex) from the received SwapRequest message

`string quantity = 4`
The accepted quantity (which may be less than the proposed quantity)

`uint32 maker_cltv_delta = 5`
The CLTV delta from the current height that should be used to set the timelock for the final hop when sending to the maker

The SwapAccepted message is sent by the maker to the taker to accept the swap request.

SwapFailed Message (0x0f)

`string id = 1`
The message's globally unique identifier, generated by the sender 

`uint64 req_id = 2`
An optional id from the received SwapRequest message. Otherwise, this field is empty

`string r_hash = 3`
The taker’s preimage hash (in hex)

`string error_message = 4`
Additional information regarding the failure reason

`uint32 failure_reason = 5`
The failure reason

The SwapFailed message can be sent by either side of the swap protocol, at any time, to announce the swap termination.

failure_reason is an optional parameter for specifying the failure reason:

Last updated