-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
140 lines (132 loc) · 4.62 KB
/
server.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
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
require('dotenv').config()
const debug = require('debug')('app');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
global.fetch = require("node-fetch");
const app = express();
const port = process.env.PORT || 5000;
const contractMiddlewareEth = require('./middleware/contractEth.js');
const contractMiddlewareMxc = require('./middleware/contractMxc.js');
const contractMiddlewareDhx = require('./middleware/contractDhx.js');
const { DApp: { sendTransactionEth, sendTransactionMxc } } = require('./helpers/contract.js');
const { DAppDhx: { sendTransactionDhx } } = require('./helpers/contractDhx.js');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
/**
* Example: http://localhost:5000/api/faucet/eth/ropsten?address=<ETHEREUM_ADDRESS>
*/
app.get('/api/faucet/eth/ropsten',
// Middleware chain
async (req, res, next) => {
console.log('Checking Ethereum account balance of requestor');
await contractMiddlewareEth.checkBalanceRequestorEth(req, res, next);
},
async (req, res, next) => {
console.log('Checking Ethereum account balance of faucet');
await contractMiddlewareEth.checkBalanceFaucetEth(req, res, next);
},
async (req, res, next) => {
if (!res.locals.isBalanceRequestorLowEth) {
res.send({ message: 'Ropsten Ether balance of requestor already deemed sufficient' });
return;
}
if (res.locals.isBalanceFaucetLowEth) {
res.send({ message: 'Ropsten Ether balance of faucet depleted. Please try again later.' });
return;
}
// Handle error in async function
try {
const to = req.query.address;
const { transactionHashUrl } = await sendTransactionEth(to);
res.send({
message: 'Ropsten Ether sent with transaction',
tx: transactionHashUrl
});
} catch (error) {
debug(error);
return;
}
}
);
/**
* Example: http://localhost:5000/api/faucet/mxc/ropsten?address=<ETHEREUM_ADDRESS>
*/
app.get('/api/faucet/mxc/ropsten',
// Middleware chain
async (req, res, next) => {
console.log('Checking MXC ERC-20 token account balance of requestor');
await contractMiddlewareMxc.checkBalanceRequestorMxc(req, res, next);
},
async (req, res, next) => {
console.log('Checking MXC ERC-20 token account balance of faucet');
await contractMiddlewareMxc.checkBalanceFaucetMxc(req, res, next);
},
async (req, res, next) => {
if (!res.locals.isBalanceRequestorLowMxc) {
res.send({ message: 'Ropsten MXC ERC-20 token balance of requestor already deemed sufficient' });
return;
}
if (res.locals.isBalanceFaucetLowMxc) {
res.send({ message: 'Ropsten MXC ERC-20 token balance of faucet depleted. Please try again later.' });
return;
}
// Handle error in async function
try {
const to = req.query.address;
const { transactionHashUrl } = await sendTransactionMxc(to);
res.send({
message: 'Ropsten MXC ERC-20 sent with transaction',
tx: transactionHashUrl
});
} catch (error) {
debug(error);
return;
}
}
);
/**
* Example: http://localhost:5000/api/faucet/dhx/harbour?address=<DATAHIGHWAY_ADDRESS>
*/
app.get('/api/faucet/dhx/harbour',
// Middleware chain
async (req, res, next) => {
console.log('Checking DHX token account balance of requestor');
await contractMiddlewareDhx.checkBalanceRequestorDhx(req, res, next);
},
async (req, res, next) => {
console.log('Checking DHX token account balance of faucet');
await contractMiddlewareDhx.checkBalanceFaucetDhx(req, res, next);
},
async (req, res, next) => {
if (!res.locals.isBalanceRequestorLowDhx) {
res.send({ message: 'Harbour DHX token balance of requestor already deemed sufficient' });
return;
}
if (res.locals.isBalanceFaucetLowDhx) {
res.send({ message: 'Harbour DHX token balance of faucet depleted. Please try again later.' });
return;
}
// Handle error in async function
try {
const to = req.query.address;
const { transactionHashUrl } = await sendTransactionDhx(to);
res.send({
message: 'Harbour DHX sent with transaction',
tx: transactionHashUrl
});
} catch (error) {
debug(error);
return;
}
}
);
if (process.env.NODE_ENV === 'production') {
// Serve any static files
app.use(express.static(path.join(__dirname, 'client/build')));
// Handle React routing, return all requests to React app
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
app.listen(port, () => console.log(`Listening on port ${port}`));