Skip to content

Commit 9705b65

Browse files
added getPublicBalance function
1 parent e8298a1 commit 9705b65

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

sdk/src/network-client.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Account } from "./account";
33
import { BlockJSON } from "./models/blockJSON";
44
import { TransactionJSON } from "./models/transaction/transactionJSON";
55
import {
6+
Address,
67
Plaintext,
78
RecordCiphertext,
89
Program,
@@ -331,16 +332,16 @@ class AleoNetworkClient {
331332
/**
332333
* Returns the contents of the block with the specified hash.
333334
*
334-
* @param {string} hash
335+
* @param {string} blockHash
335336
* @example
336337
* const block = networkClient.getBlockByHash("ab19dklwl9vp63zu3hwg57wyhvmqf92fx5g8x0t6dr72py8r87pxupqfne5t9");
337338
*/
338-
async getBlockByHash(hash: string): Promise<BlockJSON> {
339+
async getBlockByHash(blockHash: string): Promise<BlockJSON> {
339340
try {
340-
const block = await this.fetchData<BlockJSON>(`/block/${hash}`);
341+
const block = await this.fetchData<BlockJSON>(`/block/${blockHash}`);
341342
return block;
342343
} catch (error) {
343-
throw new Error(`Error fetching block ${hash}: ${error}`);
344+
throw new Error(`Error fetching block ${blockHash}: ${error}`);
344345
}
345346
}
346347

@@ -700,17 +701,34 @@ class AleoNetworkClient {
700701
*/
701702
async getProgramMappingPlaintext(programId: string, mappingName: string, key: string | Plaintext): Promise<Plaintext> {
702703
try {
703-
const value = await this.getProgramMappingValue(programId, mappingName, key);
704-
return Plaintext.fromString(value);
704+
const keyString = key instanceof Plaintext ? key.toString() : key;
705+
const value = await this.fetchRaw(`/program/${programId}/mapping/${mappingName}/${keyString}`);
706+
return Plaintext.fromString(JSON.parse(value));
705707
} catch (error) {
706-
throw new Error(`${error}`);
708+
throw new Error("Failed to fetch mapping value." + error);
709+
}
710+
}
711+
712+
/**
713+
* Returns the public balance of an address from the account mapping in credits.aleo
714+
*
715+
* @param {string} address
716+
*
717+
* @example
718+
* const account = new Account();
719+
* const publicBalance = networkClient.getPublicBalance(account.address());
720+
*/
721+
async getPublicBalance(address: Address): Promise<number> {
722+
try {
723+
const balanceStr = await this.getProgramMappingValue('credits.aleo', 'account', address.to_string());
724+
return balanceStr ? parseInt(balanceStr) : 0;
725+
} catch (error) {
726+
throw new Error(`Error fetching public balance for ${address}: ${error}`);
707727
}
708728
}
709729

710730
/**
711731
* Returns the latest state/merkle root of the Aleo blockchain.
712-
*
713-
* @param {number} height - The height for which the state root is fetched, if blank then the latest state root will be returned.
714732
*
715733
* @example
716734
* const stateRoot = networkClient.getStateRoot();
@@ -804,19 +822,19 @@ class AleoNetworkClient {
804822
}
805823

806824
/**
807-
* Returns the transactions present in the block with the specified hash.
825+
* Returns the confirmed transactions present in the block with the specified block hash.
808826
*
809-
* @param {string} hash
827+
* @param {string} blockHash
810828
* @example
811829
* const transactions = networkClient.getTransactionsByHash("ab19dklwl9vp63zu3hwg57wyhvmqf92fx5g8x0t6dr72py8r87pxupqfne5t9");
812830
*/
813-
async getTransactionsByHash(hash: string): Promise<Array<ConfirmedTransactionJSON>> {
831+
async getTransactionsByBlockHash(blockHash: string): Promise<Array<ConfirmedTransactionJSON>> {
814832
try {
815-
const block = await this.fetchData<BlockJSON>(`/block/${hash}`);
833+
const block = await this.fetchData<BlockJSON>(`/block/${blockHash}`);
816834
const height = block.header.metadata.height;
817835
return await this.getTransactions(height);
818836
} catch (error) {
819-
throw new Error(`Error fetching transactions for block ${hash}: ${error}`);
837+
throw new Error(`Error fetching transactions for block ${blockHash}: ${error}`);
820838
}
821839
}
822840

sdk/tests/network-client.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,19 @@ describe('NodeConnection', () => {
221221
});
222222
});
223223

224-
describe('getTransactionsByHash', () => {
224+
describe('getTransactionsByBlockHash', () => {
225225
it('should return an array of Transaction objects', async () => {
226226
const hash = network === 'testnet' ?
227227
'ab1sm6kyqle2ftg4z8gegafqrjy0jwjhzu6fmy73726dgszrtxhxvfqha0eee' : 'ab19dklwl9vp63zu3hwg57wyhvmqf92fx5g8x0t6dr72py8r87pxupqfne5t9';
228-
const transactions = await connection.getTransactionsByHash(hash);
228+
const transactions = await connection.getTransactionsByBlockHash(hash);
229229
expect(transactions.length).equals(4);
230+
});
231+
232+
it('should throw an error if the request fails', async () => {
233+
await expectThrowsMessage(
234+
() => connection.getTransactionsByBlockHash("fakeHash"),
235+
'Error fetching transactions for block fakeHash'
236+
)
230237
})
231238
});
232239

@@ -324,6 +331,14 @@ describe('NodeConnection', () => {
324331
});
325332
});
326333

334+
describe('Test credits.aleo convenience methods', () => {
335+
it('Public balance returned for a given address', async () => {
336+
const account = new Account();
337+
const publicBalance = await connection.getPublicBalance(account.address());
338+
expect(publicBalance).equals(0);
339+
});
340+
});
341+
327342
describe('Test API methods that return wasm objects', () => {
328343
it('Plaintext returned from the API should have expected properties', async () => {
329344
if (network === "testnet") {

0 commit comments

Comments
 (0)