| [d8ce4e2] | 1 | # Use-case 0004 — Place market BUY order
|
|---|
| 2 |
|
|---|
| 3 | **Initiating actor:** Trader
|
|---|
| 4 |
|
|---|
| 5 | **Other actors:** Market Simulator (indirect — supplies the current price via `market_trades`).
|
|---|
| 6 |
|
|---|
| 7 | A Trader buys a crypto asset at the current market price. The operation touches five tables (`orders`, `users`, `holdings`, `transactions`, `market_trades`) and must either all succeed or all roll back.
|
|---|
| 8 |
|
|---|
| 9 | ## Scenario
|
|---|
| 10 |
|
|---|
| 11 | 1. Trader chooses "Place market BUY order".
|
|---|
| [ef1c1c7] | 12 | 2. System lists the active markets, numbered, with their latest price:
|
|---|
| [d8ce4e2] | 13 |
|
|---|
| 14 | ```sql
|
|---|
| [ef1c1c7] | 15 | SELECT m.id, c.id, c.symbol, m.quote_currency,
|
|---|
| 16 | COALESCE(lp.price, 0) AS price
|
|---|
| [d8ce4e2] | 17 | FROM project.markets m
|
|---|
| 18 | JOIN project.crypto c ON c.id = m.crypto_id
|
|---|
| 19 | LEFT JOIN project.v_latest_prices lp ON lp.market_id = m.id
|
|---|
| 20 | WHERE m.is_active = true
|
|---|
| 21 | ORDER BY c.symbol;
|
|---|
| 22 | ```
|
|---|
| [ef1c1c7] | 23 | 3. Trader picks the market by its number in the listed markets, e.g. `2` (BTC).
|
|---|
| 24 | 4. System takes the chosen row's market id and crypto id from the list (no lookup by
|
|---|
| 25 | symbol) and looks up the latest price:
|
|---|
| [d8ce4e2] | 26 |
|
|---|
| 27 | ```sql
|
|---|
| [ef1c1c7] | 28 | SELECT price FROM project.v_latest_prices WHERE market_id = $1;
|
|---|
| [d8ce4e2] | 29 | ```
|
|---|
| 30 | 5. Trader enters a quantity.
|
|---|
| 31 | 6. System computes notional = quantity × price, opens a transaction, and does:
|
|---|
| 32 |
|
|---|
| 33 | ```sql
|
|---|
| 34 | BEGIN;
|
|---|
| 35 |
|
|---|
| [9577c79] | 36 | -- (a) record intent — no trade has happened yet.
|
|---|
| [d8ce4e2] | 37 | INSERT INTO project.orders
|
|---|
| [9577c79] | 38 | (user_id, market_id, side, type, status, quantity, price)
|
|---|
| [d8ce4e2] | 39 | VALUES
|
|---|
| [9577c79] | 40 | ($user_id, $market_id, 'buy', 'market', 'open', $qty, $price)
|
|---|
| [d8ce4e2] | 41 | RETURNING id; -- captured as $order_id
|
|---|
| 42 |
|
|---|
| [9577c79] | 43 | -- (b) lock and check the user balance
|
|---|
| [d8ce4e2] | 44 | SELECT available_balance FROM project.users WHERE id = $user_id FOR UPDATE;
|
|---|
| 45 | -- abort if available_balance < notional
|
|---|
| 46 |
|
|---|
| [9577c79] | 47 | -- (c) move cash from available to invested. A buy never reserves crypto
|
|---|
| 48 | -- the way a sell does — it only ever adds to the position, so there
|
|---|
| 49 | -- is nothing on the holdings side to commit before settling.
|
|---|
| [d8ce4e2] | 50 | UPDATE project.users
|
|---|
| 51 | SET available_balance = available_balance - $notional,
|
|---|
| 52 | invested_balance = invested_balance + $notional,
|
|---|
| 53 | updated_at = now()
|
|---|
| 54 | WHERE id = $user_id;
|
|---|
| 55 |
|
|---|
| [9577c79] | 56 | -- (d) upsert holding with running weighted-average price:
|
|---|
| [d8ce4e2] | 57 | SELECT quantity, avg_price
|
|---|
| 58 | FROM project.holdings
|
|---|
| 59 | WHERE user_id = $user_id AND crypto_id = $crypto_id
|
|---|
| 60 | FOR UPDATE;
|
|---|
| 61 |
|
|---|
| 62 | -- Either INSERT (new holding) or UPDATE (existing), computing
|
|---|
| 63 | -- new_avg = (old_qty*old_avg + $qty*$price) / (old_qty + $qty)
|
|---|
| 64 |
|
|---|
| [9577c79] | 65 | -- (e) ledger entry
|
|---|
| [d8ce4e2] | 66 | INSERT INTO project.transactions
|
|---|
| 67 | (user_id, type, amount, currency, related_order, description)
|
|---|
| 68 | VALUES
|
|---|
| 69 | ($user_id, 'buy', -$notional, 'USD', $order_id, 'Market buy ...');
|
|---|
| 70 |
|
|---|
| [9577c79] | 71 | -- (f) record the resulting market trade
|
|---|
| [d8ce4e2] | 72 | INSERT INTO project.market_trades
|
|---|
| 73 | (market_id, executed_at, price, quantity, side, source)
|
|---|
| 74 | VALUES
|
|---|
| 75 | ($market_id, now(), $price, $qty, 'buy', 'user');
|
|---|
| 76 |
|
|---|
| [9577c79] | 77 | -- (g) settle the order itself — it has now actually been filled.
|
|---|
| 78 | UPDATE project.orders
|
|---|
| 79 | SET status = 'executed', executed_at = now()
|
|---|
| 80 | WHERE id = $order_id;
|
|---|
| 81 |
|
|---|
| [d8ce4e2] | 82 | COMMIT;
|
|---|
| 83 | ```
|
|---|
| 84 | 7. System confirms: `Order executed: buy 0.0100 BTC @ 67140.000000 (notional 671.4000 USD)`.
|
|---|
| 85 |
|
|---|
| 86 | ### Alternate flow 6a — insufficient funds
|
|---|
| 87 |
|
|---|
| 88 | If `available_balance < notional`, the entire transaction rolls back and system shows "Insufficient funds: need X, have Y."
|
|---|
| 89 |
|
|---|
| [ef1c1c7] | 90 | ### Alternate flow 3a — number not in the list
|
|---|
| [d8ce4e2] | 91 |
|
|---|
| [ef1c1c7] | 92 | If the entered number is not one of the listed market numbers, system shows "Invalid choice, enter a number from 1 to N." and returns to the authenticated menu without opening a transaction.
|
|---|