Skip to content

Commit be01fda

Browse files
fixed failing record provider tests
1 parent 46c3692 commit be01fda

File tree

6 files changed

+51
-58
lines changed

6 files changed

+51
-58
lines changed

sdk/src/browser.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ import {
4545
BlockHeightSearch,
4646
NetworkRecordProvider,
4747
RecordProvider,
48-
RecordSearchParams,
4948
} from "./record-provider.js";
49+
import {
50+
RecordSearchParams,
51+
} from "./models/record-provider/recordSearchParams.js";
5052

5153
// @TODO: This function is no longer needed, remove it.
5254
async function initializeWasm() {

sdk/src/program-manager.ts

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Account } from "./account.js";
22
import { AleoNetworkClient, AleoNetworkClientOptions, ProgramImports } from "./network-client.js";
33
import { ImportedPrograms, ImportedVerifyingKeys } from "./models/imports.js";
4-
import { RecordProvider, RecordSearchParams } from "./record-provider.js";
4+
import { RecordProvider } from "./record-provider.js";
5+
import { RecordSearchParams } from "./models/record-provider/recordSearchParams.js";
56

67
import {
78
AleoKeyProvider,
@@ -35,6 +36,7 @@ import {
3536
} from "./constants.js";
3637

3738
import { logAndThrow } from "./utils.js";
39+
import { OwnedRecord } from "./models/record-provider/ownedRecord.js";
3840

3941
/**
4042
* Represents the options for executing a transaction in the Aleo network.
@@ -353,14 +355,12 @@ class ProgramManager {
353355
// Get the fee record from the account if it is not provided in the parameters
354356
try {
355357
feeRecord = privateFee
356-
? <RecordPlaintext>(
357-
await this.getCreditsRecord(
358+
? RecordPlaintext.fromString((await this.getCreditsRecord(
358359
priorityFee,
359360
[],
360361
feeRecord,
361362
recordSearchParams,
362-
)
363-
)
363+
)).recordPlaintext?? '')
364364
: undefined;
365365
} catch (e: any) {
366366
logAndThrow(
@@ -577,14 +577,12 @@ class ProgramManager {
577577
// Get the fee record from the account if it is not provided in the parameters
578578
try {
579579
feeRecord = privateFee
580-
? <RecordPlaintext>(
581-
await this.getCreditsRecord(
580+
? RecordPlaintext.fromString((await this.getCreditsRecord(
582581
priorityFee,
583582
[],
584583
feeRecord,
585584
recordSearchParams,
586-
)
587-
)
585+
)).recordPlaintext?? '')
588586
: undefined;
589587
} catch (e: any) {
590588
logAndThrow(
@@ -955,14 +953,12 @@ class ProgramManager {
955953
// Get the fee record from the account if it is not provided in the parameters.
956954
try {
957955
feeRecord = privateFee
958-
? <RecordPlaintext>(
959-
await this.getCreditsRecord(
956+
? RecordPlaintext.fromString((await this.getCreditsRecord(
960957
priorityFee,
961958
[],
962959
feeRecord,
963960
recordSearchParams,
964-
)
965-
)
961+
)).recordPlaintext?? '')
966962
: undefined;
967963
} catch (e: any) {
968964
logAndThrow(
@@ -1293,14 +1289,12 @@ class ProgramManager {
12931289
// Get the fee record from the account if it is not provided in the parameters
12941290
try {
12951291
feeRecord = privateFee
1296-
? <RecordPlaintext>(
1297-
await this.getCreditsRecord(
1292+
? RecordPlaintext.fromString((await this.getCreditsRecord(
12981293
priorityFee,
12991294
[],
13001295
feeRecord,
13011296
recordSearchParams,
1302-
)
1303-
)
1297+
)).recordPlaintext?? '')
13041298
: undefined;
13051299
} catch (e: any) {
13061300
logAndThrow(
@@ -1566,27 +1560,23 @@ class ProgramManager {
15661560
const nonces: string[] = [];
15671561
if (requiresAmountRecord(transferType)) {
15681562
// If the transfer type is private and requires an amount record, get it from the record provider
1569-
amountRecord = <RecordPlaintext>(
1570-
await this.getCreditsRecord(
1563+
amountRecord = RecordPlaintext.fromString((await this.getCreditsRecord(
15711564
priorityFee,
15721565
[],
15731566
amountRecord,
15741567
recordSearchParams,
1575-
)
1576-
);
1568+
)).recordPlaintext?? '');
15771569
nonces.push(amountRecord.nonce());
15781570
} else {
15791571
amountRecord = undefined;
15801572
}
15811573
feeRecord = privateFee
1582-
? <RecordPlaintext>(
1583-
await this.getCreditsRecord(
1574+
? RecordPlaintext.fromString((await this.getCreditsRecord(
15841575
priorityFee,
15851576
nonces,
15861577
feeRecord,
15871578
recordSearchParams,
1588-
)
1589-
)
1579+
)).recordPlaintext?? '')
15901580
: undefined;
15911581
} catch (e: any) {
15921582
logAndThrow(
@@ -2553,21 +2543,25 @@ class ProgramManager {
25532543
nonces: string[],
25542544
record?: RecordPlaintext | string,
25552545
params?: RecordSearchParams,
2556-
): Promise<RecordPlaintext> {
2546+
): Promise<OwnedRecord> {
25572547
try {
2558-
return record instanceof RecordPlaintext
2559-
? record
2560-
: RecordPlaintext.fromString(<string>record);
2548+
// return record instanceof RecordPlaintext
2549+
// ? record
2550+
// : RecordPlaintext.fromString(<string>record);
2551+
if (record && record instanceof RecordPlaintext) {
2552+
record = record.toString();
2553+
}
2554+
return <OwnedRecord>({
2555+
recordPlaintext: record,
2556+
programName: 'credits.aleo',
2557+
recordName: 'credits',
2558+
})
25612559
} catch (e) {
25622560
try {
25632561
const recordProvider = <RecordProvider>this.recordProvider;
2564-
return <RecordPlaintext>(
2565-
await recordProvider.findCreditsRecord(
2566-
amount,
2567-
true,
2568-
nonces,
2569-
params,
2570-
)
2562+
return await recordProvider.findCreditsRecord(
2563+
amount,
2564+
{ ...params, unspent: true, nonces }
25712565
);
25722566
} catch (e: any) {
25732567
logAndThrow(

sdk/src/record-provider.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,10 @@ class NetworkRecordProvider implements RecordProvider {
261261

262262
const recordsPts = await this.networkClient.findRecords(startHeight, endHeight, searchParameters.unspent, ["credits.aleo"], microcredits, maxAmount, searchParameters.nonces, this.account.privateKey());
263263
return recordsPts.map((record) => ({
264-
commitment: record.commitment().toString(),
265-
owner: record.owner().toString(),
266-
programName: 'credits.aleo',
267-
recordName: 'credits',
268-
recordPlaintext: record.to_string(),
269-
tag: record.tag().toString(),
264+
owner: record.owner().toString(),
265+
programName: 'credits.aleo',
266+
recordName: 'credits',
267+
recordPlaintext: record.toString(),
270268
}));
271269
}
272270

@@ -381,10 +379,7 @@ class NetworkRecordProvider implements RecordProvider {
381379

382380
const recordPts = await this.networkClient.findRecords(startHeight, endHeight, searchParameters.unspent, programs, amounts, maxAmount, searchParameters.nonces, this.account.privateKey());
383381
return recordPts.map((record) => ({
384-
commitment: record.commitment().toString(),
385-
owner: record.owner().toString(),
386-
programName: record.program().toString(),
387-
recordName: record.name().toString(),
382+
recordPlaintext: record.toString(),
388383
}));
389384
}
390385

@@ -423,10 +418,10 @@ class BlockHeightSearch implements RecordSearchParams {
423418
startHeight: number;
424419
endHeight: number;
425420
unspent: boolean;
426-
constructor(startHeight: number, endHeight: number, unspent: boolean) {
421+
constructor(startHeight: number, endHeight: number, unspent?: boolean) {
427422
this.startHeight = startHeight;
428423
this.endHeight = endHeight;
429-
this.unspent = unspent;
424+
this.unspent = !!unspent;
430425
}
431426
}
432427

sdk/src/record-scanner.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class RecordScanner implements RecordProvider {
6969
throw new Error("Account not set");
7070
} else {
7171
request = {
72-
viewKey: this.account.viewKey(),
72+
viewKey: this.account.viewKey().toString(),
7373
start: startBlock,
7474
};
7575
}
@@ -228,7 +228,7 @@ class RecordScanner implements RecordProvider {
228228
});
229229

230230
const record = records.find(record => {
231-
const plaintext = RecordPlaintext.fromString(record.recordPlaintext);
231+
const plaintext = RecordPlaintext.fromString(record.recordPlaintext ?? '');
232232
const amountStr = plaintext.getMember("microcredits").toString();
233233
const amount = parseInt(amountStr.replace("u64", ""));
234234
return amount >= microcredits;
@@ -261,7 +261,7 @@ class RecordScanner implements RecordProvider {
261261
decrypt: true,
262262
});
263263
return records.filter(record => {
264-
const plaintext = RecordPlaintext.fromString(record.recordPlaintext);
264+
const plaintext = RecordPlaintext.fromString(record.recordPlaintext ?? '');
265265
const amount = plaintext.getMember("microcredits").toString();
266266
return microcreditAmounts.includes(parseInt(amount.replace("u64", "")));
267267
});

sdk/tests/record-provider.integration.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,25 @@ describe('RecordProvider', () => {
1818
try {
1919
// Find two records with findCreditsRecords
2020
const nonces: string[] = [];
21-
const records = await recordProvider.findCreditsRecords([100, 200], true, []);
21+
const records = await recordProvider.findCreditsRecords([100, 200], { unspent: true, nonces });
2222
if (Array.isArray(records)) {
2323
expect(records.length).equal(2);
2424
records.forEach((record) => {
25-
nonces.push(record.nonce());
25+
let pt = new RecordPlaintext(record.recordPlaintext);
26+
nonces.push(pt.nonce());
2627
});
2728
} else {
2829
expect(Array.isArray(records)).equal(true);
2930
}
3031

3132
// Get another two records with findCreditsRecords and ensure they are unique
32-
const records2 = await recordProvider.findCreditsRecords([100, 200], true, nonces);
33+
const records2 = await recordProvider.findCreditsRecords([100, 200], { unspent: true, nonces });
3334
if (Array.isArray(records2)) {
3435
expect(records2.length).equal(2);
3536
records2.forEach((record) => {
36-
expect(nonces.includes(record.nonce())).equal(false);
37-
nonces.push(record.nonce());
37+
let pt = new RecordPlaintext(record.recordPlaintext);
38+
expect(nonces.includes(pt.nonce())).equal(false);
39+
nonces.push(pt.nonce());
3840
});
3941
} else {
4042
expect(Array.isArray(records2)).equal(true);

sdk/tests/record-provider.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe.skip('RecordProvider', () => {
1616
describe('Record provider', () => {
1717
it('should not find records where there are none', async () => {
1818
const params = new BlockHeightSearch(0, 100);
19-
const records = await recordProvider.findCreditsRecords([100, 200], true, [], params);
19+
const records = await recordProvider.findCreditsRecords([100, 200], params);
2020
expect(<object>records).equal([]);
2121
});
2222
});

0 commit comments

Comments
 (0)