⏰ Time:
🌡️ Temp:
Loading...
📌 แผนผังระบบ + Flow การทำงาน + ตัวอย่าง Database Schema + Backend Flow + LND Integration ให้เห็นภาพรวมว่า ระบบ Custodial BTC/Lightning
โดย puk
•2025-09-11 06:05
แผนผังระบบ + Flow การทำงาน + ตัวอย่าง Database Schema + Backend Flow + LND Integration ให้เห็นภาพรวมว่า ระบบ Custodial BTC/Lightning สำหรับร้านค้า จะทำงานยังไงแบบอัตโนมัติ
1️⃣ System Architecture (High-level)
[ลูกค้า/ร้านค้า]
│
▼
[Frontend Dashboard]
- เติมเงิน (Cash / Bank / BTC / Lightning)
- ดูยอดเครดิต
- สร้าง invoice / request withdrawal
│
▼
[Backend API / Payment Listener]
- ตรวจสอบเงินฝาก (Bank API / Payment Gateway / Lightning)
- เชื่อม Exchange API สำหรับซื้อ BTC อัตโนมัติ
- ควบคุมวงเงิน, daily limit, fraud detection
- ส่งคำสั่งเข้าระบบ Ledger
│
▼
[Ledger DB]
- users, wallets, deposits, withdrawals, invoices
- pending / confirmed balances
│
├──────────────┐
▼ ▼
[Bitcoin Core Node] [LND Node]
- รับ BTC จาก Exchange - เติมเครดิต Lightning
- รอ confirmations - Update Ledger balance
- ส่ง withdrawal - สร้าง invoices, send payments
│
▼
[Frontend / Admin Panel]
- ร้านค้าเห็นยอดเครดิต
- Admin ตรวจสอบ KYC/AML, audit, transaction
2️⃣ Database Schema ตัวอย่าง
-- Users / ร้านค้า
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE,
kyc_status BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP DEFAULT NOW()
);
-- Wallet / Lightning credit
CREATE TABLE wallets (
wallet_id SERIAL PRIMARY KEY,
user_id INT REFERENCES users(user_id),
btc_balance NUMERIC(18,8) DEFAULT 0,
lightning_balance NUMERIC(18,8) DEFAULT 0,
created_at TIMESTAMP DEFAULT NOW()
);
-- Deposits
CREATE TABLE deposits (
deposit_id SERIAL PRIMARY KEY,
user_id INT REFERENCES users(user_id),
amount NUMERIC(18,8),
currency VARCHAR(10),
method VARCHAR(20), -- BTC / Lightning / Bank
status VARCHAR(20) DEFAULT 'pending',
tx_id VARCHAR(100),
created_at TIMESTAMP DEFAULT NOW(),
confirmed_at TIMESTAMP
);
-- Withdrawals
CREATE TABLE withdrawals (
withdrawal_id SERIAL PRIMARY KEY,
user_id INT REFERENCES users(user_id),
amount NUMERIC(18,8),
currency VARCHAR(10),
method VARCHAR(20),
status VARCHAR(20) DEFAULT 'pending',
tx_id VARCHAR(100),
created_at TIMESTAMP DEFAULT NOW(),
confirmed_at TIMESTAMP
);
-- Invoices (Lightning)
CREATE TABLE invoices (
invoice_id SERIAL PRIMARY KEY,
user_id INT REFERENCES users(user_id),
lnd_invoice TEXT,
amount NUMERIC(18,8),
status VARCHAR(20) DEFAULT 'unpaid',
created_at TIMESTAMP DEFAULT NOW(),
paid_at TIMESTAMP
);
3️⃣ Backend Flow (Python-like Pseudocode)
# Payment Listener
def handle_new_deposit(deposit):
# 1. ตรวจสอบ method
if deposit.method == "bank":
# เชื่อม API ธนาคาร -> ตรวจสอบเงินเข้า
confirm_bank_payment(deposit)
# ซื้อ BTC จาก Exchange
btc_amount = exchange_buy_btc(deposit.amount)
# ส่ง BTC เข้าระบบ Bitcoin Core
txid = bitcoin_core_send_to_hotwallet(btc_amount)
update_deposit(deposit.id, status="confirmed", tx_id=txid)
elif deposit.method == "btc":
# ตรวจสอบ BTC confirmations
wait_for_confirmations(deposit.tx_id)
update_deposit_status(deposit, "confirmed")
elif deposit.method == "lightning":
# ตรวจสอบ payment settled
update_deposit_status(deposit, "confirmed")
# Update Wallet Balance / Ledger
credit_user_wallet(deposit.user_id, deposit.amount, deposit.method)
# Withdrawal Flow
def process_withdrawal(withdrawal):
if withdrawal.method == "lightning":
lncli_send(withdrawal.lnd_invoice, withdrawal.amount)
elif withdrawal.method == "btc":
bitcoin_core_send(withdrawal.address, withdrawal.amount)
update_withdrawal_status(withdrawal.id, "completed")
4️⃣ LND Integration (Auto Credit)
# Subscribe invoices
lncli subscribeinvoices | while read invoice; do
if invoice.settled:
user_id = get_user_by_invoice(invoice.r_hash)
credit_user_wallet(user_id, invoice.amt_paid, method="lightning")
mark_invoice_paid(invoice.r_hash)
done
Backend จะ listen Lightning invoices ทันที → อัปเดต balance
สำหรับ on-chain BTC → ใช้ walletnotify / RPC poll รอ confirmations → อัปเดต ledger
5️⃣ Automation / Cron / Tasks
Exchange purchase task: ตรวจสอบ deposits fiat ทุก 1–5 นาที → ซื้อ BTC
Confirm BTC task: ตรวจสอบ confirmations ทุก 1–5 นาที → update ledger
Withdrawals task: ตรวจสอบ pending withdrawals → ส่ง BTC / Lightning
Fraud / Limit check: ตรวจสอบ pattern ธุรกรรม → flag suspicious
6️⃣ Security & Compliance
Hot wallet / Cold wallet separation
Multi-sig withdrawal
KYC / AML validation ก่อนอนุมัติวงเงิน
Audit logs ครบทุก event
TLS + JWT / API Key สำหรับเชื่อม frontend / backend
💡 Tip:
Lightning → Near-instant credit
BTC on-chain → รอ 1–3 confirmations
Fiat → BTC → LND → Auto credit
Frontend update balance real-time ผ่าน websocket / push notification