Skip to content

Commit 5c021e5

Browse files
committed
Minor refactoring
1 parent 86630a7 commit 5c021e5

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

sdk/src/network-client.ts

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,18 @@ class AleoNetworkClient {
222222
nonces?: string[],
223223
privateKey?: string | PrivateKey,
224224
}): Promise<Array<RecordPlaintext>> {
225-
if (params.unspent == null) {
226-
params.unspent = false;
225+
let { startHeight, endHeight, unspent, programs, amounts, maxMicrocredits, nonces, privateKey } = params;
226+
227+
if (unspent == null) {
228+
unspent = false;
227229
}
228230

229-
if (params.nonces == null) {
230-
params.nonces = [];
231+
if (nonces == null) {
232+
nonces = [];
231233
}
232234

233235
// Ensure start height is not negative
234-
if (params.startHeight < 0) {
236+
if (startHeight < 0) {
235237
throw new Error("Start height must be greater than or equal to 0");
236238
}
237239

@@ -245,7 +247,7 @@ class AleoNetworkClient {
245247
let latestHeight: number;
246248

247249
// Ensure a private key is present to find owned records
248-
if (typeof params.privateKey === "undefined") {
250+
if (typeof privateKey === "undefined") {
249251
if (typeof this.account === "undefined") {
250252
throw new Error(
251253
"Private key must be specified in an argument to findOwnedRecords or set in the AleoNetworkClient",
@@ -256,9 +258,9 @@ class AleoNetworkClient {
256258
} else {
257259
try {
258260
resolvedPrivateKey =
259-
params.privateKey instanceof PrivateKey
260-
? params.privateKey
261-
: PrivateKey.from_string(params.privateKey);
261+
privateKey instanceof PrivateKey
262+
? privateKey
263+
: PrivateKey.from_string(privateKey);
262264
} catch (error) {
263265
throw new Error("Error parsing private key provided.");
264266
}
@@ -280,24 +282,24 @@ class AleoNetworkClient {
280282
}
281283

282284
// If no end height is specified or is greater than the latest height, set the end height to the latest height
283-
if (typeof params.endHeight === "number" && params.endHeight <= latestHeight) {
284-
end = params.endHeight;
285+
if (typeof endHeight === "number" && endHeight <= latestHeight) {
286+
end = endHeight;
285287
} else {
286288
end = latestHeight;
287289
}
288290

289291
// If the starting is greater than the ending height, return an error
290-
if (params.startHeight > end) {
292+
if (startHeight > end) {
291293
throw new Error(
292294
"Start height must be less than or equal to end height.",
293295
);
294296
}
295297

296298
// Iterate through blocks in reverse order in chunks of 50
297-
while (end > params.startHeight) {
299+
while (end > startHeight) {
298300
start = end - 50;
299-
if (start < params.startHeight) {
300-
start = params.startHeight;
301+
if (start < startHeight) {
302+
start = startHeight;
301303
}
302304
try {
303305
// Get 50 blocks (or the difference between the start and end if less than 50)
@@ -334,10 +336,10 @@ class AleoNetworkClient {
334336
];
335337
// Only search for unspent records in the specified programs.
336338
if (
337-
!(typeof params.programs === "undefined")
339+
!(typeof programs === "undefined")
338340
) {
339341
if (
340-
!params.programs.includes(
342+
!programs.includes(
341343
transition.program,
342344
)
343345
) {
@@ -380,14 +382,14 @@ class AleoNetworkClient {
380382
const nonce =
381383
recordPlaintext.nonce();
382384
if (
383-
params.nonces.includes(
385+
nonces.includes(
384386
nonce,
385387
)
386388
) {
387389
continue;
388390
}
389391

390-
if (params.unspent) {
392+
if (unspent) {
391393
// Otherwise record the nonce that has been found
392394
const serialNumber =
393395
recordPlaintext.serialNumberString(
@@ -412,13 +414,13 @@ class AleoNetworkClient {
412414
}
413415

414416
// Add the record to the list of records if the user did not specify amounts.
415-
if (!params.amounts) {
417+
if (!amounts) {
416418
records.push(
417419
recordPlaintext,
418420
);
419421
// If the user specified a maximum number of microcredits, check if the search has found enough
420422
if (
421-
typeof params.maxMicrocredits ===
423+
typeof maxMicrocredits ===
422424
"number"
423425
) {
424426
totalRecordValue +=
@@ -427,7 +429,7 @@ class AleoNetworkClient {
427429
if (
428430
totalRecordValue >=
429431
BigInt(
430-
params.maxMicrocredits,
432+
maxMicrocredits,
431433
)
432434
) {
433435
return records;
@@ -438,16 +440,16 @@ class AleoNetworkClient {
438440
// If the user specified a list of amounts, check if the search has found them
439441
if (
440442
!(
441-
typeof params.amounts ===
443+
typeof amounts ===
442444
"undefined"
443445
) &&
444-
params.amounts.length >
446+
amounts.length >
445447
0
446448
) {
447449
let amounts_found = 0;
448450
if (
449451
recordPlaintext.microcredits() >
450-
params.amounts[
452+
amounts[
451453
amounts_found
452454
]
453455
) {
@@ -457,7 +459,7 @@ class AleoNetworkClient {
457459
);
458460
// If the user specified a maximum number of microcredits, check if the search has found enough
459461
if (
460-
typeof params.maxMicrocredits ===
462+
typeof maxMicrocredits ===
461463
"number"
462464
) {
463465
totalRecordValue +=
@@ -466,15 +468,15 @@ class AleoNetworkClient {
466468
if (
467469
totalRecordValue >=
468470
BigInt(
469-
params.maxMicrocredits,
471+
maxMicrocredits,
470472
)
471473
) {
472474
return records;
473475
}
474476
}
475477
if (
476478
records.length >=
477-
params.amounts.length
479+
amounts.length
478480
) {
479481
return records;
480482
}
@@ -1083,14 +1085,16 @@ class AleoNetworkClient {
10831085
mappingName: string,
10841086
key: string | Plaintext,
10851087
}): Promise<string> {
1088+
const { programId, mappingName, key } = params;
1089+
10861090
try {
1087-
const keyString = params.key instanceof Plaintext ? params.key.toString() : params.key;
1091+
const keyString = key instanceof Plaintext ? key.toString() : key;
10881092
return await this.fetchData<string>(
1089-
`/program/${params.programId}/mapping/${params.mappingName}/${keyString}`,
1093+
`/program/${programId}/mapping/${mappingName}/${keyString}`,
10901094
);
10911095
} catch (error) {
10921096
throw new Error(
1093-
`Error fetching value for key '${params.key}' in mapping '${params.mappingName}' in program '${params.programId}' - ensure the mapping exists and the key is correct`,
1097+
`Error fetching value for key '${key}' in mapping '${mappingName}' in program '${programId}' - ensure the mapping exists and the key is correct`,
10941098
);
10951099
}
10961100
}
@@ -1139,10 +1143,12 @@ class AleoNetworkClient {
11391143
mappingName: string,
11401144
key: string | Plaintext,
11411145
}): Promise<Plaintext> {
1146+
const { programId, mappingName, key } = params;
1147+
11421148
try {
1143-
const keyString = params.key instanceof Plaintext ? params.key.toString() : params.key;
1149+
const keyString = key instanceof Plaintext ? key.toString() : key;
11441150
const value = await this.fetchRaw(
1145-
`/program/${params.programId}/mapping/${params.mappingName}/${keyString}`,
1151+
`/program/${programId}/mapping/${mappingName}/${keyString}`,
11461152
);
11471153
return Plaintext.fromString(JSON.parse(value));
11481154
} catch (error) {
@@ -1499,15 +1505,13 @@ class AleoNetworkClient {
14991505
* // Wait for the transaction to be confirmed.
15001506
* const transaction = await networkClient.waitForTransactionConfirmation({ transactionId });
15011507
*/
1502-
async waitForTransactionConfirmation({
1503-
transactionId,
1504-
checkInterval = 2000,
1505-
timeout = 45000,
1506-
}: {
1508+
async waitForTransactionConfirmation(params: {
15071509
transactionId: string,
15081510
checkInterval?: number,
15091511
timeout?: number,
15101512
}): Promise<ConfirmedTransactionJSON> {
1513+
const { transactionId, checkInterval = 2000, timeout = 45000 } = params;
1514+
15111515
const startTime = Date.now();
15121516

15131517
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)