-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.ts
469 lines (404 loc) · 17.2 KB
/
app.ts
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import express from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import dotenv from 'dotenv'
import cookieParser from 'cookie-parser'
//import session from 'express-session'
import { createServer } from 'http'
import { saveUrl, getUrl } from './src/api/handledb'
import { isValidChain } from './src/api/Chains'
import { createWebhook } from './src/utils'
import logger from './src/logger'
import {
decompressString,
scenario_info,
insert_scenario,
scenario_detailed_info,
multi_scenario_info,
create_swap,
} from './src/scenarios/parse_db'
dotenv.config()
const app = express()
const PORT = process.env.PORT || 8080
const ENV = process.env.NODE_ENV || 'development'
import testRoute from './src/routes/test'
import scenarioRoute from './src/routes/scenario'
import webhookRoute from './src/routes/webhook'
import httpRoute from './src/routes/http'
import hrmpRoute from './src/routes/hrmp'
import chainRoute from './src/routes/chain'
import templateRoute from './src/routes/template'
import actionsRoute from './src/routes/actions'
import inkRoute from './src/routes/ink'
import PreviewRoute from './src/routes/preview'
// // CORS options
const corsOptions = {
origin: ENV === 'development' ? process.env.DEV_URL : process.env.PROD_URL,
credentials: false,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204,
}
app.use(cors(corsOptions))
//app.use(express.json());
// TODO
//app.use(session({ secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: true }));
// Use body-parser middleware to parse JSON
app.use(bodyParser.json())
app.use(cors())
app.use(express.urlencoded({ extended: false }))
// app.use(bodyParser.json());
app.use(cookieParser(process.env.COOKIE_PARSER_SECRET))
/*
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true
}));
*/
app.use((req, res, next) => {
// req.io = io;
console.log('Time:', Date.now())
console.log('Request Body in app.js:', req.body)
next()
})
app.use('/api/test', testRoute)
app.use('/api/scenario', scenarioRoute)
app.use('/api/webhook', webhookRoute)
app.use('/api/http', httpRoute)
app.use('/api/hrmp', hrmpRoute)
app.use('/api/chain', chainRoute)
app.use('/api/template', templateRoute)
app.use('/api/actions', actionsRoute)
app.use('/api/ink', inkRoute)
app.use('/api/info', PreviewRoute)
app.get('/', async (req, res) => {
res.json({ success: true, documentation: 'https://docs.bagpipes.io/docs/api/docs' })
})
const httpServer = createServer(app)
// Listen for HTTP connections.
httpServer.listen(PORT, () => {
logger.info(`Server is listening and running on port ${PORT}`)
})
// // todo add kusamas
// // open channels, list open ingoing and outgoing hrmp channels for paraid
// app.post('/polkadot/openchannels', async (req, res) => {
// const paraid = req.body.paraid // get the chains paraid
// if (typeof paraid !== 'number' || paraid < 0 || paraid > 10000) {
// return res.status(400).json({ error: 'Invalid paraid. It must be a number between 0 and 10000.' })
// }
// const channels = await inandoutchannels(paraid)
// res.json({ open_hrmp_channels: channels, sourcechain: paraid })
// })
// // tx broadcast
// // /broadcast input: {chain: 'hydradx', tx: ''}
// app.post('/broadcast', async (req, res) => {
// const chain = req.body.chain
// const txdata = req.body.tx // get the chains paraid
// if (chain !== 'polkadot' && chain !== 'hydraDx' && chain !== 'assetHub') {
// return res.status(400).json({ error: 'Invalid chain. Select polkadot/hydraDx/assetHub' })
// }
// //if (typeof txdata !== 'string' || txdata.length > 1) {
// // return res.status(400).json({ error: 'Invalid txdata.' });
// //}
// try {
// const myhash = await broadcastToChain(chain, txdata)
// // console.log(`myhash:`, myhash.toString());
// res.json({ status: 'broadcasted', hash: myhash })
// } catch (error) {
// console.error('Error while broadcasting to the chain:', error)
// res.status(500).json({ error: 'An error occurred while broadcasting to the chain.' })
// }
// })
// // Save Scenario
// //router.post('/save', verifyToken, async (req, res) => {
// // save data and generate storage key/short url key
// app.post('/saveUrl', async (req, res) => {
// const longUrl = req.body
// //console.log(`Saving `, longUrl);
// try {
// // console.log(`saving it`);
// const shortUrl = await saveUrl(longUrl)
// // console.log(`after save shortUrl:`, shortUrl);
// res.json({ success: true, shortUrl })
// } catch (error) {
// console.error('Error saving URL:', error)
// res.status(500).json({ success: false, error: 'Internal Server Error' })
// }
// })
// app.get('/', async (req, res) => {
// res.json({ success: true, documentation: 'https://xcmsend.github.io/api/index.html' })
// })
// // Get URL
// app.get('/getUrl/:shortUrl', async (req, res) => {
// const { shortUrl }: { shortUrl: string } = req.params
// // console.log(`geturl`);
// try {
// const longUrl = await getUrl(shortUrl)
// // console.log(`getUrl: `, shortUrl);
// res.json({ success: true, longUrl })
// } catch (error) {
// console.error('Error getting URL:', error)
// res.status(500).json({ success: false, error: 'Internal Server Error' })
// }
// })
// bagpipes: swap assetin to assetout amount
// curl -X POST -H "Content-Type: application/json" -d '{"assetin": "0", "assetout": "5", "amount": 100 }' http://localhost:8080/create/swap
// app.post('/create/swap', async (req, res) => {
// console.log(`create swap called`);
// console.log(`create swap: `, req.body);
// try {
// console.log(`output: `, req.body.assetin, req.body.assetout, req.body.amount);
// const assetin: number = parseInt(req.body.assetin, 10)
// const assetout: number = parseInt(req.body.assetout, 10)
// const amount: number = parseInt(req.body.amount, 10)
// if (typeof assetin !== 'number' ||
// typeof assetout !== 'number' ||
// typeof amount !== 'number' ||
// isNaN(assetin) ||
// isNaN(assetout) ||
// isNaN(amount)) {
// return res
// .status(400)
// .json({ error: 'Missing required fields, view documentation: https://xcmsend.github.io/api/docs.html' })
// }
// console.log(`pling plong`);
// // Validate that the parsed values are valid numbers
// if (isNaN(assetin) || isNaN(assetout) || isNaN(amount)) {
// return res.status(400).json({ error: 'submit assetid, amount and assetout as numbers ' })
// }
// const out = await create_swap(assetin, assetout, amount)
// res.status(200).json({ success: true, swap: out })
// } catch (error) {
// console.error(error)
// res
// .status(500)
// .json({
// error: 'Internal Server Error, reach fellow developers on bagpipes discord: https://discord.gg/ZBfmzJ7Jjz',
// })
// }
// })
// // curl -X POST -H "Content-Type: application/json" -d '{"chain": "polkadot", "msg": "hack the planet"}' http://localhost:8080/system-remark
// app.post('/system-remark', async (req, res) => {
// const chain: string = req.body.chain;
// const msg: string = req.body.msg;
// const chains = ["turing", "moonriver", "mangatax", "assetHub", "interlay", "hydraDx", 'polkadot'];
// if (!chains.includes(chain)) {
// return res.json({error: "invalid chain, select one of: turing, moonriver, mangatax, assetHub, interlay, hydraDx, polkadot"});
// }
// console.log(`making system remark with: `, chain, msg);
// const tx = (await generic_system_remark(chain, msg)).toHex();
// res.json({ result: tx })
// })
// // curl -X POST -H "Content-Type: application/json" -d '{"source_chain": "polkadot", "dest_chain": "hydraDx", "destination_address": "your_destination_address", "amount": 100, "assetid": 1}' http://localhost:8080/create/scenario
// // {"result":"QWdI3KifK"}
// // create scenerio
// //
// // xcmsend: assetid1984 from assethub to hydra destaddress
// app.post('/create/scenario', async (req, res) => {
// const source_chain: string = req.body.source_chain
// const dest_chain: string = req.body.dest_chain
// const dest_address: string = req.body.destination_address // this should be dest_address
// const amount: number = req.body.amount
// const assetid: number = req.body.assetid
// // Validation checks
// if (!isValidChain(source_chain)) {
// return res.status(400).json({ error: 'Invalid source_chain provided.' })
// }
// if (!isValidChain(dest_chain)) {
// return res.status(400).json({ error: 'Invalid dest_chain provided.' })
// }
// if (!source_chain || typeof source_chain !== 'string') {
// return res.status(400).json({ error: 'Invalid source_chain provided.' })
// }
// if (!dest_chain || typeof dest_chain !== 'string') {
// return res.status(400).json({ error: 'Invalid dest_chain provided.' })
// }
// if (!dest_address || typeof dest_address !== 'string') {
// return res.status(400).json({ error: 'Invalid destination_address provided.' })
// }
// if (dest_address || dest_chain == "moonriver" || !dest_address.startsWith("0x")) {
// return res.status(400).json({error: "give me an evm address for moonbeams destinatino_address"})
// }
// if (isNaN(amount) || amount <= 0) {
// return res.status(400).json({ error: 'Invalid amount provided.' })
// }
// if (isNaN(assetid) || assetid < 0) {
// return res.status(400).json({ error: 'Invalid assetid provided.' })
// }
// const shorturl = await insert_scenario(source_chain, dest_chain, dest_address, amount, assetid)
// console.log(`got the shorturl:`, shorturl)
// res.json({ result: shorturl })
// })
// // curl -X POST -H "Content-Type: application/json" -d '{"id": "scenario_id_here"}' http://localhost:8080/scenario/info
// // get info about a certain scenario
// app.post('/scenario/info', async (req, res) => {
// const scenario_id = req.body.id
// // console.log(`scenario info`);
// if (!scenario_id) {
// return res.json({
// result:
// "No scenario id detected, provide a request like this: curl -X ENDPOINT/scenario/info -d {'id':'my scenario id here'}",
// })
// }
// const get_data = await getUrl(scenario_id)
// if (!get_data) {
// return res.json({ result: 'Could not find the scenario data' })
// }
// // console.log(`get_data is:`, get_data);
// const decoded = await decompressString(get_data)
// // console.log(`decoded: `, decoded);
// const out = await scenario_info(decoded)
// // console.log(`out is:`, out);
// res.json({ result: out })
// })
// /*
// Provide a vector of output [ node0_info, node1_info, node1 ]
// */
// // display full scenario info
// // curl -X POST -H "Content-Type: application/json" -d '{"id": "a-aPVjoub"}' http://localhost:8080/scenario/info/full
// app.post('/scenario/info/full', async (req, res) => {
// const output = {
// tx: 'could not find',
// summary: 'could not find',
// asset: 'could not find',
// amount: 'could not find',
// source_chain: 'could not find',
// dest_chain: 'could not find',
// txtype: 'could not find',
// }
// const scenario_id = req.body.id
// console.log(`got user input:`, scenario_id);
// if (!scenario_id) {
// console.log(`no scenario id`);
// return res.json({
// result:
// "No scenario id detected, provide a request like this: curl -X ENDPOINT/scenario/info -d {'id':'my scenario id here'}",
// })
// }
// try {
// console.log(`calling get url`);
// const get_data = await getUrl(scenario_id)
// if (!get_data) {
// console.log(`did not get data`);
// return res.json({ result: 'Could not find the scenario data' })
// }
// console.log(`get_data is:`, get_data);
// const decoded = await decompressString(get_data)
// console.log(`decoded: `, decoded);
// const deep_coded = await scenario_detailed_info(JSON.parse(decoded))
// console.log(`deep info:`, deep_coded);
// // const out = await scenario_info(decoded);
// output['summary'] = deep_coded.source_chain + ' > ' + deep_coded.tx_type + ' > ' + deep_coded.dest_chain
// output['txtype'] = deep_coded.tx_type
// output['amount'] = deep_coded.source_amount
// output['asset'] = deep_coded.source_asset
// output['dest_chain'] = deep_coded.dest_chain
// output['source_chain'] = deep_coded.source_chain
// // console.log(`output is:`, output);
// // const tx = await route_tx(sourchain, destchain, output['asset'], output['amount'], destinationaddress);
// // console.log(`source chain: `, deep_coded.source_chain);
// // console.log(`dest chain: `, deep_coded.dest_chain);
// // console.log(`dest address: `, deep_coded.dest_address);
// try {
// const tx = await route_tx(
// deep_coded.source_chain,
// deep_coded.dest_chain,
// parseFloat(output['asset']),
// parseFloat(output['amount']),
// deep_coded.dest_address,
// )
// console.log(`tx is: `, tx.toHex());
// output['tx'] = tx.toHex()
// } catch (error) {
// console.log(`tx gen error: `, error);
// output['tx'] = 'could not generate tx'
// }
// } catch (error) {
// console.log(`got error:`, error)
// return res.json({ error: 'Invalid scenario id' })
// }
// // console.log(`output is:`, output);
// res.json({ result: output })
// })
// // curl -X POST -H "Content-Type: application/json" -d '{"id": "frb-UXJuK"}' http://localhost:8080/scenario/info/multi
// // sample output: {"result":{"scenarios":[{"source_chain":"polkadot","source_address":"5HdRaUshTGoQFatoJ7Wg9Skg7BLCjfJaV5V9qYNoqR7zfX5B","dest_chain":"assetHub","dest_address":"5HdRaUshTGoQFatoJ7Wg9Skg7BLCjfJaV5V9qYNoqR7zfX5B","assetid":0,"amount":"13","txtype":"xTransfer","tx":"not set","source_amount":"13"},{"source_chain":"assetHub","source_address":"5HdRaUshTGoQFatoJ7Wg9Skg7BLCjfJaV5V9qYNoqR7zfX5B","dest_chain":"polkadot","dest_address":"5HdRaUshTGoQFatoJ7Wg9Skg7BLCjfJaV5V9qYNoqR7zfX5B","assetid":"3","amount":"3","txtype":"xTransfer","tx":"not set","source_amount":"3"},{"source_chain":"polkadot","source_address":"5HdRaUshTGoQFatoJ7Wg9Skg7BLCjfJaV5V9qYNoqR7zfX5B","dest_chain":"assetHub","dest_address":"5HdRaUshTGoQFatoJ7Wg9Skg7BLCjfJaV5V9qYNoqR7zfX5B","assetid":0,"amount":"1","txtype":"xTransfer","tx":"not set","source_amount":"1"}]}}
// // parse multiscenario, action node only
// app.post('/scenario/info/multi', async (req, res) => {
// const scenario_id = req.body.id
// if (!scenario_id) {
// return res.json({
// result:
// "No scenario id detected, provide a request like this: curl -X ENDPOINT/scenario/info -d {'id':'my scenario id here'}",
// })
// }
// const get_data = await getUrl(scenario_id)
// if (!get_data) {
// return res.json({ result: 'Could not find the scenario data' })
// }
// const decoded = await decompressString(get_data)
// // console.log(`decoded: `, decoded);
// const multisdaat = multi_scenario_info(JSON.parse(decoded))
// // console.log(`multisdaat:`, multisdaat);
// // console.log(`multisdaat done`);
// return res.json({ result: { scenarios: multisdaat } })
// })
// // curl -X POST -H "Content-Type: application/json" http://localhost:8080/create/webhook
// app.post('/create/webhook', createWebhook)
// /*
// app.post('/create/webhook', async ( req, res) => {
// res.json({result: "function coming soon"});
// } );
// */
/*
/execute
/createScenario
router.get('/load/:_id', verifyToken, async (req, res) => {
router.post('/stopExecution', verifyToken, async (req, res) => {
router.post('/createWebhook', async (req, res) => {
router.post('/receiveWebhook', async (req, res) => {
*/
// // xcm asset transfer
// // call a scenerio - call a scenario you created in the UI - todo
// app.post('/xcm-asset-transfer', async (req, res) => {
// console.log(`xcm-asset-transfer`)
// console.log(req.body)
// try {
// console.log(`s0`)
// const sourchain = req.body.sourchain
// console.log(`sourcechain:`, sourchain)
// const destchain = req.body.destchain
// console.log(`destchain:`, destchain)
// const assetid = req.body.assetid
// console.log(`assetid:`, assetid)
// const amount = req.body.amount
// console.log(`amount:`, amount)
// const destinationaddress = req.body.destinationaddress
// console.log(`destinationaddress:`, destinationaddress)
// console.log(`route_tx start`)
// const tx = await route_tx(sourchain, destchain, assetid, amount, destinationaddress)
// console.log(`tx route_tx`)
// res.json({ txdata: tx })
// } catch (error) {
// res.status(500).json({ error: 'An error occurred' })
// }
// })
// app.post('/polkadot/xcm-native-transfer', (req, res) => {
// const jsonData = req.body.scenarioid
// res.json({ receivedData: 'todo' })
// })
//
// db info, display metadata about stored scenarios, amount and so on
// // use template - todo
// app.post('/template/call', (req, res) => {
// const jsonData = req.body.scenarioid
// res.json({ receivedData: 'todo' })
// })
// // call a scenerio - call a scenario you created in the UI - todo
// app.post('/scenario/call', (req, res) => {
// const jsonData = req.body.scenarioid
// res.json({ receivedData: 'todo' })
// })
// app.listen(PORT, () => {
// console.log(`Server is running on port ${PORT}`)
// })