Skip to content

Commit 7340802

Browse files
committed
Minor refactoring
1 parent 31b59ef commit 7340802

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
@@ -230,16 +230,18 @@ class AleoNetworkClient {
230230
nonces?: string[],
231231
privateKey?: string | PrivateKey,
232232
}): Promise<Array<RecordPlaintext>> {
233-
if (params.unspent == null) {
234-
params.unspent = false;
233+
let { startHeight, endHeight, unspent, programs, amounts, maxMicrocredits, nonces, privateKey } = params;
234+
235+
if (unspent == null) {
236+
unspent = false;
235237
}
236238

237-
if (params.nonces == null) {
238-
params.nonces = [];
239+
if (nonces == null) {
240+
nonces = [];
239241
}
240242

241243
// Ensure start height is not negative
242-
if (params.startHeight < 0) {
244+
if (startHeight < 0) {
243245
throw new Error("Start height must be greater than or equal to 0");
244246
}
245247

@@ -253,7 +255,7 @@ class AleoNetworkClient {
253255
let latestHeight: number;
254256

255257
// Ensure a private key is present to find owned records
256-
if (typeof params.privateKey === "undefined") {
258+
if (typeof privateKey === "undefined") {
257259
if (typeof this.account === "undefined") {
258260
throw new Error(
259261
"Private key must be specified in an argument to findOwnedRecords or set in the AleoNetworkClient",
@@ -264,9 +266,9 @@ class AleoNetworkClient {
264266
} else {
265267
try {
266268
resolvedPrivateKey =
267-
params.privateKey instanceof PrivateKey
268-
? params.privateKey
269-
: PrivateKey.from_string(params.privateKey);
269+
privateKey instanceof PrivateKey
270+
? privateKey
271+
: PrivateKey.from_string(privateKey);
270272
} catch (error) {
271273
throw new Error("Error parsing private key provided.");
272274
}
@@ -288,24 +290,24 @@ class AleoNetworkClient {
288290
}
289291

290292
// If no end height is specified or is greater than the latest height, set the end height to the latest height
291-
if (typeof params.endHeight === "number" && params.endHeight <= latestHeight) {
292-
end = params.endHeight;
293+
if (typeof endHeight === "number" && endHeight <= latestHeight) {
294+
end = endHeight;
293295
} else {
294296
end = latestHeight;
295297
}
296298

297299
// If the starting is greater than the ending height, return an error
298-
if (params.startHeight > end) {
300+
if (startHeight > end) {
299301
throw new Error(
300302
"Start height must be less than or equal to end height.",
301303
);
302304
}
303305

304306
// Iterate through blocks in reverse order in chunks of 50
305-
while (end > params.startHeight) {
307+
while (end > startHeight) {
306308
start = end - 50;
307-
if (start < params.startHeight) {
308-
start = params.startHeight;
309+
if (start < startHeight) {
310+
start = startHeight;
309311
}
310312
try {
311313
// Get 50 blocks (or the difference between the start and end if less than 50)
@@ -342,10 +344,10 @@ class AleoNetworkClient {
342344
];
343345
// Only search for unspent records in the specified programs.
344346
if (
345-
!(typeof params.programs === "undefined")
347+
!(typeof programs === "undefined")
346348
) {
347349
if (
348-
!params.programs.includes(
350+
!programs.includes(
349351
transition.program,
350352
)
351353
) {
@@ -388,14 +390,14 @@ class AleoNetworkClient {
388390
const nonce =
389391
recordPlaintext.nonce();
390392
if (
391-
params.nonces.includes(
393+
nonces.includes(
392394
nonce,
393395
)
394396
) {
395397
continue;
396398
}
397399

398-
if (params.unspent) {
400+
if (unspent) {
399401
const recordViewKey = recordPlaintext.recordViewKey(viewKey).toString();
400402

401403
// Otherwise record the nonce that has been found
@@ -423,13 +425,13 @@ class AleoNetworkClient {
423425
}
424426

425427
// Add the record to the list of records if the user did not specify amounts.
426-
if (!params.amounts) {
428+
if (!amounts) {
427429
records.push(
428430
recordPlaintext,
429431
);
430432
// If the user specified a maximum number of microcredits, check if the search has found enough
431433
if (
432-
typeof params.maxMicrocredits ===
434+
typeof maxMicrocredits ===
433435
"number"
434436
) {
435437
totalRecordValue +=
@@ -438,7 +440,7 @@ class AleoNetworkClient {
438440
if (
439441
totalRecordValue >=
440442
BigInt(
441-
params.maxMicrocredits,
443+
maxMicrocredits,
442444
)
443445
) {
444446
return records;
@@ -449,15 +451,15 @@ class AleoNetworkClient {
449451
// If the user specified a list of amounts, check if the search has found them
450452
if (
451453
!(
452-
typeof params.amounts ===
454+
typeof amounts ===
453455
"undefined"
454456
) &&
455-
params.amounts.length > 0
457+
amounts.length > 0
456458
) {
457459
let amounts_found = 0;
458460
if (
459461
recordPlaintext.microcredits() >
460-
params.amounts[
462+
amounts[
461463
amounts_found
462464
]
463465
) {
@@ -467,7 +469,7 @@ class AleoNetworkClient {
467469
);
468470
// If the user specified a maximum number of microcredits, check if the search has found enough
469471
if (
470-
typeof params.maxMicrocredits ===
472+
typeof maxMicrocredits ===
471473
"number"
472474
) {
473475
totalRecordValue +=
@@ -476,15 +478,15 @@ class AleoNetworkClient {
476478
if (
477479
totalRecordValue >=
478480
BigInt(
479-
params.maxMicrocredits,
481+
maxMicrocredits,
480482
)
481483
) {
482484
return records;
483485
}
484486
}
485487
if (
486488
records.length >=
487-
params.amounts.length
489+
amounts.length
488490
) {
489491
return records;
490492
}
@@ -1263,13 +1265,15 @@ class AleoNetworkClient {
12631265
mappingName: string,
12641266
key: string | Plaintext,
12651267
}): Promise<Plaintext> {
1268+
const { programId, mappingName, key } = params;
1269+
12661270
this.ctx = { "X-ALEO-METHOD": "getProgramMappingPlaintext" };
12671271

12681272
try {
1269-
const keyString = params.key instanceof Plaintext ? params.key.toString() : params.key;
1273+
const keyString = key instanceof Plaintext ? key.toString() : key;
12701274

12711275
const value = await this.fetchRaw(
1272-
`/program/${params.programId}/mapping/${params.mappingName}/${keyString}`,
1276+
`/program/${programId}/mapping/${mappingName}/${keyString}`,
12731277
);
12741278

12751279
return Plaintext.fromString(JSON.parse(value));
@@ -1698,15 +1702,13 @@ class AleoNetworkClient {
16981702
* // Wait for the transaction to be confirmed.
16991703
* const transaction = await networkClient.waitForTransactionConfirmation({ transactionId });
17001704
*/
1701-
async waitForTransactionConfirmation({
1702-
transactionId,
1703-
checkInterval = 2000,
1704-
timeout = 45000,
1705-
}: {
1705+
async waitForTransactionConfirmation(params: {
17061706
transactionId: string,
17071707
checkInterval?: number,
17081708
timeout?: number,
17091709
}): Promise<ConfirmedTransactionJSON> {
1710+
const { transactionId, checkInterval = 2000, timeout = 45000 } = params;
1711+
17101712
const startTime = Date.now();
17111713

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