flowchart TD subgraph REG["Registration"] A["User registers: username + password"] --> B["Server derives a new P2WPKH address\n(BIP84, path m/84'/746'/0'/0/index)\nmaster xprv encrypted at rest"] B --> C["Address linked to the user profile in the DB"] end subgraph DEP["Balance top-up"] C --> D["User sends PLM to their dedicated address"] D --> E["ElectrumClient/SPV monitors the address\n(subscribe scripthash)"] E --> F{"Tx confirmed\n(1 confirmation)?"} F -- No --> E F -- Yes --> G["User balance credited in the DB\n(balance = confirmed UTXOs on the address)"] end subgraph PLAY["Bet"] G --> H{"User confirms bet purchase?\n(fixed cost: 10 PLM per round,\nmax 1 active bet at a time,\nacquires per-user DB lock shared with WITHDRAW)"} H -- No --> G H -- "Yes (balance >= bet cost)" --> I["Server builds PSBT:\nuser address -> pool address\n(bet cost) + change -> user address\nfee ~1 sat/vB deducted from the bet amount"] I --> J["Server signs with the user's derived key"] J --> K["Broadcast tx to the network"] K --> L{"Tx confirmed\n(1 confirmation)?"} L -- "No (timeout)" --> K2["Fee bump (RBF) and rebroadcast"] K2 --> K L -- Yes --> M["User registered as a participant\nin the current round (with bet amount)"] end subgraph DRAW["Periodic draw"] N["Round timer: every X minutes (configurable, default 10)"] --> O{"Are there bets\nalready broadcast but not yet confirmed?"} O -- Yes --> O O -- No --> O2["Close current round"] O2 --> P["List of round participants\n(user address + bet amount),\nordered by broadcast timestamp\n(tie-break for same-block confirmations)"] P --> Q{"Are there participants?"} Q -- No --> N Q -- Yes --> R["Draw winner (simple v1 algorithm):\n1. wait for the first block confirmed after round closing\n2. seed = block hash (hex -> integer)\n3. index = seed mod participant_count\n4. winner = participants[index]\n(anyone can recompute and verify it;\nalgorithm replaceable in the future)"] R --> S["Compute total round prize pool\n(sum of confirmed deposits to the pool address)"] S --> T["70% of the prize pool - payout tx fee\n-> winner's deposit address"] S --> U["30% of the prize pool (unchanged)\n-> fee address (configurable)"] T --> V["Payout tx signed with\nthe pool address key"] U --> V V --> V2{"Tx confirmed\n(1 confirmation)?"} V2 -- "No (timeout)" --> V3["Fee bump (RBF) and rebroadcast"] V3 --> V2 V2 -- Yes --> W["Log round\n(winner, amount, txid) for audit"] W --> N end subgraph WITHDRAW["Withdrawal (simple v1)"] G --> X["User requests withdrawal:\nexternal address + amount <= balance\n(min 1 PLM, acquires per-user DB lock\nshared with PLAY)"] X --> Y["Server builds and signs PSBT:\nuser address -> external address\n+ optional change -> user address\nfee deducted from the withdrawn amount"] Y --> Z["Broadcast + wait for 1 confirmation\n(same RBF-on-timeout pattern)"] Z --> G end M --> N