@@ -208,14 +208,15 @@ class AleoNetworkClient {
208
208
/**
209
209
* Attempt to find records in the Aleo blockchain.
210
210
*
211
- * @param {number } startHeight - The height at which to start searching for unspent records
212
- * @param {number } endHeight - The height at which to stop searching for unspent records
213
- * @param {boolean } unspent - Whether to search for unspent records only
214
- * @param {string[] } programs - The program(s) to search for unspent records in
215
- * @param {number[] } amounts - The amounts (in microcredits) to search for (eg. [100, 200, 3000])
216
- * @param {number } maxMicrocredits - The maximum number of microcredits to search for
217
- * @param {string[] } nonces - The nonces of already found records to exclude from the search
218
- * @param {string | PrivateKey } privateKey - An optional private key to use to find unspent records.
211
+ * @param {Object } params
212
+ * @param {number } params.startHeight - The height at which to start searching for unspent records
213
+ * @param {number } [params.endHeight] - The height at which to stop searching for unspent records
214
+ * @param {boolean } [params.unspent=false] - Whether to search for unspent records only
215
+ * @param {string[] } [params.programs] - The program(s) to search for unspent records in
216
+ * @param {number[] } [params.amounts] - The amounts (in microcredits) to search for (eg. [100, 200, 3000])
217
+ * @param {number } [params.maxMicrocredits] - The maximum number of microcredits to search for
218
+ * @param {string[] } [params.nonces] - The nonces of already found records to exclude from the search
219
+ * @param {string | PrivateKey } [params.privateKey] - An optional private key to use to find unspent records.
219
220
* @returns {Promise<Array<RecordPlaintext>> } An array of records belonging to the account configured in the network client.
220
221
*
221
222
* @example
@@ -231,25 +232,32 @@ class AleoNetworkClient {
231
232
* // Find specific amounts
232
233
* const startHeight = 500000;
233
234
* const amounts = [600000, 1000000];
234
- * const records = networkClient.findRecords(startHeight, undefined, true, ["credits.aleo"] amounts);
235
+ * const records = networkClient.findRecords({ startHeight, unspent: true, programs: ["credits.aleo"], amounts } );
235
236
*
236
237
* // Find specific amounts with a maximum number of cumulative microcredits
237
238
* const maxMicrocredits = 100000;
238
- * const records = networkClient.findRecords(startHeight, undefined, true, ["credits.aleo"] undefined , maxMicrocredits);
239
+ * const records = networkClient.findRecords({ startHeight, unspent: true, programs: ["credits.aleo"], maxMicrocredits } );
239
240
*/
240
- async findRecords (
241
+ async findRecords ( params : {
241
242
startHeight : number ,
242
- endHeight : number | undefined ,
243
- unspent : boolean = false ,
243
+ endHeight ? : number ,
244
+ unspent ? : boolean ,
244
245
programs ?: string [ ] ,
245
- amounts ?: number [ ] | undefined ,
246
- maxMicrocredits ?: number | undefined ,
247
- nonces ?: string [ ] | undefined ,
248
- privateKey ?: string | PrivateKey | undefined ,
249
- ) : Promise < Array < RecordPlaintext > > {
250
- nonces = nonces || [ ] ;
246
+ amounts ?: number [ ] ,
247
+ maxMicrocredits ?: number ,
248
+ nonces ?: string [ ] ,
249
+ privateKey ?: string | PrivateKey ,
250
+ } ) : Promise < Array < RecordPlaintext > > {
251
+ if ( params . unspent == null ) {
252
+ params . unspent = false ;
253
+ }
254
+
255
+ if ( params . nonces == null ) {
256
+ params . nonces = [ ] ;
257
+ }
258
+
251
259
// Ensure start height is not negative
252
- if ( startHeight < 0 ) {
260
+ if ( params . startHeight < 0 ) {
253
261
throw new Error ( "Start height must be greater than or equal to 0" ) ;
254
262
}
255
263
@@ -263,7 +271,7 @@ class AleoNetworkClient {
263
271
let latestHeight : number ;
264
272
265
273
// Ensure a private key is present to find owned records
266
- if ( typeof privateKey === "undefined" ) {
274
+ if ( typeof params . privateKey === "undefined" ) {
267
275
if ( typeof this . account === "undefined" ) {
268
276
throw new Error (
269
277
"Private key must be specified in an argument to findOwnedRecords or set in the AleoNetworkClient" ,
@@ -274,9 +282,9 @@ class AleoNetworkClient {
274
282
} else {
275
283
try {
276
284
resolvedPrivateKey =
277
- privateKey instanceof PrivateKey
278
- ? privateKey
279
- : PrivateKey . from_string ( privateKey ) ;
285
+ params . privateKey instanceof PrivateKey
286
+ ? params . privateKey
287
+ : PrivateKey . from_string ( params . privateKey ) ;
280
288
} catch ( error ) {
281
289
throw new Error ( "Error parsing private key provided." ) ;
282
290
}
@@ -298,24 +306,24 @@ class AleoNetworkClient {
298
306
}
299
307
300
308
// If no end height is specified or is greater than the latest height, set the end height to the latest height
301
- if ( typeof endHeight === "number" && endHeight <= latestHeight ) {
302
- end = endHeight ;
309
+ if ( typeof params . endHeight === "number" && params . endHeight <= latestHeight ) {
310
+ end = params . endHeight ;
303
311
} else {
304
312
end = latestHeight ;
305
313
}
306
314
307
315
// If the starting is greater than the ending height, return an error
308
- if ( startHeight > end ) {
316
+ if ( params . startHeight > end ) {
309
317
throw new Error (
310
318
"Start height must be less than or equal to end height." ,
311
319
) ;
312
320
}
313
321
314
322
// Iterate through blocks in reverse order in chunks of 50
315
- while ( end > startHeight ) {
323
+ while ( end > params . startHeight ) {
316
324
start = end - 50 ;
317
- if ( start < startHeight ) {
318
- start = startHeight ;
325
+ if ( start < params . startHeight ) {
326
+ start = params . startHeight ;
319
327
}
320
328
try {
321
329
// Get 50 blocks (or the difference between the start and end if less than 50)
@@ -352,10 +360,10 @@ class AleoNetworkClient {
352
360
] ;
353
361
// Only search for unspent records in the specified programs.
354
362
if (
355
- ! ( typeof programs === "undefined" )
363
+ ! ( typeof params . programs === "undefined" )
356
364
) {
357
365
if (
358
- ! programs . includes (
366
+ ! params . programs . includes (
359
367
transition . program ,
360
368
)
361
369
) {
@@ -398,15 +406,16 @@ class AleoNetworkClient {
398
406
const nonce =
399
407
recordPlaintext . nonce ( ) ;
400
408
if (
401
- nonces . includes (
409
+ params . nonces . includes (
402
410
nonce ,
403
411
)
404
412
) {
405
413
continue ;
406
414
}
407
415
408
- if ( unspent ) {
416
+ if ( params . unspent ) {
409
417
const recordViewKey = recordPlaintext . recordViewKey ( viewKey ) . toString ( ) ;
418
+
410
419
// Otherwise record the nonce that has been found
411
420
const serialNumber =
412
421
recordPlaintext . serialNumberString (
@@ -432,13 +441,13 @@ class AleoNetworkClient {
432
441
}
433
442
434
443
// Add the record to the list of records if the user did not specify amounts.
435
- if ( ! amounts ) {
444
+ if ( ! params . amounts ) {
436
445
records . push (
437
446
recordPlaintext ,
438
447
) ;
439
448
// If the user specified a maximum number of microcredits, check if the search has found enough
440
449
if (
441
- typeof maxMicrocredits ===
450
+ typeof params . maxMicrocredits ===
442
451
"number"
443
452
) {
444
453
totalRecordValue +=
@@ -447,7 +456,7 @@ class AleoNetworkClient {
447
456
if (
448
457
totalRecordValue >=
449
458
BigInt (
450
- maxMicrocredits ,
459
+ params . maxMicrocredits ,
451
460
)
452
461
) {
453
462
return records ;
@@ -458,16 +467,15 @@ class AleoNetworkClient {
458
467
// If the user specified a list of amounts, check if the search has found them
459
468
if (
460
469
! (
461
- typeof amounts ===
470
+ typeof params . amounts ===
462
471
"undefined"
463
472
) &&
464
- amounts . length >
465
- 0
473
+ params . amounts . length > 0
466
474
) {
467
475
let amounts_found = 0 ;
468
476
if (
469
477
recordPlaintext . microcredits ( ) >
470
- amounts [
478
+ params . amounts [
471
479
amounts_found
472
480
]
473
481
) {
@@ -477,7 +485,7 @@ class AleoNetworkClient {
477
485
) ;
478
486
// If the user specified a maximum number of microcredits, check if the search has found enough
479
487
if (
480
- typeof maxMicrocredits ===
488
+ typeof params . maxMicrocredits ===
481
489
"number"
482
490
) {
483
491
totalRecordValue +=
@@ -486,15 +494,15 @@ class AleoNetworkClient {
486
494
if (
487
495
totalRecordValue >=
488
496
BigInt (
489
- maxMicrocredits ,
497
+ params . maxMicrocredits ,
490
498
)
491
499
) {
492
500
return records ;
493
501
}
494
502
}
495
503
if (
496
504
records . length >=
497
- amounts . length
505
+ params . amounts . length
498
506
) {
499
507
return records ;
500
508
}
@@ -535,13 +543,14 @@ class AleoNetworkClient {
535
543
/**
536
544
* Attempts to find unspent records in the Aleo blockchain.
537
545
*
538
- * @param {number } startHeight - The height at which to start searching for unspent records
539
- * @param {number } endHeight - The height at which to stop searching for unspent records
540
- * @param {string[] } programs - The program(s) to search for unspent records in
541
- * @param {number[] } amounts - The amounts (in microcredits) to search for (eg. [100, 200, 3000])
542
- * @param {number } maxMicrocredits - The maximum number of microcredits to search for
543
- * @param {string[] } nonces - The nonces of already found records to exclude from the search
544
- * @param {string | PrivateKey } privateKey - An optional private key to use to find unspent records.
546
+ * @param {Object } params
547
+ * @param {number } params.startHeight - The height at which to start searching for unspent records
548
+ * @param {number } [params.endHeight] - The height at which to stop searching for unspent records
549
+ * @param {string[] } [params.programs] - The program(s) to search for unspent records in
550
+ * @param {number[] } [params.amounts] - The amounts (in microcredits) to search for (eg. [100, 200, 3000])
551
+ * @param {number } [params.maxMicrocredits] - The maximum number of microcredits to search for
552
+ * @param {string[] } [params.nonces] - The nonces of already found records to exclude from the search
553
+ * @param {string | PrivateKey } [params.privateKey] - An optional private key to use to find unspent records.
545
554
* @returns {Promise<Array<RecordPlaintext>> } An array of unspent records belonging to the account configured in the network client.
546
555
*
547
556
* @example
@@ -557,35 +566,29 @@ class AleoNetworkClient {
557
566
* const startHeight = 500000;
558
567
* const endHeight = 550000;
559
568
* const amounts = [600000, 1000000];
560
- * const records = networkClient.findUnspentRecords(startHeight, endHeight, ["credits.aleo"], amounts);
569
+ * const records = networkClient.findUnspentRecords({ startHeight, endHeight, programs: ["credits.aleo"], amounts } );
561
570
*
562
571
* // Find specific amounts with a maximum number of cumulative microcredits
563
572
* const maxMicrocredits = 100000;
564
- * const records = networkClient.findUnspentRecords(startHeight, undefined, ["credits.aleo"], undefined, maxMicrocredits);
573
+ * const records = networkClient.findUnspentRecords({ startHeight, programs: ["credits.aleo"], maxMicrocredits } );
565
574
*/
566
- async findUnspentRecords (
575
+ async findUnspentRecords ( params : {
567
576
startHeight : number ,
568
- endHeight : number | undefined ,
577
+ endHeight ? : number ,
569
578
programs ?: string [ ] ,
570
- amounts ?: number [ ] | undefined ,
571
- maxMicrocredits ?: number | undefined ,
572
- nonces ?: string [ ] | undefined ,
573
- privateKey ?: string | PrivateKey | undefined ,
574
- ) : Promise < Array < RecordPlaintext > > {
579
+ amounts ?: number [ ] ,
580
+ maxMicrocredits ?: number ,
581
+ nonces ?: string [ ] ,
582
+ privateKey ?: string | PrivateKey ,
583
+ } ) : Promise < Array < RecordPlaintext > > {
584
+ this . ctx = { "X-ALEO-METHOD" : "findUnspentRecords" } ;
585
+
575
586
try {
576
- this . ctx = { "X-ALEO-METHOD" : "findUnspentRecords" } ;
577
- return await this . findRecords (
578
- startHeight ,
579
- endHeight ,
580
- true ,
581
- programs ,
582
- amounts ,
583
- maxMicrocredits ,
584
- nonces ,
585
- privateKey ,
586
- ) ;
587
- } catch ( error ) {
588
- throw new Error ( "Error finding unspent records: " + error ) ;
587
+ return await this . findRecords ( {
588
+ ...params ,
589
+ unspent : true ,
590
+ } ) ;
591
+
589
592
} finally {
590
593
this . ctx = { } ;
591
594
}
0 commit comments