-
Notifications
You must be signed in to change notification settings - Fork 20
/
explorer.js
165 lines (134 loc) · 4.49 KB
/
explorer.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
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
/*jslint node: true */
"use strict";
const { existsSync, renameSync } = require('fs');
var desktopApp = require('ocore/desktop_app.js');
var appDataDir = desktopApp.getAppDataDir();
var path = require('path');
if (require.main === module && !existsSync(appDataDir) && existsSync(path.dirname(appDataDir)+'/byteball-explorer')){
console.log('=== will rename old explorer data dir');
renameSync(path.dirname(appDataDir)+'/byteball-explorer', appDataDir);
}
require('./relay');
var conf = require('ocore/conf.js');
var eventBus = require('ocore/event_bus.js');
var network = require('ocore/network.js');
const device = require('ocore/device');
const express = require("express");
const cors = require('cors')
const { createServer } = require("http");
const { Server } = require("socket.io");
const app = express();
const httpServer = createServer(app);
const api = require('./gateways/api');
const BalanceDumpService = require('./services/BalanceDumpService');
const io = new Server(httpServer, {
cors: {
origin: "*"
}
});
let exchange_rates = {};
if (conf.initial_peers) {
const firstPeer = conf.initial_peers[0];
const hubAddress = firstPeer.startsWith('wss://') ? firstPeer.substring(6) : firstPeer.substring(5);
device.setDeviceHub(hubAddress);
network.findOutboundPeerOrConnect(firstPeer, (err, ws) => {
if (err)
return console.log('failed to connect to initial peer ' + firstPeer + ': ' + err);
ws.bLoggedIn = true;
network.sendRequest(ws, 'hub/get_exchange_rates', null, null, (ws, err, result) => {
exchange_rates = result;
})
});
}
eventBus.on('new_joint', function() {
io.sockets.emit('update');
});
eventBus.on('rates_updated', function() {
exchange_rates = { ...exchange_rates, ...network.exchangeRates };
console.log('rates_updated: ', exchange_rates);
io.sockets.emit('rates_updated', exchange_rates);
});
app.use(cors());
app.get('/api/unit/:unit', async(req, res) => {
if (req.params.unit.length !== 44) {
return res.json({ notFound: true });
}
await api.dagGateway.info(req.params.unit, result => {
res.json(result);
});
});
app.get('/api/address/:address/info', async (req, res) => {
if (req.params.address.length !== 32) {
return res.json({ notFound: true });
}
const params = {
address: req.params.address,
...req.query,
}
await api.addressGateway.getAddressData(params, result => {
res.json(result);
});
});
app.get('/api/address/:address/next_page', async (req, res) => {
if (req.params.address.length !== 32) {
return res.json({ notFound: true });
}
const params = {
address: req.params.address,
...req.query
}
await api.addressGateway.loadNextPageAddressTransactions(params, result => {
res.json(result);
});
});
app.get('/api/asset/:asset/info', async (req, res) => {
const params = {
asset: req.params.asset,
}
await api.assetGateway.getAssetData(params, result => {
res.json(result);
});
});
app.get('/api/asset/:asset/next_page_transactions', async (req, res) => {
const params = {
asset: req.params.asset,
...req.query,
}
await api.assetGateway.loadNextPageAssetTransactions(params, result => {
res.json(result);
});
});
app.get('/api/asset/:asset/next_page_holders', async (req, res) => {
const params = {
asset: req.params.asset,
...req.query,
}
await api.assetGateway.loadNextPageAssetHolders(params, result => {
res.json(result);
});
});
io.on('connection', async (socket) => {
socket.emit('rates_updated', exchange_rates);
socket.on('info', api.dagGateway.info);
socket.on('newUnits', api.dagGateway.newUnits);
socket.on('nextUnits', api.dagGateway.nextUnits);
socket.on('prevUnits', api.dagGateway.prevUnits);
socket.on('getUnit', api.dagGateway.getUnit);
socket.on('getLastUnits', api.dagGateway.getLastUnits);
socket.on('highlightNode', api.dagGateway.highlightNode);
socket.on('getAddressData', api.addressGateway.getAddressData);
socket.on('loadNextPageAddressTransactions', api.addressGateway.loadNextPageAddressTransactions);
socket.on('getAssetData', api.assetGateway.getAssetData);
socket.on('loadNextPageAssetTransactions', api.assetGateway.loadNextPageAssetTransactions);
socket.on('loadNextPageAssetHolders', api.assetGateway.loadNextPageAssetHolders);
socket.on('fetchAssetNamesList', api.assetGateway.fetchAssetNamesList);
await api.assetGateway.fetchAssetNamesList(({ assetNames }) => {
socket.emit('updateAssetsList', assetNames);
})
});
httpServer.listen(conf.webPort);
async function start() {
const balanceDumpService = new BalanceDumpService();
await balanceDumpService.start();
}
start();