Skip to main content
This guide demonstrates how to implement a secure and efficient cryptocurrency order exchange system with the Blnk Ledger. You’ll learn how to handle order creation, escrow management, order matching, and atomic settlements. A cryptocurrency exchange operates in three main steps:
  • Order Creation: Users express their intent to trade by creating orders
  • Order Matching: Compatible orders are paired together
  • Settlement: Assets are exchanged atomically between parties
To illustrate this flow, let’s work with a practical example where two users want to exchange different cryptocurrencies:

Designing your map

Before writing code, it’s crucial to design a money movement map that outlines how money moves in your system. This serves as the blueprint for your implementation. For our order exchange system, here’s how funds will flow:

Step 1: Order creation

Each customer initiates their order by starting a transaction in an inflight state, temporarily holding their assets (e.g., ETH or MATIC) in escrow (@ETH_Escrow or @MATIC_Escrow) until the order is matched or finalized:
  1. John deposits ETH into @ETH_Escrow (inflight).
  2. Emily deposits MATIC into @MATIC_Escrow (inflight).
Order creation map Explore the map yourself here

Step 2: Order matching

Order matching occurs when the system pairs John’s inflight ETH transaction in @ETH_Escrow with Customer B’s inflight MATIC transaction in @MATIC_Escrow, linking them under a shared . This step confirms compatibility and prepares the orders for settlement, keeping funds securely held in escrow until the final exchange.
  1. John’s ETH in @ETH_Escrow matches Emily’s MATIC order (and linked by a root id: root_id_123).
  2. Emily’s MATIC in @MATIC_Escrow matches John’s ETH order (and linked by a root id: root_id_123).
Order matching map

Step 3: Order settlement

  1. Inflight MATIC transaction to @MATIC_Escrow commits and settles to John’s MATIC balance. The settlement transactions are linked to matched orders with the root_id.
  2. Inflight ETH transaction to @ETH_Escrow commits and settles to Emily’s ETH balance. The settlement transactions are linked to matched orders with the root_id.
Order settlement map

Prerequisites

Before starting, ensure you have:
  1. A running Blnk Core instance (e.g. at http://localhost:5001).
  2. An API key for Blnk (replace YOUR_API_KEY in the code examples). Required for authenticated requests.
  3. Optionally, you can connect your Blnk Core to your Blnk Cloud workspace to view your ledger data.

Setting up your ledger

Before implementing the exchange logic, we need to establish the foundational structure in your Blnk Ledger. Let’s set up separate ledgers for each cryptocurrency and create the necessary balances:

Create customer ledgers

Create two ledgers to segregate different cryptocurrencies:

Create customer balances

Create balances for both users in each ledger:

Fund the initial balances

In this example, we use a precision of 100 for simplicity (e.g., 1 ETH = 100 units). In production, cryptocurrencies like ETH use a precision of 10^18 (1 ETH = 10^18 wei). Adjust the precision parameter in all transaction calls accordingly and ensure amount calculations reflect this precision.

Phase 1: Order Creation

When John or Emily creates an order, we need to:
  1. Confirm that they have sufficient funds to successfully place the order.
  2. Initialize it via inflight on your Ledger and reserve the funds in escrow until the order is matched or canceled.
  3. Record in our order book.
  4. Bringing it all together.
Add inflight_expiry_date to automatically roll back the inflight transaction if an order isn’t matched within a timeout period.

Phase 2: Order Matching

When two orders are matched (e.g., John and Emily), we want to link them together with a common matching ID. This helps us track which orders in our Ledger were matched with each other.

Phase 3: Settling the Matched Orders

Finally, we need to execute the exchange of assets between the matched orders - John & Emily. Here’s how we do it:
  1. We need to commit these transactions to move the funds to escrow.
  2. Then we create new transactions to move funds from escrow to the respective recipient balances (John’s MATIC balance and Emily’s ETH balance).

Best Practices

  1. Balance Validation: Always verify available balances before creating orders. Remember to consider both actual balances and inflight amounts to prevent over-commitment of funds.
  2. Transaction References: Always use meaningful reference prefixes (‘order_’, ‘match_’, ‘settlement_’) combined with UUIDs. This makes it easier to track and audit transactions throughout their lifecycle.
  3. Metadata Management: Ensure metadata consistency across all related transactions. The matching ID should flow through all transactions involved in an exchange, creating a clear chain of linked operations.
  4. Error Handling: Implement comprehensive error handling at each step. If any part of the process fails, you need to be able to identify where the failure occurred and handle it appropriately.

Additional Considerations

  1. Rate Limiting: Consider implementing rate limits for order creation to prevent system overload and potential abuse. See the server and security configuration docs.
  2. Currency Precision: Different cryptocurrencies might require different precision settings. Ensure to use consistent precision value per currency in your Ledger.
  3. Escrow Management: Regularly audit escrow accounts to ensure they zero out correctly after settlements. Any remaining balance could indicate failed or incomplete settlements.