Skip to content

Instantly share code, notes, and snippets.

@erubboli

erubboli/dex.md Secret

Created May 29, 2024 18:30
Show Gist options
  • Save erubboli/e037f991181c978ec8ad0d0d8f3bd31e to your computer and use it in GitHub Desktop.
Save erubboli/e037f991181c978ec8ad0d0d8f3bd31e to your computer and use it in GitHub Desktop.

Decentralized Exchanges (DEXes)

Introduction to Atomic Swaps

Atomic swaps represent a revolutionary method for conducting direct, peer-to-peer cryptocurrency exchanges without the need for intermediaries such as centralized exchanges. This technology ensures that both parties either complete the trade successfully or the transaction is voided, eliminating the risk of one party defrauding the other.

Bitcoin - Mintlayer Atomic Swaps

The Bitcoin - Mintlayer atomic swap mechanism facilitates direct exchanges between Bitcoin and Mintlayer tokens through a secure, trustless protocol. Below, we outline the process:

Step-by-Step Process

  1. Initiating the Swap:

    • Party A generates a secret number, known only to them.
    • A creates a transaction (TX1) to send their Bitcoin to Party B, with a special condition: B can only claim the Bitcoin if they know the secret number. Additionally, if both A and B agree (using a multisignature), the Bitcoin can be refunded back to A after 48 hours if the swap doesn't go through.
    • A sends TX1 to B, but does not publish it yet to avoid potential loss of funds if B decides to back out.
  2. Creating a Refund Transaction:

    • A also creates a second transaction (TX2) to get their Bitcoin back from TX1 if needed, which requires both their signatures and can only be completed after 48 hours.
    • A sends TX2 to B, who signs it and sends it back to A, making it ready to be used if necessary.
  3. Commitment:

    • A then publishes TX1, officially committing to the swap. If B doesn't participate further, A can get their Bitcoin back after 48 hours using TX2.
  4. Reciprocation:

    • Party B creates a similar transaction (TX3) to send their Mintlayer coins to A. This transaction also has conditions: A can claim the Mintlayer coins with the secret number, or both can agree to refund B after 24 hours.
    • B sends TX3 to A but doesn't publish it immediately to protect their funds.
  5. Refund Setup:

    • B creates a refund transaction (TX4) for their Mintlayer coins, requiring both signatures, and sends it to A, who signs and returns it to B.
  6. Publishing:

    • B then publishes TX3, committing to the swap. A now has 24 hours to claim the Mintlayer coins by revealing the secret number in a transaction.
  7. Completion:

    • A claims the Mintlayer coins from TX3 by using the secret number, which also reveals the secret to B.
    • B uses the revealed secret to claim the Bitcoin from TX1.

This process ensures that if either party fails to complete their part, the other can retrieve their original funds. The use of time-locked transactions and multisignatures ensures both parties are protected throughout the swap. This method allows for a decentralized and trustless exchange of cryptocurrencies, enhancing security and reducing reliance on intermediaries.

Alternative Approach for Atomic Swaps Supporting Partial Trades on the Same Chain

Another innovative method for atomic swaps involves using a new transaction output type called TxOutput::AnyoneCanTake. This approach is particularly useful for supporting partial trades on the same blockchain.

Creating an Order

  • A new transaction output, TxOutput::AnyoneCanTake, represents a buy or sell order. This output creates an account with respective balances of the currency involved in the trade.
  • An order can be partially or fully filled by anyone. The original order creator specifies an authority who can withdraw from this account.

Filling an Order

  • To fill an order, a participant creates a transaction that "burns" the currency being bought by the order and generates the corresponding amount of the other currency from the order account.
  • This allows for the exchange of tokens for other tokens, coins for any token, or vice versa.

Handling Partial Trades

  • The exchange uses integer arithmetic to manage trades, ensuring that each token's price is adjusted correctly for partial fills. For example, if 3 tokens are sold for 100 coins, each token might cost [33, 33, 34] coins, ensuring fair distribution even in partial trades.

This method allows for a more flexible trading environment where users can fill orders partially or fully, enhancing liquidity and making the atomic swap process more versatile. The use of TxOutput::AnyoneCanTake creates a dynamic and efficient trading mechanism that supports a wide range of trade types on the same blockchain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment