-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
257 lines (243 loc) · 8.68 KB
/
app.js
File metadata and controls
257 lines (243 loc) · 8.68 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
const express = require('express');
const Web3 = require('web3');
const sqlite3 = require('sqlite3').verbose();
const fs = require('fs');
const config = require('./config.js');
const i18n = require('./i18n.js');
const app = express();
const port = config.app.port;
const db = new sqlite3.Database('./database.db');
db.serialize(() => {
db.run('CREATE TABLE IF NOT EXISTS "deposits" ("id" INTEGER, "from_chain" INTEGER, "to_chain" INTEGER, "token" TEXT, "amount" INTEGER, "sender" TEXT, "receiver" TEXT, "created_at" INTEGER, "txhash" TEXT)');
});
const ABI = {
BRIDGE: JSON.parse(fs.readFileSync('abi/bridge.json', 'utf-8')),
ERC20: JSON.parse(fs.readFileSync('abi/erc20.json', 'utf-8'))
};
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.use(express.json());
function toFloat(number, decimals) {
return number / 10 ** decimals;
}
function toPlainString(num) {
return ('' + +num).replace(/(-?)(\d*)\.?(\d*)e([+-]\d+)/,
function (a, b, c, d, e) {
return e < 0
? b + '0.' + Array(1 - e - c.length).join(0) + c + d
: b + c + d + Array(e - d.length + 1).join(0);
});
}
async function db_all(query, params = []) {
return new Promise(function (resolve, reject) {
db.all(query, params, function (err, rows) {
if (err) { return reject(err); }
resolve(rows);
});
});
}
async function db_run(query, params = []) {
return new Promise(function (resolve, reject) {
db.run(query, params, function (err) {
if (err) { return reject(err); }
resolve();
});
});
}
app.get('/', (req, res) => {
res.render('index', { config, i18n })
});
app.get('/api/cron', async (req, res) => {
let row = await db_all('SELECT * from deposits WHERE txhash = "" ORDER by created_at ASC LIMIT 1');
if (row.length > 0) {
row = row[0];
await db_run('UPDATE deposits SET txhash = ? WHERE id = ? AND from_chain = ?', ['-', row.id, row.from_chain]);
let amount = row.amount - (row.amount * config.feeMultiplier);
amount = toPlainString(amount);
let to_chain = config.chains.find(e => e.id == row.to_chain);
let token = config.tokens.find(e => e.id == row.token);
if(typeof to_chain !== 'undefined' && typeof token !== 'undefined') {
let web3 = new Web3(to_chain.rpc);
let account = web3.eth.accounts.privateKeyToAccount(config.privateKey);
let bridge = new web3.eth.Contract(ABI.BRIDGE, to_chain.contract);
let tx = {
from: account.address,
to: web3.utils.toChecksumAddress(to_chain.contract),
data: bridge.methods.withdraw(token.address[to_chain.id], amount, row.receiver).encodeABI(),
gas: 2000000,
gasPrice: to_chain.gasPrice,
nonce: (await web3.eth.getTransactionCount(account.address, 'pending'))
};
let signedTx = await web3.eth.accounts.signTransaction(tx, config.privateKey);
let sended = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
await db_run('UPDATE deposits SET txhash = ? WHERE id = ? AND from_chain = ?', [sended.transactionHash, row.id, row.from_chain]);
}
}
res.send(JSON.stringify({
error: null
}));
});
app.post('/api/deposits', async (req, res) => {
if (typeof req.body === 'undefined' || typeof req.body.account !== 'string') {
res.send(JSON.stringify({
error: 'invalidData'
}));
return;
}
let web3 = new Web3();
if (web3.utils.isAddress(req.body.account.toLowerCase())) {
let rows = await db_all('SELECT * from deposits WHERE sender = ? ORDER by created_at DESC', [req.body.account.toLowerCase()]);
res.send(JSON.stringify({
error: null,
data: rows
}));
}
else {
res.send(JSON.stringify({
error: 'invalidAccount'
}));
}
});
app.post('/api/deposit', async (req, res) => {
if (typeof req.body === 'undefined' || typeof req.body.id === 'undefined' || typeof req.body.chain === 'undefined') {
res.send(JSON.stringify({
error: 'invalidData'
}));
return;
}
let depositId = parseInt(req.body.id);
let chainId = parseInt(req.body.chain);
let from = config.chains.find(e => e.id == chainId);
if (typeof from !== 'undefined') {
let rows = await db_all('SELECT id FROM deposits WHERE id = ? AND from_chain = ?', [depositId, chainId]);
if (rows.length == 0) {
let web3 = new Web3(from.rpc);
let bridge = new web3.eth.Contract(ABI.BRIDGE, from.contract);
let deposit = await bridge.methods.get(depositId).call();
if (deposit.timestamp != '0') {
if (config.pairs.includes(from.id + '-' + deposit.chain)) {
let tokenItem = config.tokens.find(e => e.address[from.id].toLowerCase() == deposit.token.toLowerCase());
if (typeof tokenItem !== 'undefined') {
await db_run('INSERT into deposits (id, from_chain, to_chain, token, amount, sender, receiver, created_at, txhash) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', [depositId, from.id, deposit.chain, tokenItem.id, deposit.amount, deposit.sender.toLowerCase(), deposit.receiver.toLowerCase(), deposit.timestamp, ''])
res.send(JSON.stringify({
error: null
}));
}
else {
res.send(JSON.stringify({
error: 'invalidToken'
}));
}
}
else {
res.send(JSON.stringify({
error: 'invalidPair'
}));
}
}
else {
res.send(JSON.stringify({
error: 'invalidDepositId'
}));
}
}
else {
res.send(JSON.stringify({
error: 'alreadyExists'
}));
}
}
else {
res.send(JSON.stringify({
error: 'invalidChain'
}));
}
});
app.post('/api/validate', async (req, res) => {
if (typeof req.body === 'undefined' || typeof req.body.account !== 'string' || typeof req.body.from === 'undefined' || typeof req.body.to === 'undefined' || typeof req.body.token === 'undefined' || typeof req.body.amount === 'undefined') {
res.send(JSON.stringify({
error: 'invalidData'
}));
return;
}
let account = req.body.account;
let from = parseInt(req.body.from);
let to = parseInt(req.body.to);
let amount = parseFloat(parseFloat(req.body.amount).toFixed(5));
let tokenId = parseInt(req.body.token);
let web3 = new Web3();
if (web3.utils.isAddress(account.toLowerCase())) {
if (config.pairs.includes(from + '-' + to)) {
if (typeof config.tokens[tokenId] !== 'undefined') {
let token = config.tokens[tokenId];
if (typeof token !== 'undefined') {
if (amount >= token.min) {
if (amount <= token.max) {
let fromChain = config.chains.find(e => e.id == from);
let toChain = config.chains.find(e => e.id == to);
web3 = new Web3(fromChain.rpc);
let tokenContract = new web3.eth.Contract(ABI.ERC20, token.address[from]);
let balance = await tokenContract.methods.balanceOf(account).call();
balance = toFloat(balance, token.decimals);
if (amount <= balance) {
web3 = new Web3(toChain.rpc);
tokenContract = new web3.eth.Contract(ABI.ERC20, token.address[to]);
balance = await tokenContract.methods.balanceOf(toChain.contract).call();
balance = toFloat(balance, token.decimals);
if (amount <= balance) {
res.send(JSON.stringify({
error: null,
amount: amount
}));
}
else {
res.send(JSON.stringify({
error: 'errorAmount'
}));
}
}
else {
res.send(JSON.stringify({
error: 'errorBalance'
}));
}
}
else {
res.send(JSON.stringify({
error: 'errorMax'
}));
}
}
else {
res.send(JSON.stringify({
error: 'errorMin'
}));
}
}
else {
res.send(JSON.stringify({
error: 'invalidToken'
}));
}
}
else {
res.send(JSON.stringify({
error: 'invalidAccount'
}));
}
}
else {
res.send(JSON.stringify({
error: 'invalidPair'
}));
}
}
else {
res.send(JSON.stringify({
error: 'invalidAccount'
}));
}
});
app.listen(port, () => {
console.log(`App listening on port ${port}`)
});