source: docs/P3-UseCaseModel/UseCase0004.md@ 1549dae

main
Last change on this file since 1549dae was ef1c1c7, checked in by Stefan <trsunovstefan@…>, 6 days ago

Wiki docs, phase 6 and phase 7 added

  • Property mode set to 100644
File size: 3.5 KB
Line 
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
7A 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
111. Trader chooses "Place market BUY order".
122. System lists the active markets, numbered, with their latest price:
13
14 ```sql
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 ```
233. Trader picks the market by its number in the listed markets, e.g. `2` (BTC).
244. 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:
26
27 ```sql
28 SELECT price FROM project.v_latest_prices WHERE market_id = $1;
29 ```
305. Trader enters a quantity.
316. System computes notional = quantity × price, opens a transaction, and does:
32
33 ```sql
34 BEGIN;
35
36 -- (a) record intent — no trade has happened yet.
37 INSERT INTO project.orders
38 (user_id, market_id, side, type, status, quantity, price)
39 VALUES
40 ($user_id, $market_id, 'buy', 'market', 'open', $qty, $price)
41 RETURNING id; -- captured as $order_id
42
43 -- (b) lock and check the user balance
44 SELECT available_balance FROM project.users WHERE id = $user_id FOR UPDATE;
45 -- abort if available_balance < notional
46
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.
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
56 -- (d) upsert holding with running weighted-average price:
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
65 -- (e) ledger entry
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
71 -- (f) record the resulting market trade
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
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
82 COMMIT;
83 ```
847. System confirms: `Order executed: buy 0.0100 BTC @ 67140.000000 (notional 671.4000 USD)`.
85
86### Alternate flow 6a — insufficient funds
87
88If `available_balance < notional`, the entire transaction rolls back and system shows "Insufficient funds: need X, have Y."
89
90### Alternate flow 3a — number not in the list
91
92If 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.
Note: See TracBrowser for help on using the repository browser.