Skip to content

Commit cd6cf7a

Browse files
committed
Changing findRecords and findUnspentRecords to use a params object
1 parent 8e56de5 commit cd6cf7a

File tree

4 files changed

+104
-82
lines changed

4 files changed

+104
-82
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"glob": "^11.0.1"
3535
},
3636
"devDependencies": {
37+
"jsdoc": "^4.0.4",
3738
"prettier": "3.4.2",
3839
"wasm-pack": "^0.13.1"
3940
}

sdk/src/network-client.ts

Lines changed: 76 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,15 @@ class AleoNetworkClient {
208208
/**
209209
* Attempt to find records in the Aleo blockchain.
210210
*
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.
219220
* @returns {Promise<Array<RecordPlaintext>>} An array of records belonging to the account configured in the network client.
220221
*
221222
* @example
@@ -231,25 +232,32 @@ class AleoNetworkClient {
231232
* // Find specific amounts
232233
* const startHeight = 500000;
233234
* 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 });
235236
*
236237
* // Find specific amounts with a maximum number of cumulative microcredits
237238
* 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 });
239240
*/
240-
async findRecords(
241+
async findRecords(params: {
241242
startHeight: number,
242-
endHeight: number | undefined,
243-
unspent: boolean = false,
243+
endHeight?: number,
244+
unspent?: boolean,
244245
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+
251259
// Ensure start height is not negative
252-
if (startHeight < 0) {
260+
if (params.startHeight < 0) {
253261
throw new Error("Start height must be greater than or equal to 0");
254262
}
255263

@@ -263,7 +271,7 @@ class AleoNetworkClient {
263271
let latestHeight: number;
264272

265273
// Ensure a private key is present to find owned records
266-
if (typeof privateKey === "undefined") {
274+
if (typeof params.privateKey === "undefined") {
267275
if (typeof this.account === "undefined") {
268276
throw new Error(
269277
"Private key must be specified in an argument to findOwnedRecords or set in the AleoNetworkClient",
@@ -274,9 +282,9 @@ class AleoNetworkClient {
274282
} else {
275283
try {
276284
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);
280288
} catch (error) {
281289
throw new Error("Error parsing private key provided.");
282290
}
@@ -298,24 +306,24 @@ class AleoNetworkClient {
298306
}
299307

300308
// 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;
303311
} else {
304312
end = latestHeight;
305313
}
306314

307315
// If the starting is greater than the ending height, return an error
308-
if (startHeight > end) {
316+
if (params.startHeight > end) {
309317
throw new Error(
310318
"Start height must be less than or equal to end height.",
311319
);
312320
}
313321

314322
// Iterate through blocks in reverse order in chunks of 50
315-
while (end > startHeight) {
323+
while (end > params.startHeight) {
316324
start = end - 50;
317-
if (start < startHeight) {
318-
start = startHeight;
325+
if (start < params.startHeight) {
326+
start = params.startHeight;
319327
}
320328
try {
321329
// Get 50 blocks (or the difference between the start and end if less than 50)
@@ -352,10 +360,10 @@ class AleoNetworkClient {
352360
];
353361
// Only search for unspent records in the specified programs.
354362
if (
355-
!(typeof programs === "undefined")
363+
!(typeof params.programs === "undefined")
356364
) {
357365
if (
358-
!programs.includes(
366+
!params.programs.includes(
359367
transition.program,
360368
)
361369
) {
@@ -398,15 +406,16 @@ class AleoNetworkClient {
398406
const nonce =
399407
recordPlaintext.nonce();
400408
if (
401-
nonces.includes(
409+
params.nonces.includes(
402410
nonce,
403411
)
404412
) {
405413
continue;
406414
}
407415

408-
if (unspent) {
416+
if (params.unspent) {
409417
const recordViewKey = recordPlaintext.recordViewKey(viewKey).toString();
418+
410419
// Otherwise record the nonce that has been found
411420
const serialNumber =
412421
recordPlaintext.serialNumberString(
@@ -432,13 +441,13 @@ class AleoNetworkClient {
432441
}
433442

434443
// Add the record to the list of records if the user did not specify amounts.
435-
if (!amounts) {
444+
if (!params.amounts) {
436445
records.push(
437446
recordPlaintext,
438447
);
439448
// If the user specified a maximum number of microcredits, check if the search has found enough
440449
if (
441-
typeof maxMicrocredits ===
450+
typeof params.maxMicrocredits ===
442451
"number"
443452
) {
444453
totalRecordValue +=
@@ -447,7 +456,7 @@ class AleoNetworkClient {
447456
if (
448457
totalRecordValue >=
449458
BigInt(
450-
maxMicrocredits,
459+
params.maxMicrocredits,
451460
)
452461
) {
453462
return records;
@@ -458,16 +467,15 @@ class AleoNetworkClient {
458467
// If the user specified a list of amounts, check if the search has found them
459468
if (
460469
!(
461-
typeof amounts ===
470+
typeof params.amounts ===
462471
"undefined"
463472
) &&
464-
amounts.length >
465-
0
473+
params.amounts.length > 0
466474
) {
467475
let amounts_found = 0;
468476
if (
469477
recordPlaintext.microcredits() >
470-
amounts[
478+
params.amounts[
471479
amounts_found
472480
]
473481
) {
@@ -477,7 +485,7 @@ class AleoNetworkClient {
477485
);
478486
// If the user specified a maximum number of microcredits, check if the search has found enough
479487
if (
480-
typeof maxMicrocredits ===
488+
typeof params.maxMicrocredits ===
481489
"number"
482490
) {
483491
totalRecordValue +=
@@ -486,15 +494,15 @@ class AleoNetworkClient {
486494
if (
487495
totalRecordValue >=
488496
BigInt(
489-
maxMicrocredits,
497+
params.maxMicrocredits,
490498
)
491499
) {
492500
return records;
493501
}
494502
}
495503
if (
496504
records.length >=
497-
amounts.length
505+
params.amounts.length
498506
) {
499507
return records;
500508
}
@@ -535,13 +543,14 @@ class AleoNetworkClient {
535543
/**
536544
* Attempts to find unspent records in the Aleo blockchain.
537545
*
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.
545554
* @returns {Promise<Array<RecordPlaintext>>} An array of unspent records belonging to the account configured in the network client.
546555
*
547556
* @example
@@ -557,35 +566,29 @@ class AleoNetworkClient {
557566
* const startHeight = 500000;
558567
* const endHeight = 550000;
559568
* 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 });
561570
*
562571
* // Find specific amounts with a maximum number of cumulative microcredits
563572
* const maxMicrocredits = 100000;
564-
* const records = networkClient.findUnspentRecords(startHeight, undefined, ["credits.aleo"], undefined, maxMicrocredits);
573+
* const records = networkClient.findUnspentRecords({ startHeight, programs: ["credits.aleo"], maxMicrocredits });
565574
*/
566-
async findUnspentRecords(
575+
async findUnspentRecords(params: {
567576
startHeight: number,
568-
endHeight: number | undefined,
577+
endHeight?: number,
569578
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+
575586
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+
589592
} finally {
590593
this.ctx = {};
591594
}

sdk/src/record-provider.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,16 @@ class NetworkRecordProvider implements RecordProvider {
225225
logAndThrow("Start height must be less than end height");
226226
}
227227

228-
return await this.networkClient.findRecords(startHeight, endHeight, unspent, ["credits.aleo"], microcredits, maxAmount, nonces, this.account.privateKey());
228+
return await this.networkClient.findRecords({
229+
startHeight,
230+
endHeight,
231+
unspent,
232+
programs: ["credits.aleo"],
233+
amounts: microcredits,
234+
maxMicrocredits: maxAmount,
235+
nonces,
236+
privateKey: this.account.privateKey(),
237+
});
229238
}
230239

231240
/**
@@ -334,7 +343,16 @@ class NetworkRecordProvider implements RecordProvider {
334343
logAndThrow("Start height must be less than end height");
335344
}
336345

337-
return await this.networkClient.findRecords(startHeight, endHeight, unspent, programs, amounts, maxAmount, nonces, this.account.privateKey());
346+
return await this.networkClient.findRecords({
347+
startHeight,
348+
endHeight,
349+
unspent,
350+
programs,
351+
amounts,
352+
maxMicrocredits: maxAmount,
353+
nonces,
354+
privateKey: this.account.privateKey(),
355+
});
338356
}
339357

340358
}

0 commit comments

Comments
 (0)