Skip to content

Commit fff8e89

Browse files
committed
Minor refactoring
1 parent ed305fa commit fff8e89

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

sdk/src/network-client.ts

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,18 @@ class AleoNetworkClient {
248248
nonces?: string[],
249249
privateKey?: string | PrivateKey,
250250
}): Promise<Array<RecordPlaintext>> {
251-
if (params.unspent == null) {
252-
params.unspent = false;
251+
let { startHeight, endHeight, unspent, programs, amounts, maxMicrocredits, nonces, privateKey } = params;
252+
253+
if (unspent == null) {
254+
unspent = false;
253255
}
254256

255-
if (params.nonces == null) {
256-
params.nonces = [];
257+
if (nonces == null) {
258+
nonces = [];
257259
}
258260

259261
// Ensure start height is not negative
260-
if (params.startHeight < 0) {
262+
if (startHeight < 0) {
261263
throw new Error("Start height must be greater than or equal to 0");
262264
}
263265

@@ -271,7 +273,7 @@ class AleoNetworkClient {
271273
let latestHeight: number;
272274

273275
// Ensure a private key is present to find owned records
274-
if (typeof params.privateKey === "undefined") {
276+
if (typeof privateKey === "undefined") {
275277
if (typeof this.account === "undefined") {
276278
throw new Error(
277279
"Private key must be specified in an argument to findOwnedRecords or set in the AleoNetworkClient",
@@ -282,9 +284,9 @@ class AleoNetworkClient {
282284
} else {
283285
try {
284286
resolvedPrivateKey =
285-
params.privateKey instanceof PrivateKey
286-
? params.privateKey
287-
: PrivateKey.from_string(params.privateKey);
287+
privateKey instanceof PrivateKey
288+
? privateKey
289+
: PrivateKey.from_string(privateKey);
288290
} catch (error) {
289291
throw new Error("Error parsing private key provided.");
290292
}
@@ -306,24 +308,24 @@ class AleoNetworkClient {
306308
}
307309

308310
// If no end height is specified or is greater than the latest height, set the end height to the latest height
309-
if (typeof params.endHeight === "number" && params.endHeight <= latestHeight) {
310-
end = params.endHeight;
311+
if (typeof endHeight === "number" && endHeight <= latestHeight) {
312+
end = endHeight;
311313
} else {
312314
end = latestHeight;
313315
}
314316

315317
// If the starting is greater than the ending height, return an error
316-
if (params.startHeight > end) {
318+
if (startHeight > end) {
317319
throw new Error(
318320
"Start height must be less than or equal to end height.",
319321
);
320322
}
321323

322324
// Iterate through blocks in reverse order in chunks of 50
323-
while (end > params.startHeight) {
325+
while (end > startHeight) {
324326
start = end - 50;
325-
if (start < params.startHeight) {
326-
start = params.startHeight;
327+
if (start < startHeight) {
328+
start = startHeight;
327329
}
328330
try {
329331
// Get 50 blocks (or the difference between the start and end if less than 50)
@@ -360,10 +362,10 @@ class AleoNetworkClient {
360362
];
361363
// Only search for unspent records in the specified programs.
362364
if (
363-
!(typeof params.programs === "undefined")
365+
!(typeof programs === "undefined")
364366
) {
365367
if (
366-
!params.programs.includes(
368+
!programs.includes(
367369
transition.program,
368370
)
369371
) {
@@ -406,14 +408,14 @@ class AleoNetworkClient {
406408
const nonce =
407409
recordPlaintext.nonce();
408410
if (
409-
params.nonces.includes(
411+
nonces.includes(
410412
nonce,
411413
)
412414
) {
413415
continue;
414416
}
415417

416-
if (params.unspent) {
418+
if (unspent) {
417419
const recordViewKey = recordPlaintext.recordViewKey(viewKey).toString();
418420

419421
// Otherwise record the nonce that has been found
@@ -441,13 +443,13 @@ class AleoNetworkClient {
441443
}
442444

443445
// Add the record to the list of records if the user did not specify amounts.
444-
if (!params.amounts) {
446+
if (!amounts) {
445447
records.push(
446448
recordPlaintext,
447449
);
448450
// If the user specified a maximum number of microcredits, check if the search has found enough
449451
if (
450-
typeof params.maxMicrocredits ===
452+
typeof maxMicrocredits ===
451453
"number"
452454
) {
453455
totalRecordValue +=
@@ -456,7 +458,7 @@ class AleoNetworkClient {
456458
if (
457459
totalRecordValue >=
458460
BigInt(
459-
params.maxMicrocredits,
461+
maxMicrocredits,
460462
)
461463
) {
462464
return records;
@@ -467,15 +469,15 @@ class AleoNetworkClient {
467469
// If the user specified a list of amounts, check if the search has found them
468470
if (
469471
!(
470-
typeof params.amounts ===
472+
typeof amounts ===
471473
"undefined"
472474
) &&
473-
params.amounts.length > 0
475+
amounts.length > 0
474476
) {
475477
let amounts_found = 0;
476478
if (
477479
recordPlaintext.microcredits() >
478-
params.amounts[
480+
amounts[
479481
amounts_found
480482
]
481483
) {
@@ -485,7 +487,7 @@ class AleoNetworkClient {
485487
);
486488
// If the user specified a maximum number of microcredits, check if the search has found enough
487489
if (
488-
typeof params.maxMicrocredits ===
490+
typeof maxMicrocredits ===
489491
"number"
490492
) {
491493
totalRecordValue +=
@@ -494,15 +496,15 @@ class AleoNetworkClient {
494496
if (
495497
totalRecordValue >=
496498
BigInt(
497-
params.maxMicrocredits,
499+
maxMicrocredits,
498500
)
499501
) {
500502
return records;
501503
}
502504
}
503505
if (
504506
records.length >=
505-
params.amounts.length
507+
amounts.length
506508
) {
507509
return records;
508510
}
@@ -1281,13 +1283,15 @@ class AleoNetworkClient {
12811283
mappingName: string,
12821284
key: string | Plaintext,
12831285
}): Promise<Plaintext> {
1286+
const { programId, mappingName, key } = params;
1287+
12841288
this.ctx = { "X-ALEO-METHOD": "getProgramMappingPlaintext" };
12851289

12861290
try {
1287-
const keyString = params.key instanceof Plaintext ? params.key.toString() : params.key;
1291+
const keyString = key instanceof Plaintext ? key.toString() : key;
12881292

12891293
const value = await this.fetchRaw(
1290-
`/program/${params.programId}/mapping/${params.mappingName}/${keyString}`,
1294+
`/program/${programId}/mapping/${mappingName}/${keyString}`,
12911295
);
12921296

12931297
return Plaintext.fromString(JSON.parse(value));
@@ -1717,15 +1721,13 @@ class AleoNetworkClient {
17171721
* // Wait for the transaction to be confirmed.
17181722
* const transaction = await networkClient.waitForTransactionConfirmation({ transactionId });
17191723
*/
1720-
async waitForTransactionConfirmation({
1721-
transactionId,
1722-
checkInterval = 2000,
1723-
timeout = 45000,
1724-
}: {
1724+
async waitForTransactionConfirmation(params: {
17251725
transactionId: string,
17261726
checkInterval?: number,
17271727
timeout?: number,
17281728
}): Promise<ConfirmedTransactionJSON> {
1729+
const { transactionId, checkInterval = 2000, timeout = 45000 } = params;
1730+
17291731
const startTime = Date.now();
17301732

17311733
return new Promise((resolve, reject) => {

sdk/src/record-provider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ class NetworkRecordProvider implements RecordProvider {
170170
account: Account;
171171
networkClient: AleoNetworkClient;
172172

173-
constructor(account: Account, networkClient: AleoNetworkClient) {
174-
this.account = account;
175-
this.networkClient = networkClient;
173+
constructor(params: { account: Account, networkClient: AleoNetworkClient }) {
174+
this.account = params.account;
175+
this.networkClient = params.networkClient;
176176
}
177177

178178
/**

0 commit comments

Comments
 (0)