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:
Taker finds a match, e.g. buying 1 BTC for 10k DAI
Taker creates the private
r_preimage
and the publicr_hash
for the atomic swapTaker sends the
SwapRequest
message to the maker, which includesr_hash
Maker confirms full or partial quantity in the
SwapAccepted
messageTaker starts the swap by dispatching the first-leg HTLCs on the DAI payment channel to the maker end, using
r_hash
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.
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.Both nodes locally mark the swap as completed once the respective HTLC is resolved and the payment is finalized.
Possible misbehaviors and their outcome:
Misbehavior | Outcome | Effect on payment channels |
---|---|---|
Maker doesn't respond to the | Taker should timeout the swap and penalize the maker | None |
Taker doesn't start the swap after receiving the | Maker should timeout the swap and penalize the taker | None |
Maker receives the first-leg HTLC with insufficient amount or incorrect CLTV delta | Maker should send the taker a | Taker funds are locked until HTLC expiration |
Maker doesn't continue the swap after receiving the first-leg HTLC | Taker should timeout the swap and penalize the maker | Taker funds are locked until HTLC expiration |
Taker receives the second-leg HTLC with insufficient amount or incorrect CLTV delta | Taker should sends the maker a | Both Taker and Maker funds are locked until HTLC expiration |
Taker doesn't release | Maker should timeout the swap and penalize the taker | Both Taker and Maker funds are locked until HTLC expiration |
Swap Protocol
SwapRequest Message (0x0c)
The SwapRequest
message is sent by the taker to the maker to start the swap negotiation.
SwapAccepted Message (0x0d)
The SwapAccepted
message is sent by the maker to the taker to accept the swap request.
SwapFailed Message (0x0f)
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:
Failure Reason | Meaning | Description |
---|---|---|
| Order Not Found | Could not find the order specified by a swap request |
| Order On Hold | The order specified by a swap request is on hold for a different ongoing swap |
| Invalid Swap Request | The swap request contained invalid data |
| Swap Client Not Setup | We are not connected to both swap clients, or we are missing public key identifiers for the peer's nodes |
| No Route Found | Could not find a route to complete the swap |
| Unexpected Client Error | A swap client call failed for an unexpected reason |
| Invalid Swap Message Received | Received a swap message with invalid data |
| Send Payment Failure | The call to send payment failed |
| Invalid Resolve Request | The swap resolver request was invalid |
| Payment Hash Reuse | The swap request attempts to reuse a payment hash |
| Swap Timed Out | The swap timed out while we were waiting for it to complete execution |
| Deal Timed Out | The deal timed out while we were waiting for the peer to respond to our swap request |
| Unknown Error | The swap failed due to an unrecognized error |
Last updated