1
+ //@ts -types='./fadroma/toolbox/cmds/cmds.ts'
1
2
import Commands from "@hackbg/cmds"
2
3
import { bold } from "@hackbg/logs"
3
- import EventEmitter from 'node:events'
4
-
4
+ import type { Console } from "@hackbg/logs"
5
+ import process from 'node:process'
6
+ //@ts -types='./fadroma/packages/namada/Namada.ts'
7
+ import type { Validator } from "@fadroma/namada"
8
+
9
+ // For examples how to define commands, see:
10
+ // https://github.com/hackbg/fadroma/blob/v2/packages/namada/namada.ts
11
+ //
12
+ // Note that in this module it is preferable to use dynamic imports
13
+ // at the top of each command rather than all at the top of the file,
14
+ // so that temporary breakage in one code path does not break
15
+ // potentially unrelated entrypoints.
5
16
export default class UndexerCommands extends Commands {
6
- // see https://github.com/hackbg/fadroma/blob/v2/packages/namada/namada.ts
7
- // for examples how to define commands
8
17
9
- constructor ( ...args ) {
18
+ declare log : Console // FIXME
19
+
20
+ constructor ( ...args : ConstructorParameters < typeof Commands > ) {
10
21
super ( ...args )
11
22
this . log . label = ''
12
23
}
@@ -84,7 +95,7 @@ export default class UndexerCommands extends Commands {
84
95
} , async ( height ?: number ) => {
85
96
const t0 = performance . now ( )
86
97
const { default : getRPC } = await import ( './src/rpc.js' )
87
- const chain = await getRPC ( height )
98
+ const chain = await getRPC ( )
88
99
// Fetch and decode block
89
100
const block = await chain . fetchBlock ( { height } )
90
101
height ??= block . height
@@ -107,9 +118,9 @@ export default class UndexerCommands extends Commands {
107
118
args : '[HEIGHT]'
108
119
} , async ( height ?: number ) => {
109
120
const t0 = performance . now ( )
110
- const { updateBlock } = await import ( './src/block .js' )
121
+ const { Updater } = await import ( './src/updater .js' )
111
122
const { default : getRPC } = await import ( './src/rpc.js' )
112
- const chain = await getRPC ( height )
123
+ const chain = await getRPC ( )
113
124
// Fetch and decode block
114
125
const block = await chain . fetchBlock ( { height } )
115
126
height ??= block . height
@@ -127,7 +138,8 @@ export default class UndexerCommands extends Commands {
127
138
const { default : db } = await import ( './src/db.js' )
128
139
await db . sync ( )
129
140
this . log . br ( ) . log ( 'Saving block' , height , 'to database...' ) . br ( )
130
- await updateBlock ( { chain, height, block, } )
141
+ const updater = new Updater ( { log : this . log , chain } )
142
+ await updater . updateBlock ( { height, block, } )
131
143
this . log . info ( 'Done in' , performance . now ( ) - t0 , 'msec' )
132
144
} )
133
145
@@ -142,7 +154,7 @@ export default class UndexerCommands extends Commands {
142
154
epoch,
143
155
tendermintMetadata : false ,
144
156
namadaMetadata : false ,
145
- } ) ) . map ( v => v . namadaAddress ) . sort ( )
157
+ } ) ) . map ( ( v : Validator ) => v . namadaAddress ) . sort ( )
146
158
if ( epoch ) {
147
159
this . log . log ( `Validators at epoch ${ epoch } :` )
148
160
} else {
@@ -151,12 +163,12 @@ export default class UndexerCommands extends Commands {
151
163
for ( const address of addresses ) {
152
164
this . log . log ( 'Validator:' , address )
153
165
}
154
- if ( epoch ) {
155
- this . log . log ( addresses . length , `validator(s) at epoch` , epoch )
156
- } else {
157
- this . log . log ( addresses . length , ' validator(s) at current epoch.' )
158
- }
159
- this . log . br ( ) . info ( "Use the 'validators fetch all' command to get details." )
166
+ this . log
167
+ . log ( addresses . length , epoch
168
+ ? `validator(s) at epoch ${ epoch } `
169
+ : ` validator(s) at current epoch` )
170
+ . br ( )
171
+ . info ( "Use the 'validators fetch all' command to get details." )
160
172
} )
161
173
162
174
validatorsFetchAll = this . command ( {
@@ -166,11 +178,11 @@ export default class UndexerCommands extends Commands {
166
178
const { default : getRPC } = await import ( './src/rpc.js' )
167
179
const chain = await getRPC ( )
168
180
const validators = Object . values ( await chain . fetchValidators ( ) )
169
- const states = { }
181
+ const states : Record < string , number > = { }
170
182
for ( const validator of validators ) {
171
183
this . log . br ( ) . log ( validator )
172
- states [ validator . state ?. state ] ??= 0
173
- states [ validator . state ?. state ] ++
184
+ states [ ( validator as Validator ) . state ?. state ] ??= 0
185
+ states [ ( validator as Validator ) . state ?. state ] ++
174
186
}
175
187
this . log . br ( ) . info ( validators . length , "validators." )
176
188
for ( const [ state , count ] of Object . entries ( states ) ) {
@@ -184,17 +196,17 @@ export default class UndexerCommands extends Commands {
184
196
} , async ( ) => {
185
197
const { default : getRPC } = await import ( './src/rpc.js' )
186
198
const chain = await getRPC ( )
187
- const { updateValidators } = await import ( './src/validator .js' )
188
- const validators = await updateValidators ( chain )
189
- for ( const i in validators ) {
199
+ const { Updater } = await import ( './src/updater .js' )
200
+ const validators = await new Updater ( { chain } ) . updateAllValidators ( chain )
201
+ for ( const i in validators || [ ] ) {
190
202
console . log ( `#${ Number ( i ) + 1 } :` , validators [ i ] )
191
203
}
192
204
} )
193
205
194
206
validatorsStates = this . command ( {
195
207
name : 'validators states' ,
196
208
info : 'count validators in database by state'
197
- } , async ( height : number ) => {
209
+ } , async ( ) => {
198
210
const { callRoute, dbValidatorStates } = await import ( './src/routes.js' )
199
211
const states = await callRoute ( dbValidatorStates )
200
212
console . log ( { states} )
@@ -203,7 +215,7 @@ export default class UndexerCommands extends Commands {
203
215
validatorsQuery = this . command ( {
204
216
name : 'validators query' ,
205
217
info : 'query validators from db'
206
- } , async ( height : number ) => {
218
+ } , async ( ) => {
207
219
const { validatorsTop } = await import ( './src/query.js' )
208
220
const validators = ( await validatorsTop ( ) ) . map ( x => x . toJSON ( ) ) ;
209
221
console . log ( validators )
@@ -212,8 +224,8 @@ export default class UndexerCommands extends Commands {
212
224
for ( const { publicKey } of validators ) {
213
225
console . log ( await new Promise ( ( resolve , reject ) => dbValidatorByHash (
214
226
{ query : { publicKey, uptime : 100 } } ,
215
- { status : code => ( {
216
- send : ( data ) => ( code === 200 )
227
+ { status : ( code : number ) => ( {
228
+ send : ( data : unknown ) => ( code === 200 )
217
229
? resolve ( data )
218
230
: reject ( Object . assign ( new Error ( `route returned ${ code } ` ) , data ) )
219
231
} ) } ) ) )
@@ -226,7 +238,7 @@ export default class UndexerCommands extends Commands {
226
238
args : 'ADDRESS' ,
227
239
} , async ( address : string ) => {
228
240
const { becomeValidatorCount, becomeValidatorList } = await import ( './src/query.js' )
229
- let t0 = performance . now ( )
241
+ const t0 = performance . now ( )
230
242
this . log
231
243
. log ( await becomeValidatorCount ( { address } ) ,
232
244
'becomeValidator(s) for' , bold ( address ) )
@@ -240,7 +252,7 @@ export default class UndexerCommands extends Commands {
240
252
args : 'ADDRESS' ,
241
253
} , async ( validator : string ) => {
242
254
const { changeValidatorMetadataCount, changeValidatorMetadataList } = await import ( './src/query.js' )
243
- let t0 = performance . now ( )
255
+ const t0 = performance . now ( )
244
256
this . log
245
257
. log ( await changeValidatorMetadataCount ( { validator } ) ,
246
258
'changeValidatorMetadata(s) for' , bold ( validator ) )
@@ -254,7 +266,7 @@ export default class UndexerCommands extends Commands {
254
266
args : 'ADDRESS' ,
255
267
} , async ( address : string ) => {
256
268
const { deactivateValidatorCount, deactivateValidatorList } = await import ( './src/query.js' )
257
- let t0 = performance . now ( )
269
+ const t0 = performance . now ( )
258
270
this . log
259
271
. log ( await deactivateValidatorCount ( { address } ) ,
260
272
'deactivateValidator(s) for' , bold ( address ) )
@@ -275,12 +287,11 @@ export default class UndexerCommands extends Commands {
275
287
proposalCount = this . command ( {
276
288
name : 'proposal count' ,
277
289
info : 'fetch count of proposals from chain'
278
- } , ( id : string ) =>
279
- import ( './src/rpc.js' )
280
- . then ( ( { default : getRPC } ) => getRPC ( ) )
281
- . then ( chain => chain . fetchProposalCount ( ) )
282
- . then ( ( count ) => this . log
283
- . log ( 'Proposals on chain:' , count ) ) )
290
+ } , ( ) => import ( './src/rpc.js' )
291
+ . then ( ( { default : getRPC } ) => getRPC ( ) )
292
+ . then ( chain => chain . fetchProposalCount ( ) )
293
+ . then ( ( count ) => this . log
294
+ . log ( 'Proposals on chain:' , count ) ) )
284
295
285
296
proposalFetch = this . command ( {
286
297
name : 'proposal fetch' ,
@@ -337,8 +348,9 @@ export default class UndexerCommands extends Commands {
337
348
info : 'fetch and store proposal from chain'
338
349
} , async ( id : string ) => {
339
350
const chain = await import ( './src/rpc.js' ) . then ( ( { default : getRPC } ) => getRPC ( ) )
340
- const { updateProposal } = await import ( './src/proposal.js' )
341
- await updateProposal ( chain , id )
351
+ const { Updater } = await import ( './src/updater.js' )
352
+ const updater = new Updater ( { chain } )
353
+ await updater . updateProposal ( id )
342
354
} )
343
355
344
356
epoch = this . command ( {
@@ -352,7 +364,7 @@ export default class UndexerCommands extends Commands {
352
364
height : ( height === undefined ) ? undefined : Number ( height )
353
365
} ) )
354
366
. then ( this . log )
355
- . catch ( this . error ) )
367
+ . catch ( this . log . error ) )
356
368
357
369
transfersBy = this . command ( {
358
370
name : 'transfers by' ,
@@ -371,7 +383,7 @@ export default class UndexerCommands extends Commands {
371
383
args : 'ADDRESS'
372
384
} , async ( source : string ) => {
373
385
const { bondCount, bondList } = await import ( './src/query.js' )
374
- let t0 = performance . now ( )
386
+ const t0 = performance . now ( )
375
387
this . log
376
388
. log ( await bondCount ( { source } ) , 'bond(s) from' , bold ( source ) )
377
389
. log ( await bondList ( { source } ) )
@@ -384,7 +396,7 @@ export default class UndexerCommands extends Commands {
384
396
args : 'ADDRESS'
385
397
} , async ( validator : string ) => {
386
398
const { bondCount, bondList } = await import ( './src/query.js' )
387
- let t0 = performance . now ( )
399
+ const t0 = performance . now ( )
388
400
this . log
389
401
. log ( await bondCount ( { validator } ) , 'bond(s) to' , bold ( validator ) )
390
402
. log ( await bondList ( { validator } ) )
@@ -397,7 +409,7 @@ export default class UndexerCommands extends Commands {
397
409
args : 'ADDRESS'
398
410
} , async ( source : string ) => {
399
411
const { unbondCount, unbondList } = await import ( './src/query.js' )
400
- let t0 = performance . now ( )
412
+ const t0 = performance . now ( )
401
413
this . log
402
414
. log ( await unbondCount ( { source } ) , 'unbond(s) from' , bold ( source ) )
403
415
. log ( await unbondList ( { source } ) )
@@ -410,7 +422,7 @@ export default class UndexerCommands extends Commands {
410
422
args : 'ADDRESS'
411
423
} , async ( validator : string ) => {
412
424
const { unbondCount, unbondList } = await import ( './src/query.js' )
413
- let t0 = performance . now ( )
425
+ const t0 = performance . now ( )
414
426
this . log
415
427
. log ( await unbondCount ( { validator } ) , 'unbond(s) to' , bold ( validator ) )
416
428
. log ( await unbondList ( { validator } ) )
@@ -428,13 +440,14 @@ export default class UndexerCommands extends Commands {
428
440
const txs = await Transaction . findAll ( { where, attributes } )
429
441
const blocks = new Set ( txs . map ( tx => tx . get ( ) . blockHeight ) . filter ( height => height >= minHeight ) )
430
442
this . log ( blocks . size , 'blocks containing transaction' )
431
- const { updateBlock } = await import ( './src/block .js' )
443
+ const { Updater } = await import ( './src/updater .js' )
432
444
const chain = await import ( './src/rpc.js' ) . then ( ( { default : getRPC } ) => getRPC ( ) )
433
445
for ( const height of [ ...new Set ( blocks ) ] . sort ( ) ) {
434
446
while ( true ) {
435
447
this . log ( 'Reindexing block' , height )
436
448
try {
437
- await updateBlock ( { chain, height } )
449
+ const updater = new Updater ( { chain } )
450
+ await updater . updateBlock ( { height, block : undefined } )
438
451
break
439
452
} catch ( e ) {
440
453
console . error ( e )
@@ -451,7 +464,7 @@ export default class UndexerCommands extends Commands {
451
464
args : 'ADDRESS'
452
465
} , async ( address : string ) => {
453
466
const { txWithAddressCount, txWithAddressList } = await import ( './src/query.js' )
454
- let t0 = performance . now ( )
467
+ const t0 = performance . now ( )
455
468
this . log
456
469
. log ( await txWithAddressCount ( { address } ) , 'tx(s) with' , bold ( address ) )
457
470
. log ( await txWithAddressList ( { address } ) )
0 commit comments