---
# System prepended metadata

title: Order responsibilities

---

# Order responsibilities

- Create order
- Cancel order
- Match orders
- Get order list with filters (use qfilter)
- Get order details (order, trades, commission)
- Calculate commission of order
- Freeze assets of user upon order creation
- Unfreeze assets of user upon order failure
- Exchange traded assets between users
- Get orderbook snapshot
    - Return current asks and bids
    - Send asks and bids updates
- Handle order types (limit, stop, market)
    - Market and limit orders are the same for now
    - Stop limit orders only get added to order book when reach the stop price


## Create order flow

### Order Creation
```mermaid
sequenceDiagram
User ->> Order: Create order
Order ->> Order: Validate order
Order ->> Account: Check Balance (sync)
Order ->> Database: Save FreezeCredit Command
Order ->> Database: Save Order record
Order ->> User: Response
```

### After credit frozen
```mermaid
sequenceDiagram
Account ->> Order: FreezeCredit Reply
alt if success
    Order ->> Database: Save OrderCreated Event
    Order ->> OME: Store and Match
else
    Order ->> Database: Update order to failed
    Order ->> Database: Save UnfreezeCredit Command
    Order ->> Database: Save OrderFailed Event
end
```

### After OME Result
```mermaid
sequenceDiagram
OME ->> Order: Order Match Result
alt if match happened
    Order ->> Database: Save trade records
    Order ->> Database: Update order (status, filled amount, commission, ...)
    Order ->> Database: Save Trade records
    Order ->> Database: Save Exchange Command(s) (for account)
    Order ->> Database: Save Trade Event
    alt if order is filled completely
        OME ->> Order: Match Happened
        Order ->> Database: Save OrderCompleted Event
    else if order is partial
        Order ->> Database: Save OrderFill Event (include total and filled)
    end
else if failure
    Order ->> Database: Update order to failed
    Order ->> Database: Save OrderFailed Event
    Order ->> Database: Save UnfreezeCredit Command
end
```

## Cancel Order flow
```mermaid
sequenceDiagram
User ->> Order: Cancel Order
Order ->> Database: Check order status
alt if done
    Order ->> User: Cannot cancel and return done order
else if not filled/partially filled
    Order ->> Database: Set status to cancelled
    Order ->> OME: Remove from orderbook
    
    alt if OME return some unsaved match
    Order ->> Order: Save Unsaved matches (similar to above)
    end
    
    Order ->> Database: Save OrderCancelled Event (include partial fills)
    Order ->> User: Response
end
```

### Cancel order revised
```mermaid
sequenceDiagram
User ->> Order: Cancel
Order ->> Database: Check can cancel
Order ->> OME: Remove from OME
Order ->> Database: Set pending cancel
Order ->> Kafka: Cancel Command
Kafka ->> Order: Cancel Reply
d
```

### Topic names
- FreezeCredit: `account.freeze.command`
- FreezeCreditReply: `account.freeze.reply`
- UnfreezeCredit: `account.unfreeze.command`
- UnfreezeCreditReply: `account.unfreeze.reply`
- OrderCreated: `order.created.event`
- OrderFailed: `order.failed.event`
- OrderFill: `order.fill.event`
- OrderCompleted: `order.completed.event`
- Trade: `order.trade.event`


### Validations
- Fields
- Price should not be over 20% higher/lower than current market price (available via websocket - I have a client for it)
- Amount and price precision (decimal places) check to match  (avaialable via asset service)


### Near-future (v1.x) considerations
- If account service return freeze response too long, we may want to reject order
- Order expiration
- Order stalled (not expired, but stalled for more than a threshold)
- More Granular Event Topics. `order.validation.failed.event`, `order.freeze.failed.event`, ...
- More Granular Order Validation.
- Handle Limit Maker (Post Only) Order Type
- Handle taker vs maker orders
    - If order is matched immediately with one of current orderbook's orders, it's a taker order; otherwise a maker order