Skip to content

Commit 2eb9e31

Browse files
committed
Changing these APIs to use object params:
* findCreditsRecords * findCreditsRecord * findRecord * findRecords * BlockHeightSearch
1 parent dcaa0ef commit 2eb9e31

File tree

1 file changed

+112
-43
lines changed

1 file changed

+112
-43
lines changed

sdk/src/record-provider.ts

Lines changed: 112 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,53 @@ interface RecordProvider {
2323
/**
2424
* Find a credits.aleo record with a given number of microcredits from the chosen provider
2525
*
26-
* @param {number} microcredits The number of microcredits to search for
27-
* @param {boolean} unspent Whether or not the record is unspent
28-
* @param {string[]} nonces Nonces of records already found so they are not found again
29-
* @param {RecordSearchParams} searchParameters Additional parameters to search for
26+
* @param {Object} params
27+
* @param {number} params.microcredits The number of microcredits to search for
28+
* @param {boolean} params.unspent Whether or not the record is unspent
29+
* @param {string[]} [params.nonces] Nonces of records already found so they are not found again
30+
* @param {RecordSearchParams} [params.searchParameters] Additional parameters to search for
3031
* @returns {Promise<RecordPlaintext>} The record if found, otherwise an error
3132
*
3233
* @example
3334
* // A class implementing record provider can be used to find a record with a given number of microcredits
34-
* const record = await recordProvider.findCreditsRecord(5000, true, []);
35+
* const record = await recordProvider.findCreditsRecord({
36+
* microcredits: 5000,
37+
* unspent: true,
38+
* nonces: [],
39+
* });
3540
*
3641
* // When a record is found but not yet used, its nonce should be added to the nonces array so that it is not
3742
* // found again if a subsequent search is performed
38-
* const record2 = await recordProvider.findCreditsRecord(5000, true, [record.nonce()]);
43+
* const record2 = await recordProvider.findCreditsRecord({
44+
* microcredits: 5000,
45+
* unspent: true,
46+
* nonces: [record.nonce()],
47+
* });
3948
*
4049
* // When the program manager is initialized with the record provider it will be used to find automatically find
4150
* // fee records and amount records for value transfers so that they do not need to be specified manually
4251
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
4352
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
4453
*/
45-
findCreditsRecord(microcredits: number, unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext>;
54+
findCreditsRecord(params: { microcredits: number, unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams }): Promise<RecordPlaintext>;
4655

4756
/**
4857
* Find a list of credit.aleo records with a given number of microcredits from the chosen provider
4958
*
50-
* @param {number} microcreditAmounts A list of separate microcredit amounts to search for (e.g. [5000, 100000])
51-
* @param {boolean} unspent Whether or not the record is unspent
52-
* @param {string[]} nonces Nonces of records already found so that they are not found again
53-
* @param {RecordSearchParams} searchParameters Additional parameters to search for
59+
* @param {Object} params
60+
* @param {number} params.microcreditAmounts A list of separate microcredit amounts to search for (e.g. [5000, 100000])
61+
* @param {boolean} params.unspent Whether or not the record is unspent
62+
* @param {string[]} [params.nonces] Nonces of records already found so that they are not found again
63+
* @param {RecordSearchParams} [params.searchParameters] Additional parameters to search for
5464
* @returns {Promise<RecordPlaintext[]>} A list of records with a value greater or equal to the amounts specified if such records exist, otherwise an error
5565
*
5666
* @example
5767
* // A class implementing record provider can be used to find a record with a given number of microcredits
58-
* const records = await recordProvider.findCreditsRecords([5000, 5000], true, []);
68+
* const records = await recordProvider.findCreditsRecords({
69+
* microcredits: [5000, 5000],
70+
* unspent: true,
71+
* nonces: [],
72+
* });
5973
*
6074
* // When a record is found but not yet used, it's nonce should be added to the nonces array so that it is not
6175
* // found again if a subsequent search is performed
@@ -68,13 +82,14 @@ interface RecordProvider {
6882
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
6983
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
7084
*/
71-
findCreditsRecords(microcreditAmounts: number[], unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext[]>;
85+
findCreditsRecords(params: { microcredits: number[], unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams }): Promise<RecordPlaintext[]>;
7286

7387
/**
7488
* Find an arbitrary record
75-
* @param {boolean} unspent Whether or not the record is unspent
76-
* @param {string[]} nonces Nonces of records already found so that they are not found again
77-
* @param {RecordSearchParams} searchParameters Additional parameters to search for
89+
* @param {Object} params
90+
* @param {boolean} params.unspent Whether or not the record is unspent
91+
* @param {string[]} [params.nonces] Nonces of records already found so that they are not found again
92+
* @param {RecordSearchParams} [params.searchParameters] Additional parameters to search for
7893
* @returns {Promise<RecordPlaintext>} The record if found, otherwise an error
7994
*
8095
* @example
@@ -99,16 +114,21 @@ interface RecordProvider {
99114
*
100115
* const params = new CustomRecordSearch(0, 100, 5000, "credits.aleo", "credits");
101116
*
102-
* const record = await recordProvider.findRecord(true, [], params);
117+
* const record = await recordProvider.findRecord({
118+
* unspent: true,
119+
* nonces: [],
120+
* searchParameters: params,
121+
* });
103122
*/
104-
findRecord(unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext>;
123+
findRecord(params: { unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams }): Promise<RecordPlaintext>;
105124

106125
/**
107126
* Find multiple records from arbitrary programs
108127
*
109-
* @param {boolean} unspent Whether or not the record is unspent
110-
* @param {string[]} nonces Nonces of records already found so that they are not found again
111-
* @param {RecordSearchParams} searchParameters Additional parameters to search for
128+
* @param {Object} params
129+
* @param {boolean} params.unspent Whether or not the record is unspent
130+
* @param {string[]} [params.nonces] Nonces of records already found so that they are not found again
131+
* @param {RecordSearchParams} [params.searchParameters] Additional parameters to search for
112132
* @returns {Promise<RecordPlaintext>} The record if found, otherwise an error
113133
*
114134
* // The RecordSearchParams interface can be used to create parameters for custom record searches which can then
@@ -133,9 +153,13 @@ interface RecordProvider {
133153
* }
134154
*
135155
* const params = new CustomRecordSearch(0, 100, 5000, 2, "credits.aleo", "credits");
136-
* const records = await recordProvider.findRecord(true, [], params);
156+
* const records = await recordProvider.findRecords({
157+
* unspent: true,
158+
* nonces: [],
159+
* searchParameters: params,
160+
* });
137161
*/
138-
findRecords(unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext[]>;
162+
findRecords(params: { unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams }): Promise<RecordPlaintext[]>;
139163
}
140164

141165
/**
@@ -145,6 +169,7 @@ interface RecordProvider {
145169
class NetworkRecordProvider implements RecordProvider {
146170
account: Account;
147171
networkClient: AleoNetworkClient;
172+
148173
constructor(account: Account, networkClient: AleoNetworkClient) {
149174
this.account = account;
150175
this.networkClient = networkClient;
@@ -162,10 +187,11 @@ class NetworkRecordProvider implements RecordProvider {
162187
/**
163188
* Find a list of credit records with a given number of microcredits by via the official Aleo API
164189
*
165-
* @param {number[]} microcredits The number of microcredits to search for
166-
* @param {boolean} unspent Whether or not the record is unspent
167-
* @param {string[]} nonces Nonces of records already found so that they are not found again
168-
* @param {RecordSearchParams} searchParameters Additional parameters to search for
190+
* @param {Object} params
191+
* @param {number[]} params.microcredits The number of microcredits to search for
192+
* @param {boolean} params.unspent Whether or not the record is unspent
193+
* @param {string[]} [params.nonces] Nonces of records already found so that they are not found again
194+
* @param {RecordSearchParams} [params.searchParameters] Additional parameters to search for
169195
* @returns {Promise<RecordPlaintext>} The record if found, otherwise an error
170196
*
171197
* @example
@@ -179,15 +205,26 @@ class NetworkRecordProvider implements RecordProvider {
179205
*
180206
* // When a record is found but not yet used, it's nonce should be added to the nonces parameter so that it is not
181207
* // found again if a subsequent search is performed
182-
* const records = await recordProvider.findCreditsRecords(5000, true, [record.nonce()]);
208+
* const records = await recordProvider.findCreditsRecords({
209+
* microcredits: [5000],
210+
* unspent: true,
211+
* nonces: [record.nonce()],
212+
* });
183213
*
184214
* // When the program manager is initialized with the record provider it will be used to find automatically find
185215
* // fee records and amount records for value transfers so that they do not need to be specified manually
186216
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
187217
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
188218
*
189219
* */
190-
async findCreditsRecords(microcredits: number[], unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext[]> {
220+
async findCreditsRecords(params: {
221+
microcredits: number[],
222+
unspent: boolean,
223+
nonces?: string[],
224+
searchParameters?: RecordSearchParams,
225+
}): Promise<RecordPlaintext[]> {
226+
let { microcredits, unspent, nonces, searchParameters } = params;
227+
191228
let startHeight = 0;
192229
let endHeight = 0;
193230
let maxAmount = undefined;
@@ -240,10 +277,11 @@ class NetworkRecordProvider implements RecordProvider {
240277
/**
241278
* Find a credit record with a given number of microcredits by via the official Aleo API
242279
*
243-
* @param {number} microcredits The number of microcredits to search for
244-
* @param {boolean} unspent Whether or not the record is unspent
245-
* @param {string[]} nonces Nonces of records already found so that they are not found again
246-
* @param {RecordSearchParams} searchParameters Additional parameters to search for
280+
* @param {Object} params
281+
* @param {number} params.microcredits The number of microcredits to search for
282+
* @param {boolean} params.unspent Whether or not the record is unspent
283+
* @param {string[]} [params.nonces] Nonces of records already found so that they are not found again
284+
* @param {RecordSearchParams} [params.searchParameters] Additional parameters to search for
247285
* @returns {Promise<RecordPlaintext>} The record if found, otherwise an error
248286
*
249287
* @example
@@ -253,7 +291,11 @@ class NetworkRecordProvider implements RecordProvider {
253291
* const recordProvider = new NetworkRecordProvider(account, networkClient);
254292
*
255293
* // The record provider can be used to find records with a given number of microcredits
256-
* const record = await recordProvider.findCreditsRecord(5000, true, []);
294+
* const record = await recordProvider.findCreditsRecord({
295+
* microcredits: 5000,
296+
* unspent: true,
297+
* nonces: [],
298+
* });
257299
*
258300
* // When a record is found but not yet used, it's nonce should be added to the nonces parameter so that it is not
259301
* // found again if a subsequent search is performed
@@ -264,11 +306,23 @@ class NetworkRecordProvider implements RecordProvider {
264306
* const programManager = new ProgramManager("https://api.explorer.provable.com/v1", keyProvider, recordProvider);
265307
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
266308
*/
267-
async findCreditsRecord(microcredits: number, unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext> {
309+
async findCreditsRecord(params: {
310+
microcredits: number,
311+
unspent: boolean,
312+
nonces?: string[],
313+
searchParameters?: RecordSearchParams,
314+
}): Promise<RecordPlaintext> {
315+
const { microcredits, unspent, nonces, searchParameters } = params;
316+
268317
let records = null;
269318

270319
try {
271-
records = await this.findCreditsRecords([microcredits], unspent, nonces, searchParameters);
320+
records = await this.findCreditsRecords({
321+
microcredits: [microcredits],
322+
unspent,
323+
nonces,
324+
searchParameters,
325+
});
272326
} catch (e) {
273327
console.log("No records found with error:", e);
274328
}
@@ -284,14 +338,24 @@ class NetworkRecordProvider implements RecordProvider {
284338
/**
285339
* Find an arbitrary record. WARNING: This function is not implemented yet and will throw an error.
286340
*/
287-
async findRecord(unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext> {
341+
async findRecord(params: {
342+
unspent: boolean,
343+
nonces?: string[],
344+
searchParameters?: RecordSearchParams,
345+
}): Promise<RecordPlaintext> {
288346
throw new Error("Not implemented");
289347
}
290348

291349
/**
292350
* Find multiple records from a specified program.
293351
*/
294-
async findRecords(unspent: boolean, nonces?: string[], searchParameters?: RecordSearchParams): Promise<RecordPlaintext[]> {
352+
async findRecords(params: {
353+
unspent: boolean,
354+
nonces?: string[],
355+
searchParameters?: RecordSearchParams,
356+
}): Promise<RecordPlaintext[]> {
357+
let { unspent, nonces, searchParameters } = params;
358+
295359
let startHeight = 0;
296360
let endHeight = 0;
297361
let amounts = undefined;
@@ -363,7 +427,7 @@ class NetworkRecordProvider implements RecordProvider {
363427
*
364428
* @example
365429
* // Create a new BlockHeightSearch
366-
* const params = new BlockHeightSearch(89995, 99995);
430+
* const params = new BlockHeightSearch({ startHeight: 89995, endHeight: 99995 });
367431
*
368432
* // Create a new NetworkRecordProvider
369433
* const networkClient = new AleoNetworkClient("https://api.explorer.provable.com/v1");
@@ -372,15 +436,20 @@ class NetworkRecordProvider implements RecordProvider {
372436
*
373437
* // The record provider can be used to find records with a given number of microcredits and the block height search
374438
* // can be used to find records within a given block height range
375-
* const record = await recordProvider.findCreditsRecord(5000, true, [], params);
439+
* const record = await recordProvider.findCreditsRecord({
440+
* microcredits: 5000,
441+
* unspent: true,
442+
* nonces: [],
443+
* searchParameters: params,
444+
* });
376445
*
377446
*/
378447
class BlockHeightSearch implements RecordSearchParams {
379448
startHeight: number;
380449
endHeight: number;
381-
constructor(startHeight: number, endHeight: number) {
382-
this.startHeight = startHeight;
383-
this.endHeight = endHeight;
450+
constructor(params: { startHeight: number, endHeight: number }) {
451+
this.startHeight = params.startHeight;
452+
this.endHeight = params.endHeight;
384453
}
385454
}
386455

0 commit comments

Comments
 (0)