Decred and the Atomic Swap Revolution

The concept of cross-chain atomic transfers was proposed in 2013 and has taken tremendous effort to realise the capabilities of this technology.

Decred and the Atomic Swap Revolution
Decred and the atomic swap revolution


One of the first breakthroughs came In 2017 when Decred conducted the first atomic swap between Decred DCR and Litecoin (LTC).

Decred has taken atomic swap technology from a single swap concept to a full trading experience with DCRDEX. These fulfilled goals have taken the crypto space by surprise, to the point, that most are still sceptical as to its success and legitimacy.

In the world of crypto where the normal behaviour is to either not deliver or delivered products that don’t resemble the original design, Decred has smashed through this wall to make the next evolution that will benefit the entire space. Decentralisation is easy to talk about but hard to deliver, Decred delivers on its targets every time!

In this series of videos, it’s been my aim at show that Decred has indeed delivered a product that follows through on all of the original promises from 2017. This includes:

  • On-chain atomic swaps for peer to peer trading
  • No trading fees
  • No custodians or middle men
  • No KYC
  • No high frequency trading
  • A secure and stable product
  • Internal, external and SPV wallets
  • and all code is open source

All of this has been paid for in full by the Decred treasury. Future improvements include a mesh network that will allow anyone to spin up an exchange server and link their order books to all others. Further integrations include full interaction with both Decred and Bitcoin’s Lightning Networks.

Atomic swap technology is the now and the future, which has brought a new standard for peer to peer exchange, interoperability and transaction. The big question is, how does DCRDEX’s atomic swaps work?

DCRDEX Atomic Swap

Anyone can open DCRDEX and view the order books and anyone can create or import wallets for their desired pairs, the most common wallets and pairs are currently Decred (DCR), Bitcoin (BTC) and Litecoin (LTC). Once you have DCRDEX open, the wallets functioning, and populated with coins you are ready to use the exchange.

A cross-chain atomic swap is a trade between two users of different cryptocurrencies. For example, one party may send Decred to a second party's Decred address, while the second party would send Bitcoin to the first party's Bitcoin address. DCRDEX amplifies this process with an order matching engine that builds a market with others looking to swap the same currencies.

Atomic Swap Process Contract and redemption

DCRDEX primarily acts as a matchmaker, connecting two orders and relaying messages between the two clients. Additionally, the DEX performs some trade monitoring to ensure that parties are acting in good faith.

In the most basic case, demonstrated in DCRDEX’s specification, Alice and a trader named Bob are the only participants and their orders match perfectly.

Decred Atomic Swap

STEP 1: Alice places a limit order to sell 3 DCR at 0.1 BTC per DCR for a total of 0.3 BTC. As a limit order, Alice must specify both the quantity and the price, as a rate, as well as her BTC address. Alice signs the order information. The signature provides proof of the order and justifies any penalties imposed on Alice should she fail to follow through on a trade.

STEP 2: During some later epoch (which is a block of time typically less than 10 seconds), Bob places a market order to buy DCR with BTC at the best available price. Bob’s order will contain both an address and a signature. As a market order, Bob’s order must match in this epoch. Any unfilled portion is immediately canceled.

STEP 3: At the end of the epoch, the DEX performs a match cycle, recognizes the match, and informs both Alice and Bob that a match has occurred. Because the order quantities are identical, the orders are simply removed from the book and set aside for monitoring. The DEX informs both clients that a match has been made and provides Bob with Alice’s address.

STEP 4: Because Alice is the maker, she will broadcast her initialization transaction first. She will be expected to broadcast and report her transaction to the DEX within a specified amount of time.

Before Alice can prepare her initialization transaction, she must generate a key known only to herself. From the key, Alice generates a “lock” and constructs a swap contract such that if someone can provide both Alice’s key and the pubkey for Bob’s specified address, they can spend the output. In practice, the key is simply a random 32-byte sequence, and the lock is its hash.

In addition, Alice constructs the contract with a second, alternative lock that allows her to spend the output herself, but only if the output remains unspent for a specified amount of time. Alice sets her timelock to 20 hours. (This is the refund mechanism???)

Alice broadcasts her initialization transaction to the Decred network. She informs the DEX of the transaction details and sends the lock, which the DEX relays to Bob. Alice does not reveal the key that she created.

STEP 5: Bob inspects Alice's transaction. After the requisite number of confirmations, Bob prepares his initialization transaction. He uses Alice's lock here as well, and creates a swap contract satisfied by Alice’s key and the pubkey for Alice’s address.

Bob sets his timelock to 8 hours. Bob should also check that the timelock in Alice's initialization is set to 20 hours. If Alice’s timelock duration is set near or below Bob’s timelock duration, Alice could potentially spend the DCR output before Bob.

Bob broadcasts his initialization transaction to the Bitcoin network. He informs the DEX of the transaction details, and the DEX relays the information to Alice.

STEP 6: Alice prepares a redemption transaction that spends the output from Bob’s initialization transaction to her own address. In order to spend the Bitcoin from Bob’s transaction, she needs to provide the key created in step 4.

Alice broadcasts her redemption transaction to the Bitcoin network. Though both the DEX and Bob are capable of monitoring for the transaction, it is expected that Alice informs the DEX server of the transaction details anyway. The DEX server will notify Bob of the details.

STEP 7: Bob prepares his redemption transaction using the key gleaned from Alice’s redemption transaction and the output from Alice’s initialization transaction.

Bob broadcasts his redemption transaction to the Decred network, completing the atomic swap.

Hopefully, that’s given you some insight into how Atomic Swaps work in DCRDEX and how the process allows for a non custodian peer to peer exchange experience.