| [ef1c1c7] | 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".
|
|---|
| 12 | 2. System lists the active markets, numbered, with their latest price:
|
|---|
| 13 |
|
|---|
| 14 | {{{
|
|---|
| 15 | SELECT m.id, c.id, c.symbol, m.quote_currency,
|
|---|
| 16 | COALESCE(lp.price, 0) AS price
|
|---|
| 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 | }}}
|
|---|
| 23 |
|
|---|
| 24 | 3. Trader picks the market by its number in the listed markets, e.g. `2` (BTC).
|
|---|
| 25 | 4. System takes the chosen row's market id and crypto id from the list (no lookup by
|
|---|
| 26 | symbol) and looks up the latest price:
|
|---|
| 27 |
|
|---|
| 28 | {{{
|
|---|
| 29 | SELECT price FROM project.v_latest_prices WHERE market_id = $1;
|
|---|
| 30 | }}}
|
|---|
| 31 |
|
|---|
| 32 | 5. Trader enters a quantity.
|
|---|
| 33 | 6. System computes notional = quantity × price, opens a transaction, and does:
|
|---|
| 34 |
|
|---|
| 35 | {{{
|
|---|
| 36 | BEGIN;
|
|---|
| 37 |
|
|---|
| 38 | -- (a) record intent — no trade has happened yet.
|
|---|
| 39 | INSERT INTO project.orders
|
|---|
| 40 | (user_id, market_id, side, type, status, quantity, price)
|
|---|
| 41 | VALUES
|
|---|
| 42 | ($user_id, $market_id, 'buy', 'market', 'open', $qty, $price)
|
|---|
| 43 | RETURNING id; -- captured as $order_id
|
|---|
| 44 |
|
|---|
| 45 | -- (b) lock and check the user balance
|
|---|
| 46 | SELECT available_balance FROM project.users WHERE id = $user_id FOR UPDATE;
|
|---|
| 47 | -- abort if available_balance < notional
|
|---|
| 48 |
|
|---|
| 49 | -- (c) move cash from available to invested. A buy never reserves crypto
|
|---|
| 50 | -- the way a sell does — it only ever adds to the position, so there
|
|---|
| 51 | -- is nothing on the holdings side to commit before settling.
|
|---|
| 52 | UPDATE project.users
|
|---|
| 53 | SET available_balance = available_balance - $notional,
|
|---|
| 54 | invested_balance = invested_balance + $notional,
|
|---|
| 55 | updated_at = now()
|
|---|
| 56 | WHERE id = $user_id;
|
|---|
| 57 |
|
|---|
| 58 | -- (d) upsert holding with running weighted-average price:
|
|---|
| 59 | SELECT quantity, avg_price
|
|---|
| 60 | FROM project.holdings
|
|---|
| 61 | WHERE user_id = $user_id AND crypto_id = $crypto_id
|
|---|
| 62 | FOR UPDATE;
|
|---|
| 63 |
|
|---|
| 64 | -- Either INSERT (new holding) or UPDATE (existing), computing
|
|---|
| 65 | -- new_avg = (old_qty*old_avg + $qty*$price) / (old_qty + $qty)
|
|---|
| 66 |
|
|---|
| 67 | -- (e) ledger entry
|
|---|
| 68 | INSERT INTO project.transactions
|
|---|
| 69 | (user_id, type, amount, currency, related_order, description)
|
|---|
| 70 | VALUES
|
|---|
| 71 | ($user_id, 'buy', -$notional, 'USD', $order_id, 'Market buy ...');
|
|---|
| 72 |
|
|---|
| 73 | -- (f) record the resulting market trade
|
|---|
| 74 | INSERT INTO project.market_trades
|
|---|
| 75 | (market_id, executed_at, price, quantity, side, source)
|
|---|
| 76 | VALUES
|
|---|
| 77 | ($market_id, now(), $price, $qty, 'buy', 'user');
|
|---|
| 78 |
|
|---|
| 79 | -- (g) settle the order itself — it has now actually been filled.
|
|---|
| 80 | UPDATE project.orders
|
|---|
| 81 | SET status = 'executed', executed_at = now()
|
|---|
| 82 | WHERE id = $order_id;
|
|---|
| 83 |
|
|---|
| 84 | COMMIT;
|
|---|
| 85 | }}}
|
|---|
| 86 |
|
|---|
| 87 | 7. System confirms: `Order executed: buy 0.0100 BTC @ 67140.000000 (notional 671.4000 USD)`.
|
|---|
| 88 |
|
|---|
| 89 | === Alternate flow 6a — insufficient funds ===
|
|---|
| 90 |
|
|---|
| 91 | If `available_balance < notional`, the entire transaction rolls back and system shows "Insufficient funds: need X, have Y."
|
|---|
| 92 |
|
|---|
| 93 | === Alternate flow 3a — number not in the list ===
|
|---|
| 94 |
|
|---|
| 95 | 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.
|
|---|