Skip to content

Commit 8188c15

Browse files
committed
Changing getProgramMappingValue, getProgramMappingPlaintext, and waitForTransactionConfirmation to use object params
1 parent e02cc0b commit 8188c15

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
@@ -1158,9 +1158,10 @@ class AleoNetworkClient {
11581158
/**
11591159
* Returns the value of a program's mapping for a specific key.
11601160
*
1161-
* @param {string} programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1162-
* @param {string} mappingName - The name of the mapping to get the value of (e.g. "account")
1163-
* @param {string | Plaintext} key - The key to look up in the mapping (e.g. an address for the "account" mapping)
1161+
* @param {Object} params
1162+
* @param {string} params.programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1163+
* @param {string} params.mappingName - The name of the mapping to get the value of (e.g. "account")
1164+
* @param {string | Plaintext} params.key - The key to look up in the mapping (e.g. an address for the "account" mapping)
11641165
* @returns {Promise<string>} String representation of the value of the mapping
11651166
*
11661167
* @example
@@ -1170,25 +1171,35 @@ class AleoNetworkClient {
11701171
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
11711172
*
11721173
* // Get public balance of an account
1173-
* const mappingValue = networkClient.getMappingValue("credits.aleo", "account", "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px");
1174+
* const mappingValue = networkClient.getProgramMappingValue({
1175+
* programId: "credits.aleo",
1176+
* mappingName: "account",
1177+
* key: "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px",
1178+
* });
11741179
* const expectedValue = "0u64";
11751180
* assert(mappingValue === expectedValue);
11761181
*/
1177-
async getProgramMappingValue(
1182+
async getProgramMappingValue(params: {
11781183
programId: string,
11791184
mappingName: string,
11801185
key: string | Plaintext,
1181-
): Promise<string> {
1186+
}): Promise<string> {
1187+
const { programId, mappingName, key } = params;
1188+
1189+
this.ctx = { "X-ALEO-METHOD": "getProgramMappingValue" };
1190+
11821191
try {
1183-
this.ctx = { "X-ALEO-METHOD": "getProgramMappingValue" };
11841192
const keyString = key instanceof Plaintext ? key.toString() : key;
1193+
11851194
return await this.fetchData<string>(
11861195
`/program/${programId}/mapping/${mappingName}/${keyString}`,
11871196
);
1197+
11881198
} catch (error) {
11891199
throw new Error(
11901200
`Error fetching value for key '${key}' in mapping '${mappingName}' in program '${programId}' - ensure the mapping exists and the key is correct: ${error}`,
11911201
);
1202+
11921203
} finally {
11931204
this.ctx = {};
11941205
}
@@ -1197,9 +1208,10 @@ class AleoNetworkClient {
11971208
/**
11981209
* 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.
11991210
*
1200-
* @param {string} programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1201-
* @param {string} mappingName - The name of the mapping to get the value of (e.g. "bonded")
1202-
* @param {string | Plaintext} key - The key to look up in the mapping (e.g. an address for the "bonded" mapping)
1211+
* @param {Object} params
1212+
* @param {string} params.programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1213+
* @param {string} params.mappingName - The name of the mapping to get the value of (e.g. "bonded")
1214+
* @param {string | Plaintext} params.key - The key to look up in the mapping (e.g. an address for the "bonded" mapping)
12031215
* @returns {Promise<Plaintext>} String representation of the value of the mapping
12041216
*
12051217
* @example
@@ -1209,7 +1221,11 @@ class AleoNetworkClient {
12091221
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
12101222
*
12111223
* // Get the bond state as an account.
1212-
* const unbondedState = networkClient.getMappingPlaintext("credits.aleo", "bonded", "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px");
1224+
* const unbondedState = networkClient.getProgramMappingPlaintext({
1225+
* programId: "credits.aleo",
1226+
* mappingName: "bonded",
1227+
* key: "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px",
1228+
* });
12131229
*
12141230
* // Get the two members of the object individually.
12151231
* const validator = unbondedState.getMember("validator");
@@ -1228,20 +1244,25 @@ class AleoNetworkClient {
12281244
* };
12291245
* assert.equal(unbondedState, expectedState);
12301246
*/
1231-
async getProgramMappingPlaintext(
1247+
async getProgramMappingPlaintext(params: {
12321248
programId: string,
12331249
mappingName: string,
12341250
key: string | Plaintext,
1235-
): Promise<Plaintext> {
1251+
}): Promise<Plaintext> {
1252+
this.ctx = { "X-ALEO-METHOD": "getProgramMappingPlaintext" };
1253+
12361254
try {
1237-
this.ctx = { "X-ALEO-METHOD": "getProgramMappingPlaintext" };
1238-
const keyString = key instanceof Plaintext ? key.toString() : key;
1255+
const keyString = params.key instanceof Plaintext ? params.key.toString() : params.key;
1256+
12391257
const value = await this.fetchRaw(
1240-
`/program/${programId}/mapping/${mappingName}/${keyString}`,
1258+
`/program/${params.programId}/mapping/${params.mappingName}/${keyString}`,
12411259
);
1260+
12421261
return Plaintext.fromString(JSON.parse(value));
1262+
12431263
} catch (error) {
12441264
throw new Error("Failed to fetch mapping value." + error);
1265+
12451266
} finally {
12461267
this.ctx = {};
12471268
}
@@ -1270,11 +1291,11 @@ class AleoNetworkClient {
12701291
this.ctx = { "X-ALEO-METHOD": "getPublicBalance" };
12711292
const addressString =
12721293
address instanceof Address ? address.to_string() : address;
1273-
const balanceStr = await this.getProgramMappingValue(
1274-
"credits.aleo",
1275-
"account",
1276-
addressString,
1277-
);
1294+
const balanceStr = await this.getProgramMappingValue({
1295+
programId: "credits.aleo",
1296+
mappingName: "account",
1297+
key: addressString,
1298+
});
12781299
return balanceStr ? parseInt(balanceStr) : 0;
12791300
} catch (error) {
12801301
throw new Error(
@@ -1599,9 +1620,10 @@ class AleoNetworkClient {
15991620
/**
16001621
* Await a submitted transaction to be confirmed or rejected on the Aleo network.
16011622
*
1602-
* @param {string} transactionId - The transaction ID to wait for confirmation
1603-
* @param {number} checkInterval - The interval in milliseconds to check for confirmation (default: 2000)
1604-
* @param {number} timeout - The maximum time in milliseconds to wait for confirmation (default: 45000)
1623+
* @param {Object} params
1624+
* @param {string} params.transactionId - The transaction ID to wait for confirmation
1625+
* @param {number} [params.checkInterval=2000] - The interval in milliseconds to check for confirmation (default: 2000)
1626+
* @param {number} [params.timeout=45000] - The maximum time in milliseconds to wait for confirmation (default: 45000)
16051627
* @returns {Promise<Transaction>} The confirmed transaction object that returns if the transaction is confirmed.
16061628
*
16071629
* @example
@@ -1623,11 +1645,15 @@ class AleoNetworkClient {
16231645
* // Wait for the transaction to be confirmed.
16241646
* const transaction = await networkClient.waitForTransactionConfirmation(transactionId);
16251647
*/
1626-
async waitForTransactionConfirmation(
1648+
async waitForTransactionConfirmation({
1649+
transactionId,
1650+
checkInterval = 2000,
1651+
timeout = 45000,
1652+
}: {
16271653
transactionId: string,
1628-
checkInterval: number = 2000,
1629-
timeout: number = 45000,
1630-
): Promise<ConfirmedTransactionJSON> {
1654+
checkInterval: number,
1655+
timeout: number,
1656+
}): Promise<ConfirmedTransactionJSON> {
16311657
const startTime = Date.now();
16321658

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

0 commit comments

Comments
 (0)