@@ -3,6 +3,7 @@ import { Account } from "./account";
3
3
import { BlockJSON } from "./models/blockJSON" ;
4
4
import { TransactionJSON } from "./models/transaction/transactionJSON" ;
5
5
import {
6
+ Address ,
6
7
Plaintext ,
7
8
RecordCiphertext ,
8
9
Program ,
@@ -331,16 +332,16 @@ class AleoNetworkClient {
331
332
/**
332
333
* Returns the contents of the block with the specified hash.
333
334
*
334
- * @param {string } hash
335
+ * @param {string } blockHash
335
336
* @example
336
337
* const block = networkClient.getBlockByHash("ab19dklwl9vp63zu3hwg57wyhvmqf92fx5g8x0t6dr72py8r87pxupqfne5t9");
337
338
*/
338
- async getBlockByHash ( hash : string ) : Promise < BlockJSON > {
339
+ async getBlockByHash ( blockHash : string ) : Promise < BlockJSON > {
339
340
try {
340
- const block = await this . fetchData < BlockJSON > ( `/block/${ hash } ` ) ;
341
+ const block = await this . fetchData < BlockJSON > ( `/block/${ blockHash } ` ) ;
341
342
return block ;
342
343
} catch ( error ) {
343
- throw new Error ( `Error fetching block ${ hash } : ${ error } ` ) ;
344
+ throw new Error ( `Error fetching block ${ blockHash } : ${ error } ` ) ;
344
345
}
345
346
}
346
347
@@ -700,17 +701,34 @@ class AleoNetworkClient {
700
701
*/
701
702
async getProgramMappingPlaintext ( programId : string , mappingName : string , key : string | Plaintext ) : Promise < Plaintext > {
702
703
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 ) ) ;
705
707
} 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 } ` ) ;
707
727
}
708
728
}
709
729
710
730
/**
711
731
* 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.
714
732
*
715
733
* @example
716
734
* const stateRoot = networkClient.getStateRoot();
@@ -804,19 +822,19 @@ class AleoNetworkClient {
804
822
}
805
823
806
824
/**
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.
808
826
*
809
- * @param {string } hash
827
+ * @param {string } blockHash
810
828
* @example
811
829
* const transactions = networkClient.getTransactionsByHash("ab19dklwl9vp63zu3hwg57wyhvmqf92fx5g8x0t6dr72py8r87pxupqfne5t9");
812
830
*/
813
- async getTransactionsByHash ( hash : string ) : Promise < Array < ConfirmedTransactionJSON > > {
831
+ async getTransactionsByBlockHash ( blockHash : string ) : Promise < Array < ConfirmedTransactionJSON > > {
814
832
try {
815
- const block = await this . fetchData < BlockJSON > ( `/block/${ hash } ` ) ;
833
+ const block = await this . fetchData < BlockJSON > ( `/block/${ blockHash } ` ) ;
816
834
const height = block . header . metadata . height ;
817
835
return await this . getTransactions ( height ) ;
818
836
} catch ( error ) {
819
- throw new Error ( `Error fetching transactions for block ${ hash } : ${ error } ` ) ;
837
+ throw new Error ( `Error fetching transactions for block ${ blockHash } : ${ error } ` ) ;
820
838
}
821
839
}
822
840
0 commit comments