Skip to main content

Overview

AnySwap swaps between Lightning and on-chain Bitcoin or Liquid. The SDK, in Rust and in TypeScript, carries a driver that runs any number of swaps to their end, so an app creates swaps and shows their status.

FlowThe user paysThe user receives
SwapInBTC or L-BTC on chaina Lightning payment to their invoice
SwapOuta Lightning hold invoiceBTC or L-BTC on chain

What the user risks​

The on-chain funds sit in a taproot output with three spending paths:

PathSpent byWhen
Preimagethe side paid on chainwith the Lightning payment's preimage
Keypatheither sideonce the other side has handed over its key
CSVthe side that funded italone, after the timeout

The server can delay a swap, but not redirect it. If it stops cooperating:

SwapOutcome
SwapInthe deposit comes back by the CSV path
SwapOut, before Claimingthe hold invoice fails back
SwapOut, Claimingthe driver claims by the preimage path, and must be running to do it before the CSV runs out

A SwapOut enters Claiming only with at least claim_margin of its CSV left: 252 blocks, about 42 hours, on Bitcoin by default.

A status comes from the chain and from secrets the driver checked itself, never from the server's word. The server's own label is kept as server_status, for display.

Who does what​

JobDone by
Mint the invoice a SwapIn pays out toyour app
Pay the hold invoice of a SwapOutyour app
Give the claim and refund addressesyour app, per swap or through a hook
Keep the swap recordsyour app, behind the store interface
Check the server's terms against your requestthe driver
Track each swap on chainthe driver
Send the protocol messagesthe driver
Claim a SwapOut, refund a SwapInthe driver

Install​

cargo add anyswap-sdk --features esplora

Where to go next​

ToRead
Run a first swapQuickstart
Know every status and what to showSwaps
Run the driver and handle its errorsDriving
Get a SwapIn's funds backRefunds
Survive restarts and lost devicesPersistence and recovery
Swap on LiquidLiquid
Handle a refusal or a cancelErrors
Tune timeouts, depths and fee capsConfiguration
Run in a browser, or on your own chain backendPlatforms
Move from a Boltz clientCompared with Boltz
Look up a type or methoddocs.rs for Rust, the TypeScript reference