-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
246 lines (207 loc) · 6.38 KB
/
Copy pathserver.js
File metadata and controls
246 lines (207 loc) · 6.38 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// UnionLedger Server
const express = require('express');
const path = require('path');
const WebSocket = require('ws');
const http = require('http');
// Alerts + Wallet Ledger
const { sendSlackAlert, logEvent } = require('./backend/alerts');
const { getBalance, updateBalance } = require('./backend/utils/wallets');
const app = express();
const server = http.createServer(app);
// Middleware
app.use(express.json());
app.use(express.static('.'));
// Serve static HTML files
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'src', 'index.html'));
});
app.get('/dashboard', (req, res) => {
res.sendFile(path.join(__dirname, 'src', 'dashboard.html'));
});
app.get('/register', (req, res) => {
res.sendFile(path.join(__dirname, 'src', 'register.html'));
});
app.get('/transfer', (req, res) => {
res.sendFile(path.join(__dirname, 'src', 'transfer.html'));
});
app.get('/trading', (req, res) => {
res.sendFile(path.join(__dirname, 'src', 'trading.html'));
});
app.get('/audit', (req, res) => {
res.sendFile(path.join(__dirname, 'src', 'audit.html'));
});
// API Routes
const { verifyWallet, verifyKYC } = require('./backend/auth');
const { deposit, withdraw, transfer } = require('./backend/transactions');
const {
startBot,
stopBot,
getBotStatus,
getTradingHistory,
updateStrategy,
getBotInstance
} = require('./backend/tradingBot');
// Auth endpoints
app.post('/api/auth/wallet', async (req, res) => {
try {
const result = await verifyWallet(req.body.address);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.post('/api/auth/kyc', async (req, res) => {
try {
const result = await verifyKYC(req.body);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// Transaction endpoints
app.post('/api/tx/deposit', async (req, res) => {
try {
const { wallet, amount } = req.body;
const result = await deposit(wallet, amount);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.post('/api/tx/withdraw', async (req, res) => {
try {
const { wallet, amount } = req.body;
const result = await withdraw(wallet, amount);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.post('/api/tx/transfer', async (req, res) => {
try {
const { fromWallet, toWallet, amount } = req.body;
const result = await transfer(fromWallet, toWallet, amount);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// Internal wallet balance lookup
app.get('/api/wallet/balance/:wallet', (req, res) => {
const balance = getBalance(req.params.wallet);
res.json({ wallet: req.params.wallet, balance });
});
// Paystack Webhook
app.post('/api/paystack/webhook', async (req, res) => {
if (!verifyPaystackSignature(req)) {
return res.status(401).json({ error: 'Invalid signature' });
}
const event = req.body;
try {
logEvent("Paystack Webhook", `Event received: ${event.event}`);
sendSlackAlert(`Paystack event: ${event.event}`);
if (event.event === "charge.success") {
const email = event.data.customer.email;
const amount = event.data.amount / 100;
// Update internal wallet ledger
updateBalance(email, amount);
// Alerts
sendSlackAlert(`💰 Deposit credited: ${email} +₦${amount}`);
logEvent("Deposit", `${email} credited with ₦${amount}`);
}
res.status(200).json({ status: "ok" });
} catch (err) {
console.error('[Paystack] Webhook error:', err.message);
res.status(500).json({ error: 'Webhook processing failed' });
}
});
// Trading Bot endpoints
app.post('/api/trading/start', async (req, res) => {
try {
const result = await startBot();
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.post('/api/trading/stop', async (req, res) => {
try {
const result = await stopBot();
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.get('/api/trading/status', (req, res) => {
try {
const status = getBotStatus();
res.json(status);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.get('/api/trading/history', (req, res) => {
try {
const limit = parseInt(req.query.limit) || 50;
const history = getTradingHistory(limit);
res.json(history);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.post('/api/trading/strategy/:strategyKey', async (req, res) => {
try {
const { strategyKey } = req.params;
const result = updateStrategy(strategyKey, req.body);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
const PORT = process.env.PORT || 8080;
// WebSocket server for real-time trading updates
const wss = new WebSocket.Server({ server, path: '/trading-ws' });
wss.on('connection', (ws) => {
console.log('🔌 Trading WebSocket client connected');
// Send current bot status on connection
const status = getBotStatus();
ws.send(JSON.stringify({ type: 'status', data: status }));
ws.on('close', () => {
console.log('🔌 Trading WebSocket client disconnected');
});
});
// Setup real-time event listeners for trading bot
const botInstance = getBotInstance();
botInstance.on('marketUpdate', (data) => {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'marketUpdate', data }));
}
});
});
botInstance.on('tradeExecuted', (data) => {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'tradeExecuted', data }));
}
});
});
botInstance.on('botStarted', (data) => {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'botStarted', data }));
}
});
});
botInstance.on('botStopped', (data) => {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ type: 'botStopped', data }));
}
});
});
server.listen(PORT, () => {
console.log(`✅ UnionLedger server running on port ${PORT}`);
console.log(`🌐 Access the application at: http://localhost:${PORT}`);
console.log(`🤖 Trading WebSocket available at: ws://localhost:${PORT}/trading-ws`);
});