How to design a decentralized exchange
What’s Decentralized Exchange
A decentralized exchange (better known as a DEX) is a peer-to-peer marketplace where transactions occur directly between crypto traders. DEXs fulfill one of crypto’s core possibilities: fostering financial transactions that aren’t officiated by banks, brokers, payment processors, or any other kind of intermediary.
So a Dex will have the following features:
- Anonymous
- Many coin pair
- Peer-to-Peer exchange
Requirements
- User should be able to sell or buy coin pairs.
- User should get realtime updates of price changes.
- User should be able to see their own order history.
Goal of the system design
- High availability, Scalable
- Low latency
Design Overview
I prefer to use Domain-Driven Design(DDD) to differentiate each microservice.
Step 1. Roughly distinguish the various components and find the core component.
graph TD subgraph boundary ["Boundary"] subgraph trading ["Trading"] end subgraph user ["User"] end subgraph product ["Product"] end subgraph chia ["Chia"] end subgraph pubsub ["Pubsub"] end end style trading fill:#79c4f2
Step 2. Write the functions of each component.
graph TD subgraph boundary ["Boundary"] subgraph trading ["Trading"] order["Order Context"] matching["Matching Context"] end subgraph user ["User"] authentication["Authentication Context"] end subgraph product ["Product"] market["Market Context"] end subgraph chia ["Chia"] offer["Offer Context"] onchain["Onchain Context"] end subgraph pubsub ["Pubsub"] publish["Publish Context"] end end style trading fill:#79c4f2
Step 3. Connect the dependencies of each component.
graph TD subgraph boundary ["Boundary"] subgraph trading ["Trading"] order["Order Context"] matching["Matching Context"] end subgraph user ["User"] authentication["Authentication Context"] end subgraph product ["Product"] market["Market Context"] end subgraph chia ["Chia"] offer["Offer Context"] onchain["Onchain Context"] end subgraph pubsub ["Pubsub"] publish["Publish Context"] end end authentication-->order market-->order order-->matching order-->offer order-->publish matching-->publish matching-->onchain style trading fill:#79c4f2
Step 4. Define the kind of domain for each component.
graph TD subgraph boundary ["Boundary"] subgraph trading ["Trading (core subdobmain)"] order["Order Context"] matching["Matching Context"] end subgraph user ["User (support subdobmain)"] authentication["Authentication Context"] end subgraph product ["Product (support subdobmain)"] market["Market Context"] end subgraph chia ["Chia (generic subdobmain)"] offer["Offer Context"] onchain["Onchain Context"] end subgraph pubsub ["Pubsub (generic subdobmain)"] publish["Publish Context"] end end authentication-->order market-->order order-->matching order-->offer order-->publish matching-->publish matching-->onchain style trading fill:#79c4f2
And then we can get high level system diagram for our system.
System Overview
graph TD subgraph boundary ["Boundary"] subgraph user ["User"] login(Login) end subgraph product ["Product"] currency(Currency) market(Market) end subgraph trading ["Trading"] submit(Submit Order) matching(Matching Engine) end subgraph chia ["Chia"] offer(Parse offer) onchain(OnChain) end subgraph pubsub ["Pubsub"] publish(publish) end end login-->submit currency-->market market-->submit submit-->matching submit-->offer submit-->publish matching-->onchain matching-->publish style trading fill:#79c4f2
Components
- User: It’s an anonymous user component which only create an id for user exclude any identify personal information achieve decentralized system.
- Product:
- Currency: It’s a
Chia Asset Token
(CATs) or coin. - Market: It’s a coin pair to exchange different currencies for user.
- Currency: It’s a
- Trading: It’s a core component in DEX which handle whole transaction.
- Chia:
- Offer: We need a service can parse offer file content from Chia lightweight wallet generate offer.
- OnChain: We need a service to bundle many offer to Chia blockchain then the trading transaction before you can.
- Pubsub: It’s a message queue which publish order or any information to user.