Skip to content

Commit ee6ca17

Browse files
committed
Minor refactoring
1 parent 291e154 commit ee6ca17

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
@@ -215,16 +215,18 @@ class AleoNetworkClient {
215215
nonces?: string[],
216216
privateKey?: string | PrivateKey,
217217
}): Promise<Array<RecordPlaintext>> {
218-
if (params.unspent == null) {
219-
params.unspent = false;
218+
let { startHeight, endHeight, unspent, programs, amounts, maxMicrocredits, nonces, privateKey } = params;
219+
220+
if (unspent == null) {
221+
unspent = false;
220222
}
221223

222-
if (params.nonces == null) {
223-
params.nonces = [];
224+
if (nonces == null) {
225+
nonces = [];
224226
}
225227

226228
// Ensure start height is not negative
227-
if (params.startHeight < 0) {
229+
if (startHeight < 0) {
228230
throw new Error("Start height must be greater than or equal to 0");
229231
}
230232

@@ -238,7 +240,7 @@ class AleoNetworkClient {
238240
let latestHeight: number;
239241

240242
// Ensure a private key is present to find owned records
241-
if (typeof params.privateKey === "undefined") {
243+
if (typeof privateKey === "undefined") {
242244
if (typeof this.account === "undefined") {
243245
throw new Error(
244246
"Private key must be specified in an argument to findOwnedRecords or set in the AleoNetworkClient",
@@ -249,9 +251,9 @@ class AleoNetworkClient {
249251
} else {
250252
try {
251253
resolvedPrivateKey =
252-
params.privateKey instanceof PrivateKey
253-
? params.privateKey
254-
: PrivateKey.from_string(params.privateKey);
254+
privateKey instanceof PrivateKey
255+
? privateKey
256+
: PrivateKey.from_string(privateKey);
255257
} catch (error) {
256258
throw new Error("Error parsing private key provided.");
257259
}
@@ -273,24 +275,24 @@ class AleoNetworkClient {
273275
}
274276

275277
// If no end height is specified or is greater than the latest height, set the end height to the latest height
276-
if (typeof params.endHeight === "number" && params.endHeight <= latestHeight) {
277-
end = params.endHeight;
278+
if (typeof endHeight === "number" && endHeight <= latestHeight) {
279+
end = endHeight;
278280
} else {
279281
end = latestHeight;
280282
}
281283

282284
// If the starting is greater than the ending height, return an error
283-
if (params.startHeight > end) {
285+
if (startHeight > end) {
284286
throw new Error(
285287
"Start height must be less than or equal to end height.",
286288
);
287289
}
288290

289291
// Iterate through blocks in reverse order in chunks of 50
290-
while (end > params.startHeight) {
292+
while (end > startHeight) {
291293
start = end - 50;
292-
if (start < params.startHeight) {
293-
start = params.startHeight;
294+
if (start < startHeight) {
295+
start = startHeight;
294296
}
295297
try {
296298
// Get 50 blocks (or the difference between the start and end if less than 50)
@@ -327,10 +329,10 @@ class AleoNetworkClient {
327329
];
328330
// Only search for unspent records in the specified programs.
329331
if (
330-
!(typeof params.programs === "undefined")
332+
!(typeof programs === "undefined")
331333
) {
332334
if (
333-
!params.programs.includes(
335+
!programs.includes(
334336
transition.program,
335337
)
336338
) {
@@ -373,14 +375,14 @@ class AleoNetworkClient {
373375
const nonce =
374376
recordPlaintext.nonce();
375377
if (
376-
params.nonces.includes(
378+
nonces.includes(
377379
nonce,
378380
)
379381
) {
380382
continue;
381383
}
382384

383-
if (params.unspent) {
385+
if (unspent) {
384386
const recordViewKey = recordPlaintext.recordViewKey(viewKey).toString();
385387

386388
// Otherwise record the nonce that has been found
@@ -408,13 +410,13 @@ class AleoNetworkClient {
408410
}
409411

410412
// Add the record to the list of records if the user did not specify amounts.
411-
if (!params.amounts) {
413+
if (!amounts) {
412414
records.push(
413415
recordPlaintext,
414416
);
415417
// If the user specified a maximum number of microcredits, check if the search has found enough
416418
if (
417-
typeof params.maxMicrocredits ===
419+
typeof maxMicrocredits ===
418420
"number"
419421
) {
420422
totalRecordValue +=
@@ -423,7 +425,7 @@ class AleoNetworkClient {
423425
if (
424426
totalRecordValue >=
425427
BigInt(
426-
params.maxMicrocredits,
428+
maxMicrocredits,
427429
)
428430
) {
429431
return records;
@@ -434,15 +436,15 @@ class AleoNetworkClient {
434436
// If the user specified a list of amounts, check if the search has found them
435437
if (
436438
!(
437-
typeof params.amounts ===
439+
typeof amounts ===
438440
"undefined"
439441
) &&
440-
params.amounts.length > 0
442+
amounts.length > 0
441443
) {
442444
let amounts_found = 0;
443445
if (
444446
recordPlaintext.microcredits() >
445-
params.amounts[
447+
amounts[
446448
amounts_found
447449
]
448450
) {
@@ -452,7 +454,7 @@ class AleoNetworkClient {
452454
);
453455
// If the user specified a maximum number of microcredits, check if the search has found enough
454456
if (
455-
typeof params.maxMicrocredits ===
457+
typeof maxMicrocredits ===
456458
"number"
457459
) {
458460
totalRecordValue +=
@@ -461,15 +463,15 @@ class AleoNetworkClient {
461463
if (
462464
totalRecordValue >=
463465
BigInt(
464-
params.maxMicrocredits,
466+
maxMicrocredits,
465467
)
466468
) {
467469
return records;
468470
}
469471
}
470472
if (
471473
records.length >=
472-
params.amounts.length
474+
amounts.length
473475
) {
474476
return records;
475477
}
@@ -1249,13 +1251,15 @@ class AleoNetworkClient {
12491251
mappingName: string,
12501252
key: string | Plaintext,
12511253
}): Promise<Plaintext> {
1254+
const { programId, mappingName, key } = params;
1255+
12521256
this.ctx = { "X-ALEO-METHOD": "getProgramMappingPlaintext" };
12531257

12541258
try {
1255-
const keyString = params.key instanceof Plaintext ? params.key.toString() : params.key;
1259+
const keyString = key instanceof Plaintext ? key.toString() : key;
12561260

12571261
const value = await this.fetchRaw(
1258-
`/program/${params.programId}/mapping/${params.mappingName}/${keyString}`,
1262+
`/program/${programId}/mapping/${mappingName}/${keyString}`,
12591263
);
12601264

12611265
return Plaintext.fromString(JSON.parse(value));
@@ -1645,15 +1649,13 @@ class AleoNetworkClient {
16451649
* // Wait for the transaction to be confirmed.
16461650
* const transaction = await networkClient.waitForTransactionConfirmation({ transactionId });
16471651
*/
1648-
async waitForTransactionConfirmation({
1649-
transactionId,
1650-
checkInterval = 2000,
1651-
timeout = 45000,
1652-
}: {
1652+
async waitForTransactionConfirmation(params: {
16531653
transactionId: string,
16541654
checkInterval?: number,
16551655
timeout?: number,
16561656
}): Promise<ConfirmedTransactionJSON> {
1657+
const { transactionId, checkInterval = 2000, timeout = 45000 } = params;
1658+
16571659
const startTime = Date.now();
16581660

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