Back to articles
Technical

Inside Keeta's Atomic Swap Orderbook

LBLightbeam Team
12 min read
Bull — Lightbeam

The Lightbeam orderbook is a sophisticated decentralized exchange (DEX) mechanism built on the Keeta Network blockchain. Unlike traditional orderbooks that rely on centralized matching engines, Lightbeam uses atomic swaps combined with storage account pools and order chaining to enable trustless, non-custodial trading with advanced features like grid trading and liquidity provision.

1. Atomic Swaps: The Foundation

The Double Swap Pattern

At the heart of the Lightbeam orderbook is a double swap pattern. Each user creates an atomic swap with the DEX wallet (not directly with each other):

  1. Maker (Limit Order): Creates an atomic swap with the DEX wallet
    • SEND operation: Transfers their tokens to the DEX wallet
    • RECEIVE operation: Specifies the exact amount and type of tokens they expect back from the DEX wallet
  2. Taker (Limit Order): Creates a separate atomic swap with the DEX wallet
    • SEND operation: Transfers their tokens to the DEX wallet
    • RECEIVE operation: Specifies the exact amount and type of tokens they expect back from the DEX wallet
  3. DEX Wallet Matching: The DEX wallet signs and executes both swaps simultaneously
    • Acts as the counterparty for both sides
    • Matches compatible limit orders at the same price
    • Executes both atomic swaps together to complete the orderbook trade

This pattern ensures that:

  • Users never trade directly with each other
  • The DEX wallet cannot steal funds (atomic swaps guarantee exact amounts or nothing)
  • Orders sit in the orderbook as unsigned blocks until matched
  • When matched, the DEX wallet completes both swaps atomically
Atomic Swap Two-Block Pattern

Why Atomic Swaps Are Extremely Safe

Atomic swaps are fundamentally safer than smart contract-based exchanges because they operate at the blockchain protocol level, not in application code:

  • All-or-Nothing Execution: The swap completes exactly as specified or doesn't execute at all. No partial fills, no approximations, no rounding errors.
  • No Smart Contract Risk: Unlike traditional DEXs that rely on potentially buggy smart contracts, atomic swaps use native blockchain operations. This eliminates entire categories of vulnerabilities: reentrancy attacks, logic bugs, admin key compromises.
  • Exact Amount Guarantees: Both SEND and RECEIVE operations must complete with precisely the specified amounts. Even a single wei/sat difference causes the entire transaction to fail. No slippage, no "minimum receive" manipulation.
  • Complete Trustlessness: You never give up custody until you receive exactly what you want. No need to trust the DEX, smart contract developer, or counterparty—the blockchain enforces swap terms through cryptographic verification.

Atomic swaps provide mathematical certainty rather than contractual promises. Your tokens can only move if you simultaneously receive exactly the tokens you specified—enforced by the blockchain's fundamental rules, not application code.

What Are Storage Accounts?

Storage accounts are dedicated blockchain accounts that serve as escrow addresses for orders. Each order gets its own storage account that:

  • Holds the tokens being offered for sale
  • Contains the pre-signed atomic swap block ready for execution
  • Acts as the escrow address in the orderbook

2. Chained Atomic Swaps: Linking Orders Together

Chained atomic swaps enable self-funding trading sequences that execute automatically without manual intervention. By linking orders together through the forward mechanism, traders can create sophisticated strategies like grid trading and automated market making.

Forward Mechanism

Atomic swaps support an optional forward address parameter (see Keeta docs). When a swap completes, received tokens are automatically sent to the specified forward address instead of the order creator. This happens atomically—either the entire swap completes with the forward, or nothing happens at all.

How Swap Chains Work

A swap chain is a sequence of orders where each order's received tokens automatically fund the next order in the chain:

  1. Order 0 executes and receives tokens → automatically forwards to Order 1's storage account
  2. Order 1 is now funded and executable → when it executes, forwards to Order 2
  3. Order 2 continues the pattern → and so on through the chain

Each order in a chain has:

  • swap_chain_id: UUID shared by all orders in the sequence
  • swap_chain_index: Sequential position (0, 1, 2, ...)
  • forward_to_address: Next order's storage account (null for the final order)
  • escrow_address: This order's storage account

Why Use Chains?

Chaining provides critical advantages:

  • Automatic Capital Rotation: Profits from one trade immediately fund the next
  • Reduced Costs: Create the entire sequence once instead of recreating orders after each fill
  • Guaranteed Order: The blockchain enforces execution sequence
  • Self-Sustaining: Strategies continue automatically as market conditions trigger orders

Example: Oscillating Market Trading

A trader expects a token to oscillate between 0.95 and 1.05. They create a 6-order chain that automatically buys low and sells high:

Oscillating Market Trading Chain
  • Price drops to 0.95 → Order 0 executes, forwards 100 TRADE to storage1
  • Price rises to 1.05 → Order 1 executes, forwards 105 KTA to storage2
  • Price drops to 0.95 → Order 2 executes with increased capital
  • Pattern repeats, profits compound automatically

After all orders complete, the final order returns accumulated tokens (plus profits) to the trader.

3. Time Extensions: Long-Lived Orders

The Expiration Problem

On Keeta Network, blockchain blocks have a 5-minute voting window. This means any unsigned atomic swap block must be submitted and voted on within 5 minutes, or it becomes invalid. For orderbook orders that need to remain open for hours or days, this presents a challenge.

How Time Extensions Work

Lightbeam solves this using time extension blocks—pre-computed blockchain blocks with future timestamps that keep orders alive indefinitely.

Initial Order Creation: When you create an order, the wallet automatically generates 360 extension blocks (one every 4 minutes) bundled together in a single file. This gives your order an initial lifespan of 24 hours. These blocks are uploaded to cloud storage and registered with the orderbook, which automatically submits them as your order approaches expiration.

Automatic Background Extension: The wallet runs a background service that continuously extends your orders up to a 7-day maximum. Every few minutes, the background process checks which orders are getting close to expiration (within 3 days). For each expiring order, it generates another batch of 360 blocks—adding another 24 hours to the order's life. This continues automatically until the order reaches the maximum 7-day lifetime.

User Control: You can enable or disable automatic extensions in the wallet settings. When disabled, orders will naturally expire after their current extension blocks run out (within 24 hours of the last extension).

Why This Works: Without extension blocks, an order would expire after just 5 minutes. With extensions, your order can stay active for up to 7 days, automatically renewed in the background without any manual intervention.

4. Liquidity Pools

What Are Liquidity Pools?

Lightbeam's Automatic Liquidity system lets you provide concentrated, on-chain liquidity on any trading pair with a custom distribution. Unlike traditional AMM pools that spread your capital inefficiently across all price ranges, Lightbeam lets you target specific price levels for maximum profitability.

Key benefits:

  • Automated Profit Generation: Buy low, sell high on autopilot—capturing profits on each market swing
  • No Impermanent Loss: Orders only execute at your chosen prices
  • Capital Efficiency: 100% of your capital works at targeted price levels
  • Continuous Operation: Orders automatically flip after execution to keep earning

How Chained Orders Enable Continuous Operation

Each price level uses order chaining to create a self-sustaining profit cycle. The first order in the chain executes and forwards tokens to the next order, which flips to the opposite side and forwards to the third. This pattern continues through the chain, alternating buy/sell, until the final order returns accumulated tokens (plus profits) back to you.

This chain structure means your liquidity pool continues operating automatically, compounding profits with each complete cycle, until you choose to cancel it.

For a detailed guide on setting up your own liquidity pools, see the Automatic Liquidity documentation.

Key Takeaways

  • Orders are pre-signed atomic swap blocks held in dedicated storage accounts
  • Forwarding enables automatic capital rotation through order chains
  • Time extension blocks keep orders alive for up to 7 days automatically
  • Liquidity pools use chained orders to create self-sustaining profit cycles
  • The DEX wallet executes matched swaps by signing pre-computed blocks

This design enables sophisticated trading strategies while maintaining the security guarantees of atomic blockchain operations.

The Lightbeam orderbook demonstrates how blockchain primitives (accounts, blocks, atomic operations) can be composed into complex financial instruments without sacrificing security or decentralization. Together, these components create a non-custodial DEX where users maintain complete control of their funds while enjoying advanced trading features typically found only on centralized exchanges.