forked from Ethereal-Future/FuTuRe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar.js
More file actions
350 lines (320 loc) · 10.8 KB
/
Copy pathstellar.js
File metadata and controls
350 lines (320 loc) · 10.8 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import express from 'express';
import * as StellarSDK from '@stellar/stellar-sdk';
import * as StellarService from '../services/stellar.js';
import * as AMMService from '../services/amm.js';
import { getRate, getAllRates, convert } from '../services/exchangeRate.js';
import { broadcastToAccount } from '../services/websocket.js';
import { validate, rules } from '../middleware/validate.js';
import { SUPPORTED_ASSETS, getIssuer } from '../config/assets.js';
import { dispatchEvent } from '../webhooks/dispatcher.js';
import { cacheMiddleware } from '../middleware/cache.js';
import { keys as cacheKeys, TTL, invalidateBalance } from '../cache/appCache.js';
const router = express.Router();
/**
* @swagger
* /api/stellar/account/create:
* post:
* summary: Create a new Stellar account
* description: Generates a new random keypair for a Stellar account.
* tags: [Stellar]
* responses:
* 200:
* description: Account created successfully
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Account'
* 500:
* description: Server error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
router.post('/account/create', async (req, res) => {
try {
const account = await StellarService.createAccount();
res.json(account);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
router.post('/account/import', rules.importAccount, validate, async (req, res) => {
try {
const { secretKey } = req.body;
const keypair = StellarSDK.Keypair.fromSecret(secretKey);
const publicKey = keypair.publicKey();
const balance = await StellarService.getBalance(publicKey);
res.json({ publicKey, secretKey, balances: balance.balances });
} catch (error) {
res.status(400).json({ error: 'Invalid secret key or account not found on network' });
}
});
/**
* @swagger
* /api/stellar/account/{publicKey}:
* get:
* summary: Get account balance
* description: Retrieves the balance for a given Stellar public key.
* tags: [Stellar]
* parameters:
* - in: path
* name: publicKey
* required: true
* schema:
* type: string
* description: The public key of the account to check.
* responses:
* 200:
* description: Balance retrieved successfully
* content:
* application/json:
* schema:
* type: array
* items:
* $ref: '#/components/schemas/Balance'
* 500:
* description: Server error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
router.get('/account/:publicKey', rules.publicKeyParam, validate,
cacheMiddleware(TTL.BALANCE, (req) => cacheKeys.balance(req.params.publicKey)),
async (req, res) => {
try {
const balance = await StellarService.getBalance(req.params.publicKey);
res.json(balance);
} catch (error) {
res.status(500).json({ error: error.message });
}
}
);
/**
* @swagger
* /api/stellar/payment/send:
* post:
* summary: Send a payment
* description: Sends a payment from one Stellar account to another.
* tags: [Stellar]
* requestBody:
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/PaymentRequest'
* responses:
* 200:
* description: Payment sent successfully
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/PaymentResult'
* 400:
* description: Invalid request
* 500:
* description: Server error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
router.post('/payment/send', rules.sendPayment, validate, async (req, res) => {
try {
const { sourceSecret, destination, amount, assetCode } = req.body;
const result = await StellarService.sendPayment(sourceSecret, destination, amount, assetCode);
const notification = { type: 'transaction', hash: result.hash, amount, assetCode: assetCode || 'XLM', timestamp: Date.now() };
// Notify sender's updated balance + tx notification
const senderKey = StellarSDK.Keypair.fromSecret(sourceSecret).publicKey();
const senderBalance = await StellarService.getBalance(senderKey);
broadcastToAccount(senderKey, { ...notification, direction: 'sent', balance: senderBalance.balances });
dispatchEvent(senderKey, 'payment_sent', { hash: result.hash, amount, assetCode: assetCode || 'XLM', destination });
// Invalidate cached balances for sender and recipient
await invalidateBalance(senderKey);
await invalidateBalance(destination);
// Notify recipient of incoming tx + updated balance
try {
const recipientBalance = await StellarService.getBalance(destination);
broadcastToAccount(destination, { ...notification, direction: 'received', balance: recipientBalance.balances });
dispatchEvent(destination, 'payment_received', { hash: result.hash, amount, assetCode: assetCode || 'XLM', source: senderKey });
} catch (_) {}
res.json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
/**
* @swagger
* /api/stellar/exchange-rate/{from}/{to}:
* get:
* summary: Get exchange rate
* description: Retrieves the exchange rate between two assets on the Stellar network.
* tags: [Stellar]
* parameters:
* - in: path
* name: from
* required: true
* schema:
* type: string
* description: The source asset code.
* - in: path
* name: to
* required: true
* schema:
* type: string
* description: The target asset code.
* responses:
* 200:
* description: Exchange rate retrieved successfully
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ExchangeRate'
* 500:
* description: Server error
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/
router.get('/account/:publicKey/transactions', rules.publicKeyParam, validate, async (req, res) => {
try {
const { cursor, limit, type, dateFrom, dateTo } = req.query;
const result = await StellarService.getTransactions(req.params.publicKey, {
cursor,
limit: limit ? Math.min(parseInt(limit), 50) : 10,
type,
dateFrom,
dateTo,
});
res.json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
router.get('/fee-stats', cacheMiddleware(TTL.FEE_STATS, () => cacheKeys.feeStats()), async (req, res) => {
try {
res.json(await StellarService.getFeeStats());
} catch (error) {
res.status(500).json({ error: error.message });
}
});
router.get('/exchange-rate/:from/:to', rules.assetCodeParams, validate,
cacheMiddleware(TTL.RATE, (req) => cacheKeys.rate(req.params.from, req.params.to)),
async (req, res) => {
try {
const { from, to } = req.params;
const rate = await getRate(from, to);
if (rate === null) {
return res.status(503).json({ error: `Exchange rate unavailable for ${from}/${to}: no liquidity in orderbook` });
}
res.json({ from, to, rate });
} catch (error) {
res.status(500).json({ error: error.message });
}
}
);
// All supported pair rates in one call
router.get('/rates', async (req, res) => {
try {
const rates = await getAllRates();
res.json({ rates });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Convert an amount between assets
router.get('/convert/:from/:to/:amount', rules.assetCodeParams, validate, async (req, res) => {
try {
const amount = parseFloat(req.params.amount);
if (!isFinite(amount) || amount <= 0) return res.status(422).json({ error: 'Invalid amount' });
const result = await convert(amount, req.params.from, req.params.to);
res.json({ from: req.params.from, to: req.params.to, amount, converted: result });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
router.get('/network/status', async (req, res) => {
try {
const status = await StellarService.getNetworkStatus();
res.json(status);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
router.post('/amm/pools/register', (req, res) => {
try {
res.json(AMMService.registerPool(req.body));
} catch (error) {
res.status(400).json({ error: error.message });
}
});
router.get('/amm/pools/:poolId', (req, res) => {
try {
res.json(AMMService.getPoolState(req.params.poolId));
} catch (error) {
res.status(404).json({ error: error.message });
}
});
router.post('/amm/swap', (req, res) => {
try {
res.json(AMMService.executeSwap(req.body));
} catch (error) {
res.status(400).json({ error: error.message });
}
});
router.get('/amm/arbitrage/:assetA/:assetB', (req, res) => {
const opportunities = AMMService.detectArbitrageOpportunities([req.params.assetA, req.params.assetB]);
res.json({ opportunities });
});
router.post('/amm/strategies/run', (req, res) => {
try {
res.json(AMMService.runAutomatedStrategy(req.body));
} catch (error) {
res.status(400).json({ error: error.message });
}
});
router.post('/amm/liquidity/automate', (req, res) => {
try {
res.json(AMMService.automateLiquidityProvision(req.body));
} catch (error) {
res.status(400).json({ error: error.message });
}
});
router.post('/amm/yield/estimate', (req, res) => {
try {
res.json(AMMService.estimateYieldFarming(req.body));
} catch (error) {
res.status(400).json({ error: error.message });
}
});
router.get('/amm/analytics', (req, res) => {
res.json(AMMService.getAMMAnalytics());
});
router.get('/amm/risk', (req, res) => {
res.json(AMMService.runRiskChecks());
});
router.get('/amm/optimize', (req, res) => {
res.json(AMMService.optimizeAMMPerformance());
});
// Returns supported assets and their issuers
router.get('/assets', (req, res) => {
const assets = SUPPORTED_ASSETS.map(code => ({
code,
issuer: code === 'XLM' ? null : getIssuer(code),
native: code === 'XLM',
}));
res.json({ assets });
});
// Create a trustline for a non-native asset (e.g. USDC)
router.post('/trustline', rules.createTrustline, validate, async (req, res) => {
try {
const { sourceSecret, assetCode } = req.body;
const result = await StellarService.createTrustline(sourceSecret, assetCode);
res.json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
export default router;