-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
85 lines (72 loc) · 2.91 KB
/
app.js
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
import express from 'express';
import bodyParser from 'body-parser';
import {createTransaction} from "./send.js";
const app = express();
import winston from 'winston';
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: {service: 'user-service'},
transports: [
//
// - Write all logs with importance level of `error` or less to `error.log`
// - Write all logs with importance level of `info` or less to `combined.log`
//
new winston.transports.File({filename: 'error.log', level: 'error'}),
new winston.transports.File({filename: 'combined.log'}),
],
});
app.use(bodyParser.json());
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.post('/create', async (req, res) => {
const network = req.body.network;
const projectId = req.body.project;
const fromAddress = req.body.from;
const toAddress = req.body.to;
const privateKey = req.body.key;
const data = req.body.data;
if (!network || network === '')
return res.status(400).json({message: "Network is required"});
if (!projectId || projectId === '')
return res.status(400).json({message: "Infura project id is required"});
if (!fromAddress || fromAddress === '')
return res.status(400).json({message: "From address is required"});
if (!toAddress || toAddress === '')
return res.status(400).json({message: "To address is required"});
if (!privateKey || privateKey === '')
return res.status(400).json({message: "Private key is required"});
try {
return await performCall(network, projectId, fromAddress, toAddress, privateKey, data, false, res);
} catch (e) {
console.log("caught first error: " + e);
try {
console.log("trying again...");
return await performCall(network, projectId, fromAddress, toAddress, privateKey, data, true, res);
} catch (err) {
console.log("caught second error: " + err);
logger.log({
level: 'error',
message: 'Transaction error. data: ' + data + ' Message: ' + err.toString()
});
return res.status(500).json({message: err.toString()});
}
}
});
async function performCall(network, projectId, fromAddress, toAddress, privateKey, data, shouldAddToGas, res) {
logger.log({
level: 'info',
message: 'New transaction request. data: ' + data
});
const result = await createTransaction(network, projectId, fromAddress, toAddress, privateKey, data, shouldAddToGas);
res.status(200).json(result);
logger.log({
level: 'info',
message: 'Transaction completed successfully. data: ' + data
});
}
app.listen(8000);