Skip to content

Commit 08f3332

Browse files
committed
Changing getProgramMappingValue, getProgramMappingPlaintext, and waitForTransactionConfirmation to use object params
1 parent 94063da commit 08f3332

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
@@ -1172,9 +1172,10 @@ class AleoNetworkClient {
11721172
/**
11731173
* Returns the value of a program's mapping for a specific key.
11741174
*
1175-
* @param {string} programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1176-
* @param {string} mappingName - The name of the mapping to get the value of (e.g. "account")
1177-
* @param {string | Plaintext} key - The key to look up in the mapping (e.g. an address for the "account" mapping)
1175+
* @param {Object} params
1176+
* @param {string} params.programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1177+
* @param {string} params.mappingName - The name of the mapping to get the value of (e.g. "account")
1178+
* @param {string | Plaintext} params.key - The key to look up in the mapping (e.g. an address for the "account" mapping)
11781179
* @returns {Promise<string>} String representation of the value of the mapping
11791180
*
11801181
* @example
@@ -1184,25 +1185,35 @@ class AleoNetworkClient {
11841185
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
11851186
*
11861187
* // Get public balance of an account
1187-
* const mappingValue = networkClient.getMappingValue("credits.aleo", "account", "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px");
1188+
* const mappingValue = networkClient.getProgramMappingValue({
1189+
* programId: "credits.aleo",
1190+
* mappingName: "account",
1191+
* key: "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px",
1192+
* });
11881193
* const expectedValue = "0u64";
11891194
* assert(mappingValue === expectedValue);
11901195
*/
1191-
async getProgramMappingValue(
1196+
async getProgramMappingValue(params: {
11921197
programId: string,
11931198
mappingName: string,
11941199
key: string | Plaintext,
1195-
): Promise<string> {
1200+
}): Promise<string> {
1201+
const { programId, mappingName, key } = params;
1202+
1203+
this.ctx = { "X-ALEO-METHOD": "getProgramMappingValue" };
1204+
11961205
try {
1197-
this.ctx = { "X-ALEO-METHOD": "getProgramMappingValue" };
11981206
const keyString = key instanceof Plaintext ? key.toString() : key;
1207+
11991208
return await this.fetchData<string>(
12001209
`/program/${programId}/mapping/${mappingName}/${keyString}`,
12011210
);
1211+
12021212
} catch (error) {
12031213
throw new Error(
12041214
`Error fetching value for key '${key}' in mapping '${mappingName}' in program '${programId}' - ensure the mapping exists and the key is correct: ${error}`,
12051215
);
1216+
12061217
} finally {
12071218
this.ctx = {};
12081219
}
@@ -1211,9 +1222,10 @@ class AleoNetworkClient {
12111222
/**
12121223
* 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.
12131224
*
1214-
* @param {string} programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1215-
* @param {string} mappingName - The name of the mapping to get the value of (e.g. "bonded")
1216-
* @param {string | Plaintext} key - The key to look up in the mapping (e.g. an address for the "bonded" mapping)
1225+
* @param {Object} params
1226+
* @param {string} params.programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1227+
* @param {string} params.mappingName - The name of the mapping to get the value of (e.g. "bonded")
1228+
* @param {string | Plaintext} params.key - The key to look up in the mapping (e.g. an address for the "bonded" mapping)
12171229
* @returns {Promise<Plaintext>} String representation of the value of the mapping
12181230
*
12191231
* @example
@@ -1223,7 +1235,11 @@ class AleoNetworkClient {
12231235
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
12241236
*
12251237
* // Get the bond state as an account.
1226-
* const unbondedState = networkClient.getMappingPlaintext("credits.aleo", "bonded", "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px");
1238+
* const unbondedState = networkClient.getProgramMappingPlaintext({
1239+
* programId: "credits.aleo",
1240+
* mappingName: "bonded",
1241+
* key: "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px",
1242+
* });
12271243
*
12281244
* // Get the two members of the object individually.
12291245
* const validator = unbondedState.getMember("validator");
@@ -1242,20 +1258,25 @@ class AleoNetworkClient {
12421258
* };
12431259
* assert.equal(unbondedState, expectedState);
12441260
*/
1245-
async getProgramMappingPlaintext(
1261+
async getProgramMappingPlaintext(params: {
12461262
programId: string,
12471263
mappingName: string,
12481264
key: string | Plaintext,
1249-
): Promise<Plaintext> {
1265+
}): Promise<Plaintext> {
1266+
this.ctx = { "X-ALEO-METHOD": "getProgramMappingPlaintext" };
1267+
12501268
try {
1251-
this.ctx = { "X-ALEO-METHOD": "getProgramMappingPlaintext" };
1252-
const keyString = key instanceof Plaintext ? key.toString() : key;
1269+
const keyString = params.key instanceof Plaintext ? params.key.toString() : params.key;
1270+
12531271
const value = await this.fetchRaw(
1254-
`/program/${programId}/mapping/${mappingName}/${keyString}`,
1272+
`/program/${params.programId}/mapping/${params.mappingName}/${keyString}`,
12551273
);
1274+
12561275
return Plaintext.fromString(JSON.parse(value));
1276+
12571277
} catch (error) {
12581278
throw new Error("Failed to fetch mapping value." + error);
1279+
12591280
} finally {
12601281
this.ctx = {};
12611282
}
@@ -1284,11 +1305,11 @@ class AleoNetworkClient {
12841305
this.ctx = { "X-ALEO-METHOD": "getPublicBalance" };
12851306
const addressString =
12861307
address instanceof Address ? address.to_string() : address;
1287-
const balanceStr = await this.getProgramMappingValue(
1288-
"credits.aleo",
1289-
"account",
1290-
addressString,
1291-
);
1308+
const balanceStr = await this.getProgramMappingValue({
1309+
programId: "credits.aleo",
1310+
mappingName: "account",
1311+
key: addressString,
1312+
});
12921313
return balanceStr ? parseInt(balanceStr) : 0;
12931314
} catch (error) {
12941315
throw new Error(
@@ -1652,9 +1673,10 @@ class AleoNetworkClient {
16521673
/**
16531674
* Await a submitted transaction to be confirmed or rejected on the Aleo network.
16541675
*
1655-
* @param {string} transactionId - The transaction ID to wait for confirmation
1656-
* @param {number} checkInterval - The interval in milliseconds to check for confirmation (default: 2000)
1657-
* @param {number} timeout - The maximum time in milliseconds to wait for confirmation (default: 45000)
1676+
* @param {Object} params
1677+
* @param {string} params.transactionId - The transaction ID to wait for confirmation
1678+
* @param {number} [params.checkInterval=2000] - The interval in milliseconds to check for confirmation (default: 2000)
1679+
* @param {number} [params.timeout=45000] - The maximum time in milliseconds to wait for confirmation (default: 45000)
16581680
* @returns {Promise<Transaction>} The confirmed transaction object that returns if the transaction is confirmed.
16591681
*
16601682
* @example
@@ -1676,11 +1698,15 @@ class AleoNetworkClient {
16761698
* // Wait for the transaction to be confirmed.
16771699
* const transaction = await networkClient.waitForTransactionConfirmation(transactionId);
16781700
*/
1679-
async waitForTransactionConfirmation(
1701+
async waitForTransactionConfirmation({
1702+
transactionId,
1703+
checkInterval = 2000,
1704+
timeout = 45000,
1705+
}: {
16801706
transactionId: string,
1681-
checkInterval: number = 2000,
1682-
timeout: number = 45000,
1683-
): Promise<ConfirmedTransactionJSON> {
1707+
checkInterval: number,
1708+
timeout: number,
1709+
}): Promise<ConfirmedTransactionJSON> {
16841710
const startTime = Date.now();
16851711

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

0 commit comments

Comments
 (0)