Skip to content

Commit aa6ccb3

Browse files
committed
Changing getProgramMappingValue, getProgramMappingPlaintext, and waitForTransactionConfirmation to use object params
1 parent b4ed8b2 commit aa6ccb3

File tree

1 file changed

+54
-28
lines changed

1 file changed

+54
-28
lines changed

sdk/src/network-client.ts

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,9 +1190,10 @@ class AleoNetworkClient {
11901190
/**
11911191
* Returns the value of a program's mapping for a specific key.
11921192
*
1193-
* @param {string} programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1194-
* @param {string} mappingName - The name of the mapping to get the value of (e.g. "account")
1195-
* @param {string | Plaintext} key - The key to look up in the mapping (e.g. an address for the "account" mapping)
1193+
* @param {Object} params
1194+
* @param {string} params.programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1195+
* @param {string} params.mappingName - The name of the mapping to get the value of (e.g. "account")
1196+
* @param {string | Plaintext} params.key - The key to look up in the mapping (e.g. an address for the "account" mapping)
11961197
* @returns {Promise<string>} String representation of the value of the mapping
11971198
*
11981199
* @example
@@ -1202,25 +1203,35 @@ class AleoNetworkClient {
12021203
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
12031204
*
12041205
* // Get public balance of an account
1205-
* const mappingValue = networkClient.getMappingValue("credits.aleo", "account", "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px");
1206+
* const mappingValue = networkClient.getProgramMappingValue({
1207+
* programId: "credits.aleo",
1208+
* mappingName: "account",
1209+
* key: "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px",
1210+
* });
12061211
* const expectedValue = "0u64";
12071212
* assert(mappingValue === expectedValue);
12081213
*/
1209-
async getProgramMappingValue(
1214+
async getProgramMappingValue(params: {
12101215
programId: string,
12111216
mappingName: string,
12121217
key: string | Plaintext,
1213-
): Promise<string> {
1218+
}): Promise<string> {
1219+
const { programId, mappingName, key } = params;
1220+
1221+
this.ctx = { "X-ALEO-METHOD": "getProgramMappingValue" };
1222+
12141223
try {
1215-
this.ctx = { "X-ALEO-METHOD": "getProgramMappingValue" };
12161224
const keyString = key instanceof Plaintext ? key.toString() : key;
1225+
12171226
return await this.fetchData<string>(
12181227
`/program/${programId}/mapping/${mappingName}/${keyString}`,
12191228
);
1229+
12201230
} catch (error) {
12211231
throw new Error(
12221232
`Error fetching value for key '${key}' in mapping '${mappingName}' in program '${programId}' - ensure the mapping exists and the key is correct: ${error}`,
12231233
);
1234+
12241235
} finally {
12251236
this.ctx = {};
12261237
}
@@ -1229,9 +1240,10 @@ class AleoNetworkClient {
12291240
/**
12301241
* Returns the value of a mapping as a wasm Plaintext object. Returning an object in this format allows it to be converted to a Js type and for its internal members to be inspected if it's a struct or array.
12311242
*
1232-
* @param {string} programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1233-
* @param {string} mappingName - The name of the mapping to get the value of (e.g. "bonded")
1234-
* @param {string | Plaintext} key - The key to look up in the mapping (e.g. an address for the "bonded" mapping)
1243+
* @param {Object} params
1244+
* @param {string} params.programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1245+
* @param {string} params.mappingName - The name of the mapping to get the value of (e.g. "bonded")
1246+
* @param {string | Plaintext} params.key - The key to look up in the mapping (e.g. an address for the "bonded" mapping)
12351247
* @returns {Promise<Plaintext>} String representation of the value of the mapping
12361248
*
12371249
* @example
@@ -1241,7 +1253,11 @@ class AleoNetworkClient {
12411253
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
12421254
*
12431255
* // Get the bond state as an account.
1244-
* const unbondedState = networkClient.getMappingPlaintext("credits.aleo", "bonded", "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px");
1256+
* const unbondedState = networkClient.getProgramMappingPlaintext({
1257+
* programId: "credits.aleo",
1258+
* mappingName: "bonded",
1259+
* key: "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px",
1260+
* });
12451261
*
12461262
* // Get the two members of the object individually.
12471263
* const validator = unbondedState.getMember("validator");
@@ -1260,20 +1276,25 @@ class AleoNetworkClient {
12601276
* };
12611277
* assert.equal(unbondedState, expectedState);
12621278
*/
1263-
async getProgramMappingPlaintext(
1279+
async getProgramMappingPlaintext(params: {
12641280
programId: string,
12651281
mappingName: string,
12661282
key: string | Plaintext,
1267-
): Promise<Plaintext> {
1283+
}): Promise<Plaintext> {
1284+
this.ctx = { "X-ALEO-METHOD": "getProgramMappingPlaintext" };
1285+
12681286
try {
1269-
this.ctx = { "X-ALEO-METHOD": "getProgramMappingPlaintext" };
1270-
const keyString = key instanceof Plaintext ? key.toString() : key;
1287+
const keyString = params.key instanceof Plaintext ? params.key.toString() : params.key;
1288+
12711289
const value = await this.fetchRaw(
1272-
`/program/${programId}/mapping/${mappingName}/${keyString}`,
1290+
`/program/${params.programId}/mapping/${params.mappingName}/${keyString}`,
12731291
);
1292+
12741293
return Plaintext.fromString(JSON.parse(value));
1294+
12751295
} catch (error) {
12761296
throw new Error("Failed to fetch mapping value." + error);
1297+
12771298
} finally {
12781299
this.ctx = {};
12791300
}
@@ -1302,11 +1323,11 @@ class AleoNetworkClient {
13021323
this.ctx = { "X-ALEO-METHOD": "getPublicBalance" };
13031324
const addressString =
13041325
address instanceof Address ? address.to_string() : address;
1305-
const balanceStr = await this.getProgramMappingValue(
1306-
"credits.aleo",
1307-
"account",
1308-
addressString,
1309-
);
1326+
const balanceStr = await this.getProgramMappingValue({
1327+
programId: "credits.aleo",
1328+
mappingName: "account",
1329+
key: addressString,
1330+
});
13101331
return balanceStr ? parseInt(balanceStr) : 0;
13111332
} catch (error) {
13121333
throw new Error(
@@ -1671,9 +1692,10 @@ class AleoNetworkClient {
16711692
/**
16721693
* Await a submitted transaction to be confirmed or rejected on the Aleo network.
16731694
*
1674-
* @param {string} transactionId - The transaction ID to wait for confirmation
1675-
* @param {number} checkInterval - The interval in milliseconds to check for confirmation (default: 2000)
1676-
* @param {number} timeout - The maximum time in milliseconds to wait for confirmation (default: 45000)
1695+
* @param {Object} params
1696+
* @param {string} params.transactionId - The transaction ID to wait for confirmation
1697+
* @param {number} [params.checkInterval=2000] - The interval in milliseconds to check for confirmation (default: 2000)
1698+
* @param {number} [params.timeout=45000] - The maximum time in milliseconds to wait for confirmation (default: 45000)
16771699
* @returns {Promise<Transaction>} The confirmed transaction object that returns if the transaction is confirmed.
16781700
*
16791701
* @example
@@ -1695,11 +1717,15 @@ class AleoNetworkClient {
16951717
* // Wait for the transaction to be confirmed.
16961718
* const transaction = await networkClient.waitForTransactionConfirmation(transactionId);
16971719
*/
1698-
async waitForTransactionConfirmation(
1720+
async waitForTransactionConfirmation({
1721+
transactionId,
1722+
checkInterval = 2000,
1723+
timeout = 45000,
1724+
}: {
16991725
transactionId: string,
1700-
checkInterval: number = 2000,
1701-
timeout: number = 45000,
1702-
): Promise<ConfirmedTransactionJSON> {
1726+
checkInterval: number,
1727+
timeout: number,
1728+
}): Promise<ConfirmedTransactionJSON> {
17031729
const startTime = Date.now();
17041730

17051731
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)