Skip to content

Commit 0c97bf2

Browse files
committed
Build 1.0.3
1 parent 8d0f50a commit 0c97bf2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+895
-5771
lines changed

dist/api/Api.js

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
11
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = void 0;var _DataCollector = require("./lib/DataCollector");
22
var _modules = require("./modules");
33
var _types = require("../lib/types");
4-
var _getCirculatingSupply = _interopRequireDefault(require("./lib/getCirculatingSupply"));
54
var _blocksCollections = require("../lib/blocksCollections");
65
var _apiTools = require("./lib/apiTools");
7-
var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
6+
var _config = _interopRequireDefault(require("../lib/config"));
7+
var _NativeContracts = _interopRequireDefault(require("../lib/NativeContracts"));
8+
9+
var _getCirculatingSupply = _interopRequireDefault(require("./lib/getCirculatingSupply"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };} // It is used only in case Stats cannot provide the circulating supply
810

911
class Api extends _DataCollector.DataCollector {
10-
constructor({ db, initConfig, nativeContracts }, { modules, collectionsNames, lastBlocks } = {}) {
12+
constructor({ db, initConfig }, { modules, collectionsNames, lastBlocks } = {}) {
1113
const collectionName = collectionsNames.Blocks;
1214
super(db, { collectionName });
1315
this.collectionsNames = collectionsNames;
1416
this.collections = (0, _blocksCollections.getDbBlocksCollections)(db);
15-
this.lastLimit = lastBlocks || 10;
16-
this.latest = 0;
17-
this.lastBlocks = [];
18-
this.lastTransactions = [];
17+
this.lastLimit = lastBlocks || 100;
18+
this.latest = undefined;
19+
this.lastBlocks = { data: [] };
20+
this.lastTransactions = { data: [] };
1921
this.circulatingSupply = null;
2022
this.stats = { timestamp: 0 };
2123
this.loadModules((0, _modules.getEnabledApiModules)(modules));
2224
this.initConfig = initConfig;
23-
const { isNativeContract } = nativeContracts;
25+
const { isNativeContract } = (0, _NativeContracts.default)(initConfig);
2426
this.isNativeContract = isNativeContract;
27+
this.tick();
2528
}
2629
tick() {
2730
this.setLastBlocks();
28-
this.setCirculatingSupply();
2931
}
3032

3133
loadModules(modules) {
3234
Object.keys(modules).forEach(name => {
33-
const module = new modules[name](this.collections, name);
34-
this.log.info(`Loading module ${name}`);
35-
this.addModule(module, name);
35+
const constructor = modules[name];
36+
if (typeof constructor === 'function') {
37+
const module = new constructor(this.collections, name);
38+
this.log.info(`Loading module ${name}`);
39+
this.addModule(module, name);
40+
}
3641
});
3742
}
3843

@@ -67,51 +72,48 @@ class Api extends _DataCollector.DataCollector {
6772

6873
async setLastBlocks() {
6974
try {
70-
let { collection, lastLimit } = this;
75+
const Block = this.getModule('Block');
7176
const Tx = this.getModule('Tx');
72-
let blocks = await collection.find().sort({ number: -1 }).limit(lastLimit).toArray();
73-
let txs = await Tx.db.find({ txType: { $in: [_types.txTypes.default, _types.txTypes.contract] } }).
74-
sort({ blockNumber: -1, transactionIndex: -1 }).
75-
limit(this.lastLimit).
76-
toArray();
77-
78-
this.updateLastBlocks(blocks, txs);
77+
let limit = this.lastLimit;
78+
let blocks = await Block.run('getBlocks', { limit, addMetadata: true });
79+
let query = { txType: [_types.txTypes.default, _types.txTypes.contract] };
80+
let transactions = await Tx.run('getTransactions', { query, limit });
81+
this.updateLastBlocks(blocks, transactions);
7982
} catch (err) {
8083
this.log.debug(err);
8184
}
8285
}
83-
84-
async setCirculatingSupply() {
85-
try {
86-
const collection = this.collections.Addrs;
87-
let circulating = await (0, _getCirculatingSupply.default)(collection, this.initConfig.nativeContracts);
88-
this.circulatingSupply = Object.assign({}, circulating);
89-
} catch (err) {
90-
this.log.debug(err);
91-
}
86+
getStats() {
87+
return this.formatData(this.stats);
9288
}
9389
getCirculatingSupply() {
9490
return this.formatData(this.circulatingSupply);
9591
}
9692

9793
getLastBlocks() {
98-
let blocks = this.lastBlocks;
99-
let transactions = this.lastTransactions;
100-
return this.formatData({ blocks, transactions });
94+
let data = this.lastBlocks;
95+
return this.formatData(data);
96+
}
97+
98+
getLastTransactions() {
99+
let data = this.lastTransactions;
100+
return this.formatData(data);
101101
}
102102

103103
getLastBlock() {
104-
return this.lastBlocks[0] || null;
104+
let { data } = this.lastBlocks;
105+
return data[0] || null;
105106
}
106107

107108
updateLastBlocks(blocks, transactions) {
109+
let blockData = blocks.data;
108110
this.lastBlocks = blocks;
109111
this.lastTransactions = transactions;
110112
let latest;
111-
if (blocks && blocks[0]) latest = blocks[0].number;
113+
if (blockData && blockData[0]) latest = blockData[0].number;
112114
if (latest !== this.latest) {
113115
this.latest = latest;
114-
this.events.emit('newBlocks', this.formatData({ blocks, transactions }));
116+
this.events.emit('newBlocks', this.getLastBlocks());
115117
this.updateStats();
116118
}
117119
}
@@ -132,19 +134,33 @@ class Api extends _DataCollector.DataCollector {
132134

133135
async updateStats() {
134136
const oldStats = this.stats;
135-
const stats = await this.getModule('Stats').run('getLatest');
137+
const Stats = await this.getModule('Stats');
138+
if (!Stats) return;
139+
const stats = await Stats.run('getLatest');
136140
if (!stats) return;
137141

138-
const ExtendedStats = this.getModule('ExtendedStats');
139-
if (ExtendedStats) {
140-
const blockNumber = parseInt(stats.blockNumber);
141-
const extendedStats = await ExtendedStats.getExtendedStats(blockNumber);
142-
Object.assign(stats, extendedStats);
143-
}
144-
142+
/* const ExtendedStats = this.getModule('ExtendedStats')
143+
if (ExtendedStats) {
144+
const blockNumber = parseInt(stats.blockNumber)
145+
const extendedStats = await ExtendedStats.getExtendedStats(blockNumber)
146+
Object.assign(stats, extendedStats)
147+
} */
148+
let circulatingSupply = stats.circulatingSupply || (await this.getCirculatingSupplyFromDb());
149+
this.circulatingSupply = circulatingSupply;
145150
this.stats = Object.assign({}, stats);
146-
if (stats.timestamp !== oldStats.timestamp) {
147-
this.events.emit('newStats', this.stats);
151+
let timestamp = stats.timestamp || 0;
152+
if (timestamp > oldStats.timestamp) {
153+
this.events.emit('newStats', this.getStats());
154+
}
155+
}
156+
async getCirculatingSupplyFromDb() {
157+
try {
158+
const collection = this.collections.Addrs;
159+
const { nativeContracts } = this.initConfig;
160+
let circulating = await (0, _getCirculatingSupply.default)(collection, nativeContracts);
161+
return circulating;
162+
} catch (err) {
163+
this.log.debug(err);
148164
}
149165
}}var _default =
150166

dist/api/HttpServer.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ const HttpServer = ({ api, status, log }) => {
1010
app.set('etag', false);
1111
app.set('x-powered-by', false);
1212

13+
// status
1314
app.get('/status', (req, res) => {
1415
const data = status.getState().data;
1516
res.send(data);
1617
});
1718

18-
app.get('/circulating', (req, res) => {
19-
const data = api.getCirculatingSupply().data;
19+
// circulating supply
20+
app.get('/circulating/:field?', (req, res) => {
21+
let { field } = req.params;
22+
let { data } = api.getCirculatingSupply();
23+
data = field ? `${data[field]}` : data;
2024
res.send(data);
2125
});
2226

dist/api/Status.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.default = exports.Status = void 0;var _DataCollector = require("./lib/DataCollector/");
2-
var _config = _interopRequireDefault(require("../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
3-
const { collectionsNames } = _config.default;
2+
var _blocksCollections = require("../lib/blocksCollections");
43

54
class Status extends _DataCollector.DataCollector {
65
constructor(db) {
7-
const collectionName = collectionsNames.Status;
8-
super(db, { collectionName });
6+
const collections = (0, _blocksCollections.getDbBlocksCollections)(db);
7+
const { Status, Blocks } = collections;
8+
super(db, { collectionName: 'Status' });
99
this.tickDelay = 5000;
1010
this.state = {};
11-
this.addModule(new _DataCollector.DataCollectorItem(db.collection(collectionsNames.Status), 'Status'));
12-
this.addModule(new _DataCollector.DataCollectorItem(db.collection(collectionsNames.Blocks), 'Blocks'));
11+
this.addModule(new _DataCollector.DataCollectorItem(Status, 'Status'));
12+
this.addModule(new _DataCollector.DataCollectorItem(Blocks, 'Blocks'));
1313
}
1414
tick() {
1515
this.updateState().then(newState => {

dist/api/channels.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var _Channel = _interopRequireDefault(require("./lib/Channel"));function _intero
33

44
const CHANNELS = {
55
blocksChannel: 'blocks',
6+
txsChannel: 'transactions',
67
statusChannel: 'status',
78
txPoolChannel: 'txpool',
89
statsChannel: 'stats' };exports.CHANNELS = CHANNELS;

dist/api/docs.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ definitions:
6868
type: integer
6969
next:
7070
type: string
71+
fields:
72+
type:
73+
oneOf:
74+
- array
75+
- object
76+
77+
78+
7179

7280
Response:
7381
type: object

dist/api/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ var _evaluateError = require("./lib/evaluateError");function _interopRequireDefa
1414
const port = _config.default.api.port || '3003';
1515
const address = _config.default.api.address || 'localhost';
1616

17-
(0, _dataSource.setup)({ log: _log.default, skipCheck: true }).then(({ db, initConfig, nativeContracts }) => {
17+
(0, _dataSource.setup)({ log: _log.default, skipCheck: true }).then(({ db, initConfig }) => {
1818
_log.default.info('Database connected');
1919

2020
// data collectors
21-
const api = new _Api.default({ db, initConfig, nativeContracts }, _config.default.api);
21+
const api = new _Api.default({ db, initConfig }, _config.default.api);
2222
const status = new _Status.default(db);
2323
const txPool = new _TxPool.default(db);
2424
api.start();
@@ -39,7 +39,7 @@ const address = _config.default.api.address || 'localhost';
3939

4040
// create channels
4141
const channels = (0, _channels.createChannels)(io);
42-
const { blocksChannel, statusChannel, txPoolChannel, statsChannel } = channels.channels;
42+
const { blocksChannel, statusChannel, txPoolChannel, statsChannel, txsChannel } = channels.channels;
4343

4444
// send blocks on join
4545
blocksChannel.on('join', socket => {
@@ -57,9 +57,19 @@ const address = _config.default.api.address || 'localhost';
5757
socket.emit('data', (0, _apiTools.formatRes)({ action: 'txPoolChart', result: txPool.getPoolChart() }));
5858
});
5959

60-
// send new blocks to channel
60+
// send transactions on join
61+
txsChannel.on('join', socket => {
62+
socket.emit('data', (0, _apiTools.formatRes)({ action: 'newTransactions', result: api.getLastTransactions() }));
63+
});
64+
// send new blocks & transactions to channels
6165
api.events.on('newBlocks', result => {
6266
blocksChannel.emit('newBlocks', result);
67+
txsChannel.emit('newTransactions', api.getLastTransactions());
68+
});
69+
70+
// send stats on join
71+
statsChannel.on('join', socket => {
72+
socket.emit('data', (0, _apiTools.formatRes)({ action: 'stats', result: api.getStats() }));
6373
});
6474

6575
// send status to channel

dist/api/lib/Channel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ function Channel(channelName, io) {
2828
}
2929
};
3030

31-
const confirmSubsctption = socket => {
31+
const confirmSubscription = socket => {
3232
socket.emit('subscription', { channel: channelName });
3333
};
3434
const join = socket => {
3535
channelEvent('join', socket);
36-
confirmSubsctption(socket);
36+
confirmSubscription(socket);
3737
};
3838

3939
const leave = socket => {

dist/api/lib/DataCollector/DataCollector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class DataCollector {
7171
}
7272

7373
formatData(data) {
74-
return { data: data };
74+
return { data };
7575
}}exports.DataCollector = DataCollector;var _default =
7676

7777

dist/api/lib/DataCollector/DataCollectorItem.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.formatResponse = formatResponse;exports.getFieldsTypes = getFieldsTypes;exports.filterSort = filterSort;exports.fieldFilterParse = fieldFilterParse;exports.default = exports.DataCollectorItem = void 0;var _mongodb = require("mongodb");
1+
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.formatResponse = formatResponse;exports.getFieldsTypes = getFieldsTypes;exports.filterSort = filterSort;exports.fieldFilterParse = fieldFilterParse;exports.getCursorData = getCursorData;exports.default = exports.DataCollectorItem = void 0;var _mongodb = require("mongodb");
22
var _pagination = require("./pagination");
33
var _types = require("../../../lib/types");
44
class DataCollectorItem {
@@ -37,6 +37,12 @@ class DataCollectorItem {
3737
}
3838
}
3939

40+
async count(query) {
41+
let collection = this.db;
42+
let data = await (0, _pagination.countDocuments)(collection, query);
43+
return { data };
44+
}
45+
4046
async find(query, sort, limit, project) {
4147
let collection = this.db;
4248
project = project || this.getDefaultsFields();
@@ -84,10 +90,9 @@ class DataCollectorItem {
8490
}
8591

8692
async setCursorData() {
87-
const cursorField = this.cursorField;
93+
let { cursorField } = this;
8894
const types = await this.getFieldsTypes();
89-
const cursorType = types[cursorField];
90-
this.cursorData = { cursorField, cursorType, fields: types };
95+
this.cursorData = await getCursorData(this.db, cursorField, types);
9196
return this.cursorData;
9297
}
9398

@@ -101,15 +106,15 @@ class DataCollectorItem {
101106

102107
async getPrevNext(query, project, data) {
103108
try {
104-
let { cursorField } = this;
109+
let { cursorField, db } = this;
105110
project = project || this.getDefaultsFields();
106111
if (!data) data = await this.getOne(query);
107112
if (data) data = data.data;
108113
if (!data) return;
109114
let value = query[cursorField] || data[cursorField];
110115
if (undefined === value) throw new Error(`Missing ${cursorField} value`);
111-
let prev = (await (0, _pagination.find)(this.db, { [cursorField]: { $lt: value } }, { [cursorField]: -1 }, 1, project))[0];
112-
let next = (await (0, _pagination.find)(this.db, { [cursorField]: { $gt: value } }, { [cursorField]: 1 }, 1, project))[0];
116+
let prev = (await (0, _pagination.find)(db, { [cursorField]: { $lt: value } }, { [cursorField]: -1 }, 1, project))[0];
117+
let next = (await (0, _pagination.find)(db, { [cursorField]: { $gt: value } }, { [cursorField]: 1 }, 1, project))[0];
113118
return { prev, data, next };
114119
} catch (err) {
115120
return Promise.reject(err);
@@ -185,6 +190,12 @@ function fieldFilterParse(field, value, query) {
185190
if (ninArr.length) fieldQuery['$nin'] = ninArr;
186191
if (fieldQuery) query[field] = fieldQuery;
187192
return query;
193+
}
194+
195+
async function getCursorData(collection, cursorField, types) {
196+
types = types || (await getFieldsTypes(collection));
197+
const cursorType = types[cursorField];
198+
return { cursorField, cursorType, fields: types };
188199
}var _default =
189200

190201
DataCollectorItem;exports.default = _default;

dist/api/lib/DataCollector/pagination.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.generateCursorQuery = generateCursorQuery;exports.formatSearchValue = formatSearchValue;exports.generateSort = generateSort;exports.generateQuery = generateQuery;exports.parseParams = parseParams;exports.findPages = findPages;exports.aggregatePages = aggregatePages;exports.modifyAggregate = modifyAggregate;exports.getAggregateTotal = getAggregateTotal;exports.paginationResponse = paginationResponse;exports.find = find;exports.encodeValue = encodeValue;exports.generateCursor = generateCursor;var _types = require("../../../lib/types");
1+
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.countDocuments = countDocuments;exports.generateCursorQuery = generateCursorQuery;exports.formatSearchValue = formatSearchValue;exports.generateSort = generateSort;exports.generateQuery = generateQuery;exports.parseParams = parseParams;exports.findPages = findPages;exports.aggregatePages = aggregatePages;exports.modifyAggregate = modifyAggregate;exports.getAggregateTotal = getAggregateTotal;exports.paginationResponse = paginationResponse;exports.find = find;exports.encodeValue = encodeValue;exports.generateCursor = generateCursor;var _types = require("../../../lib/types");
22
var _mongodb = require("mongodb");
33
var _config = _interopRequireDefault(require("../../../lib/config"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}
44
const { MAX_LIMIT, MAX_PAGES } = _config.default.api;
55
const SEPARATOR = '__';
66

7+
async function countDocuments(collection, query) {
8+
query = query || {};
9+
try {
10+
let result = await collection.countDocuments(query);
11+
return result;
12+
} catch (err) {
13+
return Promise.reject(err);
14+
}
15+
}
16+
717
function generateCursorQuery({ cursorField, sortDir, value, sortField }) {
818
if (!value) return;
919
const op = sortDir === -1 ? '$lt' : '$gt';
@@ -113,7 +123,7 @@ async function findPages(collection, cursorData, query, params) {
113123
const $query = generateQuery(params, query);
114124
const $sort = generateSort(params);
115125
let data = !countOnly ? await find(collection, $query, $sort, queryLimit + 1, fields) : null;
116-
let total = count ? await collection.countDocuments(query) : null;
126+
let total = count ? await countDocuments(collection, query) : null;
117127
return paginationResponse(params, data, total);
118128
} catch (err) {
119129
return Promise.reject(err);

0 commit comments

Comments
 (0)