Skip to content

Commit 092acf4

Browse files
fixed failing record provider tests
1 parent c4fdc9c commit 092acf4

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(
@@ -583,14 +583,12 @@ class ProgramManager {
583583
// Get the fee record from the account if it is not provided in the parameters
584584
try {
585585
feeRecord = privateFee
586-
? <RecordPlaintext>(
587-
await this.getCreditsRecord(
586+
? RecordPlaintext.fromString((await this.getCreditsRecord(
588587
priorityFee,
589588
[],
590589
feeRecord,
591590
recordSearchParams,
592-
)
593-
)
591+
)).recordPlaintext?? '')
594592
: undefined;
595593
} catch (e: any) {
596594
logAndThrow(
@@ -976,14 +974,12 @@ class ProgramManager {
976974
// Get the fee record from the account if it is not provided in the parameters.
977975
try {
978976
feeRecord = privateFee
979-
? <RecordPlaintext>(
980-
await this.getCreditsRecord(
977+
? RecordPlaintext.fromString((await this.getCreditsRecord(
981978
priorityFee,
982979
[],
983980
feeRecord,
984981
recordSearchParams,
985-
)
986-
)
982+
)).recordPlaintext?? '')
987983
: undefined;
988984
} catch (e: any) {
989985
logAndThrow(
@@ -1314,14 +1310,12 @@ class ProgramManager {
13141310
// Get the fee record from the account if it is not provided in the parameters
13151311
try {
13161312
feeRecord = privateFee
1317-
? <RecordPlaintext>(
1318-
await this.getCreditsRecord(
1313+
? RecordPlaintext.fromString((await this.getCreditsRecord(
13191314
priorityFee,
13201315
[],
13211316
feeRecord,
13221317
recordSearchParams,
1323-
)
1324-
)
1318+
)).recordPlaintext?? '')
13251319
: undefined;
13261320
} catch (e: any) {
13271321
logAndThrow(
@@ -1587,27 +1581,23 @@ class ProgramManager {
15871581
const nonces: string[] = [];
15881582
if (requiresAmountRecord(transferType)) {
15891583
// If the transfer type is private and requires an amount record, get it from the record provider
1590-
amountRecord = <RecordPlaintext>(
1591-
await this.getCreditsRecord(
1584+
amountRecord = RecordPlaintext.fromString((await this.getCreditsRecord(
15921585
priorityFee,
15931586
[],
15941587
amountRecord,
15951588
recordSearchParams,
1596-
)
1597-
);
1589+
)).recordPlaintext?? '');
15981590
nonces.push(amountRecord.nonce());
15991591
} else {
16001592
amountRecord = undefined;
16011593
}
16021594
feeRecord = privateFee
1603-
? <RecordPlaintext>(
1604-
await this.getCreditsRecord(
1595+
? RecordPlaintext.fromString((await this.getCreditsRecord(
16051596
priorityFee,
16061597
nonces,
16071598
feeRecord,
16081599
recordSearchParams,
1609-
)
1610-
)
1600+
)).recordPlaintext?? '')
16111601
: undefined;
16121602
} catch (e: any) {
16131603
logAndThrow(
@@ -2574,21 +2564,25 @@ class ProgramManager {
25742564
nonces: string[],
25752565
record?: RecordPlaintext | string,
25762566
params?: RecordSearchParams,
2577-
): Promise<RecordPlaintext> {
2567+
): Promise<OwnedRecord> {
25782568
try {
2579-
return record instanceof RecordPlaintext
2580-
? record
2581-
: RecordPlaintext.fromString(<string>record);
2569+
// return record instanceof RecordPlaintext
2570+
// ? record
2571+
// : RecordPlaintext.fromString(<string>record);
2572+
if (record && record instanceof RecordPlaintext) {
2573+
record = record.toString();
2574+
}
2575+
return <OwnedRecord>({
2576+
recordPlaintext: record,
2577+
programName: 'credits.aleo',
2578+
recordName: 'credits',
2579+
})
25822580
} catch (e) {
25832581
try {
25842582
const recordProvider = <RecordProvider>this.recordProvider;
2585-
return <RecordPlaintext>(
2586-
await recordProvider.findCreditsRecord(
2587-
amount,
2588-
true,
2589-
nonces,
2590-
params,
2591-
)
2583+
return await recordProvider.findCreditsRecord(
2584+
amount,
2585+
{ ...params, unspent: true, nonces }
25922586
);
25932587
} catch (e: any) {
25942588
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)