1
1
"use strict" ; Object . defineProperty ( exports , "__esModule" , { value : true } ) ; exports . default = void 0 ; var _DataCollector = require ( "./lib/DataCollector" ) ;
2
2
var _modules = require ( "./modules" ) ;
3
3
var _types = require ( "../lib/types" ) ;
4
- var _getCirculatingSupply = _interopRequireDefault ( require ( "./lib/getCirculatingSupply" ) ) ;
5
4
var _blocksCollections = require ( "../lib/blocksCollections" ) ;
6
5
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
8
10
9
11
class Api extends _DataCollector . DataCollector {
10
- constructor ( { db, initConfig, nativeContracts } , { modules, collectionsNames, lastBlocks } = { } ) {
12
+ constructor ( { db, initConfig } , { modules, collectionsNames, lastBlocks } = { } ) {
11
13
const collectionName = collectionsNames . Blocks ;
12
14
super ( db , { collectionName } ) ;
13
15
this . collectionsNames = collectionsNames ;
14
16
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 : [ ] } ;
19
21
this . circulatingSupply = null ;
20
22
this . stats = { timestamp : 0 } ;
21
23
this . loadModules ( ( 0 , _modules . getEnabledApiModules ) ( modules ) ) ;
22
24
this . initConfig = initConfig ;
23
- const { isNativeContract } = nativeContracts ;
25
+ const { isNativeContract } = ( 0 , _NativeContracts . default ) ( initConfig ) ;
24
26
this . isNativeContract = isNativeContract ;
27
+ this . tick ( ) ;
25
28
}
26
29
tick ( ) {
27
30
this . setLastBlocks ( ) ;
28
- this . setCirculatingSupply ( ) ;
29
31
}
30
32
31
33
loadModules ( modules ) {
32
34
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
+ }
36
41
} ) ;
37
42
}
38
43
@@ -67,51 +72,48 @@ class Api extends _DataCollector.DataCollector {
67
72
68
73
async setLastBlocks ( ) {
69
74
try {
70
- let { collection , lastLimit } = this ;
75
+ const Block = this . getModule ( 'Block' ) ;
71
76
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 ) ;
79
82
} catch ( err ) {
80
83
this . log . debug ( err ) ;
81
84
}
82
85
}
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 ) ;
92
88
}
93
89
getCirculatingSupply ( ) {
94
90
return this . formatData ( this . circulatingSupply ) ;
95
91
}
96
92
97
93
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 ) ;
101
101
}
102
102
103
103
getLastBlock ( ) {
104
- return this . lastBlocks [ 0 ] || null ;
104
+ let { data } = this . lastBlocks ;
105
+ return data [ 0 ] || null ;
105
106
}
106
107
107
108
updateLastBlocks ( blocks , transactions ) {
109
+ let blockData = blocks . data ;
108
110
this . lastBlocks = blocks ;
109
111
this . lastTransactions = transactions ;
110
112
let latest ;
111
- if ( blocks && blocks [ 0 ] ) latest = blocks [ 0 ] . number ;
113
+ if ( blockData && blockData [ 0 ] ) latest = blockData [ 0 ] . number ;
112
114
if ( latest !== this . latest ) {
113
115
this . latest = latest ;
114
- this . events . emit ( 'newBlocks' , this . formatData ( { blocks , transactions } ) ) ;
116
+ this . events . emit ( 'newBlocks' , this . getLastBlocks ( ) ) ;
115
117
this . updateStats ( ) ;
116
118
}
117
119
}
@@ -132,19 +134,33 @@ class Api extends _DataCollector.DataCollector {
132
134
133
135
async updateStats ( ) {
134
136
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' ) ;
136
140
if ( ! stats ) return ;
137
141
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 ;
145
150
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 ) ;
148
164
}
149
165
} } var _default =
150
166
0 commit comments