Skip to content

Commit a870300

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

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

sdk/src/network-client.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,9 +1057,10 @@ class AleoNetworkClient {
10571057
/**
10581058
* Returns the value of a program's mapping for a specific key.
10591059
*
1060-
* @param {string} programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1061-
* @param {string} mappingName - The name of the mapping to get the value of (e.g. "account")
1062-
* @param {string | Plaintext} key - The key to look up in the mapping (e.g. an address for the "account" mapping)
1060+
* @param {Object} params
1061+
* @param {string} params.programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1062+
* @param {string} params.mappingName - The name of the mapping to get the value of (e.g. "account")
1063+
* @param {string | Plaintext} params.key - The key to look up in the mapping (e.g. an address for the "account" mapping)
10631064
* @returns {Promise<string>} String representation of the value of the mapping
10641065
*
10651066
* @example
@@ -1069,33 +1070,38 @@ class AleoNetworkClient {
10691070
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
10701071
*
10711072
* // Get public balance of an account
1072-
* const mappingValue = networkClient.getMappingValue("credits.aleo", "account", "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px");
1073+
* const mappingValue = networkClient.getProgramMappingValue({
1074+
* programId: "credits.aleo",
1075+
* mappingName: "account",
1076+
* key: "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px",
1077+
* });
10731078
* const expectedValue = "0u64";
10741079
* assert(mappingValue === expectedValue);
10751080
*/
1076-
async getProgramMappingValue(
1081+
async getProgramMappingValue(params: {
10771082
programId: string,
10781083
mappingName: string,
10791084
key: string | Plaintext,
1080-
): Promise<string> {
1085+
}): Promise<string> {
10811086
try {
1082-
const keyString = key instanceof Plaintext ? key.toString() : key;
1087+
const keyString = params.key instanceof Plaintext ? params.key.toString() : params.key;
10831088
return await this.fetchData<string>(
1084-
`/program/${programId}/mapping/${mappingName}/${keyString}`,
1089+
`/program/${params.programId}/mapping/${params.mappingName}/${keyString}`,
10851090
);
10861091
} catch (error) {
10871092
throw new Error(
1088-
`Error fetching value for key '${key}' in mapping '${mappingName}' in program '${programId}' - ensure the mapping exists and the key is correct`,
1093+
`Error fetching value for key '${params.key}' in mapping '${params.mappingName}' in program '${params.programId}' - ensure the mapping exists and the key is correct`,
10891094
);
10901095
}
10911096
}
10921097

10931098
/**
10941099
* 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.
10951100
*
1096-
* @param {string} programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1097-
* @param {string} mappingName - The name of the mapping to get the value of (e.g. "bonded")
1098-
* @param {string | Plaintext} key - The key to look up in the mapping (e.g. an address for the "bonded" mapping)
1101+
* @param {Object} params
1102+
* @param {string} params.programId - The program ID to get the mapping value of (e.g. "credits.aleo")
1103+
* @param {string} params.mappingName - The name of the mapping to get the value of (e.g. "bonded")
1104+
* @param {string | Plaintext} params.key - The key to look up in the mapping (e.g. an address for the "bonded" mapping)
10991105
* @returns {Promise<Plaintext>} String representation of the value of the mapping
11001106
*
11011107
* @example
@@ -1105,7 +1111,11 @@ class AleoNetworkClient {
11051111
* const networkClient = new AleoNetworkClient("http://api.explorer.provable.com/v1", undefined);
11061112
*
11071113
* // Get the bond state as an account.
1108-
* const unbondedState = networkClient.getMappingPlaintext("credits.aleo", "bonded", "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px");
1114+
* const unbondedState = networkClient.getProgramMappingPlaintext({
1115+
* programId: "credits.aleo",
1116+
* mappingName: "bonded",
1117+
* key: "aleo1rhgdu77hgyqd3xjj8ucu3jj9r2krwz6mnzyd80gncr5fxcwlh5rsvzp9px",
1118+
* });
11091119
*
11101120
* // Get the two members of the object individually.
11111121
* const validator = unbondedState.getMember("validator");
@@ -1124,15 +1134,15 @@ class AleoNetworkClient {
11241134
* };
11251135
* assert.equal(unbondedState, expectedState);
11261136
*/
1127-
async getProgramMappingPlaintext(
1137+
async getProgramMappingPlaintext(params: {
11281138
programId: string,
11291139
mappingName: string,
11301140
key: string | Plaintext,
1131-
): Promise<Plaintext> {
1141+
}): Promise<Plaintext> {
11321142
try {
1133-
const keyString = key instanceof Plaintext ? key.toString() : key;
1143+
const keyString = params.key instanceof Plaintext ? params.key.toString() : params.key;
11341144
const value = await this.fetchRaw(
1135-
`/program/${programId}/mapping/${mappingName}/${keyString}`,
1145+
`/program/${params.programId}/mapping/${params.mappingName}/${keyString}`,
11361146
);
11371147
return Plaintext.fromString(JSON.parse(value));
11381148
} catch (error) {
@@ -1162,11 +1172,11 @@ class AleoNetworkClient {
11621172
try {
11631173
const addressString =
11641174
address instanceof Address ? address.to_string() : address;
1165-
const balanceStr = await this.getProgramMappingValue(
1166-
"credits.aleo",
1167-
"account",
1168-
addressString,
1169-
);
1175+
const balanceStr = await this.getProgramMappingValue({
1176+
programId: "credits.aleo",
1177+
mappingName: "account",
1178+
key: addressString,
1179+
});
11701180
return balanceStr ? parseInt(balanceStr) : 0;
11711181
} catch (error) {
11721182
throw new Error(
@@ -1464,9 +1474,10 @@ class AleoNetworkClient {
14641474
/**
14651475
* Await a submitted transaction to be confirmed or rejected on the Aleo network.
14661476
*
1467-
* @param {string} transactionId - The transaction ID to wait for confirmation
1468-
* @param {number} checkInterval - The interval in milliseconds to check for confirmation (default: 2000)
1469-
* @param {number} timeout - The maximum time in milliseconds to wait for confirmation (default: 45000)
1477+
* @param {Object} params
1478+
* @param {string} params.transactionId - The transaction ID to wait for confirmation
1479+
* @param {number} [params.checkInterval=2000] - The interval in milliseconds to check for confirmation (default: 2000)
1480+
* @param {number} [params.timeout=45000] - The maximum time in milliseconds to wait for confirmation (default: 45000)
14701481
* @returns {Promise<Transaction>} The confirmed transaction object that returns if the transaction is confirmed.
14711482
*
14721483
* @example
@@ -1488,11 +1499,15 @@ class AleoNetworkClient {
14881499
* // Wait for the transaction to be confirmed.
14891500
* const transaction = await networkClient.waitForTransactionConfirmation(transactionId);
14901501
*/
1491-
async waitForTransactionConfirmation(
1502+
async waitForTransactionConfirmation({
1503+
transactionId,
1504+
checkInterval = 2000,
1505+
timeout = 45000,
1506+
}: {
14921507
transactionId: string,
1493-
checkInterval: number = 2000,
1494-
timeout: number = 45000,
1495-
): Promise<ConfirmedTransactionJSON> {
1508+
checkInterval: number,
1509+
timeout: number,
1510+
}): Promise<ConfirmedTransactionJSON> {
14961511
const startTime = Date.now();
14971512

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

0 commit comments

Comments
 (0)