-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard-api.js
More file actions
159 lines (140 loc) · 5.5 KB
/
dashboard-api.js
File metadata and controls
159 lines (140 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const http = require('http');
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const PORT = 7893;
const KAZ_DIR = '/Users/services/kaz';
const MEMORY_DIR = path.join(KAZ_DIR, 'memory');
function readFile(filepath) {
try {
return fs.readFileSync(filepath, 'utf8');
} catch { return null; }
}
function getKalshiData() {
try {
const raw = execSync(`cd ${KAZ_DIR} && python3 -c "
from kalshi_client import get_balance, api_request
import json
bal = get_balance()
positions = api_request('GET', '/portfolio/positions')
orders = api_request('GET', '/portfolio/orders')
print(json.dumps({
'balance_cents': bal.get('balance', 0),
'portfolio_value_cents': bal.get('portfolio_value', 0),
'positions': positions.get('market_positions', []),
'orders': [o for o in orders.get('orders', []) if o.get('status') == 'resting']
}))
"`, { timeout: 15000 }).toString().trim();
return JSON.parse(raw);
} catch (e) {
return { balance_cents: 0, portfolio_value_cents: 0, positions: [], orders: [], error: e.message };
}
}
function getGumroadData() {
try {
const creds = JSON.parse(fs.readFileSync('/Users/services/.kaz-credentials/gumroad.json', 'utf8'));
const token = creds.access_token;
const raw = execSync(`curl -s "https://api.gumroad.com/v2/products?access_token=${token}"`, { timeout: 10000 }).toString();
const data = JSON.parse(raw);
if (!data.success) return { products: [], sales_total: 0 };
const products = (data.products || []).map(p => ({
name: p.name,
price: p.price,
url: p.short_url || p.url,
sales_count: p.sales_count || 0,
revenue_cents: p.revenue || 0
}));
const salesRaw = execSync(`curl -s "https://api.gumroad.com/v2/sales?access_token=${token}"`, { timeout: 10000 }).toString();
const salesData = JSON.parse(salesRaw);
const salesTotal = (salesData.sales || []).reduce((sum, s) => sum + (parseFloat(s.price) || 0), 0);
return { products, sales_total_cents: Math.round(salesTotal * 100) };
} catch (e) {
return { products: [], sales_total_cents: 0, error: e.message };
}
}
function getPortfolio() {
const kalshi = getKalshiData();
const gumroad = getGumroadData();
const kalshiBalanceDollars = kalshi.balance_cents / 100;
const kalshiPortfolioDollars = kalshi.portfolio_value_cents / 100;
const gumroadRevenueDollars = gumroad.sales_total_cents / 100;
const totalValue = kalshiBalanceDollars + kalshiPortfolioDollars + gumroadRevenueDollars;
const startingCapital = 100;
const pnl = totalValue - startingCapital;
return {
updated_at: new Date().toISOString(),
starting_capital: startingCapital,
total_value: Math.round(totalValue * 100) / 100,
pnl: Math.round(pnl * 100) / 100,
pnl_pct: Math.round((pnl / startingCapital) * 10000) / 100,
accounts: {
kalshi: {
cash: kalshiBalanceDollars,
portfolio_value: kalshiPortfolioDollars,
total: Math.round((kalshiBalanceDollars + kalshiPortfolioDollars) * 100) / 100,
positions: kalshi.positions.map(p => ({
ticker: p.ticker,
side: p.side || 'yes',
quantity: parseInt(p.total_traded_fp || p.position_fp || '0'),
market_value_dollars: parseFloat(p.market_value_dollars || '0'),
realized_pnl_dollars: parseFloat(p.realized_pnl_dollars || '0')
})),
resting_orders: kalshi.orders.map(o => ({
ticker: o.ticker,
side: o.side,
remaining: parseInt(o.remaining_count_fp || '0'),
price_dollars: parseFloat(o.yes_price_dollars || o.no_price_dollars || '0')
}))
},
gumroad: {
revenue: gumroadRevenueDollars,
products: gumroad.products
}
},
revenue_streams: [
{
name: 'Kalshi Trading',
status: 'active',
description: 'Prediction market trading — CPI, Fed, political events',
value: Math.round((kalshiBalanceDollars + kalshiPortfolioDollars) * 100) / 100
},
{
name: 'Gumroad Products',
status: gumroad.products.length > 0 ? 'active' : 'pending_listing',
description: 'Digital products — AI Dev Toolkit ($29), Automation Cookbook ($39)',
value: gumroadRevenueDollars,
products: gumroad.products
}
]
};
}
const server = http.createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}
if (req.url === '/api/portfolio' && req.method === 'GET') {
try {
const data = getPortfolio();
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data, null, 2));
} catch (e) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: e.message }));
}
} else if (req.url === '/api/health' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'ok', service: 'kaz-dashboard-api', port: PORT }));
} else {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Not found', endpoints: ['/api/portfolio', '/api/health'] }));
}
});
server.listen(PORT, () => {
console.log(`kaz dashboard API running on http://localhost:${PORT}`);
console.log(`Portfolio endpoint: http://localhost:${PORT}/api/portfolio`);
});