realtime tracker

This commit is contained in:
React User
2026-01-17 23:41:47 +00:00
parent 7c6219e8ce
commit 62dad43e26
4 changed files with 1261 additions and 240 deletions

View File

@@ -18,39 +18,36 @@ CREATE TABLE IF NOT EXISTS wallets (
INDEX idx_trade_count (trade_count)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Tracked wallet addresses from Hyperliquid trades';
-- Wallet PnL snapshots table (optional - for historical tracking)
CREATE TABLE IF NOT EXISTS wallet_pnl_snapshots (
-- Note: PnL is now calculated from trades table, so wallet_pnl_snapshots table is no longer used
-- Trades table to store individual trades for each wallet
CREATE TABLE IF NOT EXISTS trades (
id INT AUTO_INCREMENT PRIMARY KEY,
wallet_id INT NOT NULL,
wallet_address VARCHAR(66) NOT NULL,
pnl DECIMAL(30, 8) COMMENT 'Profit and Loss value',
account_value DECIMAL(30, 8) COMMENT 'Total account value',
unrealized_pnl DECIMAL(30, 8) COMMENT 'Unrealized PnL',
snapshot_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
entry_hash VARCHAR(66) NOT NULL UNIQUE COMMENT 'Unique identifier for the trade entry (transaction hash)',
entry_date TIMESTAMP NULL COMMENT 'When the position was opened',
close_date TIMESTAMP NULL COMMENT 'When the position was closed (NULL if still open)',
coin VARCHAR(20) NOT NULL COMMENT 'Trading pair/coin symbol',
amount DECIMAL(30, 8) NOT NULL COMMENT 'Trade size/amount',
entry_price DECIMAL(30, 8) NOT NULL COMMENT 'Price when position was opened',
close_price DECIMAL(30, 8) NULL COMMENT 'Price when position was closed (NULL if still open)',
direction ENUM('buy', 'sell') NOT NULL DEFAULT 'buy' COMMENT 'Trade direction: buy or sell',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (wallet_id) REFERENCES wallets(id) ON DELETE CASCADE,
INDEX idx_wallet_id (wallet_id),
INDEX idx_wallet_address (wallet_address),
INDEX idx_snapshot_at (snapshot_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Historical PnL snapshots for tracked wallets';
INDEX idx_entry_hash (entry_hash),
INDEX idx_coin (coin),
INDEX idx_entry_date (entry_date),
INDEX idx_close_date (close_date),
INDEX idx_direction (direction)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Individual trades for tracked wallets';
-- View for wallets with latest PnL
CREATE OR REPLACE VIEW wallets_with_latest_pnl AS
SELECT
w.id,
w.address,
w.first_seen_at,
w.last_seen_at,
w.trade_count,
wps.pnl,
wps.account_value,
wps.unrealized_pnl,
wps.snapshot_at as last_pnl_snapshot
FROM wallets w
LEFT JOIN wallet_pnl_snapshots wps ON w.id = wps.wallet_id
LEFT JOIN (
SELECT wallet_id, MAX(snapshot_at) as max_snapshot
FROM wallet_pnl_snapshots
GROUP BY wallet_id
) latest ON w.id = latest.wallet_id AND wps.snapshot_at = latest.max_snapshot;
-- Migration: Add direction column if it doesn't exist
ALTER TABLE trades ADD COLUMN IF NOT EXISTS direction ENUM('buy', 'sell') NOT NULL DEFAULT 'buy' COMMENT 'Trade direction: buy or sell' AFTER close_price;
ALTER TABLE trades ADD INDEX IF NOT EXISTS idx_direction (direction);
-- Note: PnL is now calculated on-demand from trades table, so the view is no longer needed