File tree Expand file tree Collapse file tree 16 files changed +129
-1
lines changed Expand file tree Collapse file tree 16 files changed +129
-1
lines changed Original file line number Diff line number Diff line change @@ -136,6 +136,18 @@ adenaSDK.getAccount().then((account) => {
136136});
137137```
138138
139+ ### ` getNetwork `
140+
141+ Retrieves network information from the connected wallet.
142+
143+ ** Example:**
144+
145+ ```
146+ adenaSDK.getNetwork().then((network) => {
147+ console.log('Network:', network);
148+ });
149+ ```
150+
139151### ` switchNetwork `
140152
141153Switches the wallet to a different network.
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ export const mockWalletProvider: jest.Mocked<WalletProvider> = {
1717 isConnected : jest . fn ( ) . mockResolvedValue ( makeResponseMessage ( WalletResponseSuccessType . CONNECTION_SUCCESS ) ) ,
1818 addEstablish : jest . fn ( ) . mockResolvedValue ( makeResponseMessage ( WalletResponseSuccessType . CONNECTION_SUCCESS ) ) ,
1919 getAccount : jest . fn ( ) . mockResolvedValue ( makeResponseMessage ( WalletResponseSuccessType . GET_ACCOUNT_SUCCESS ) ) ,
20+ getNetwork : jest . fn ( ) . mockResolvedValue ( makeResponseMessage ( WalletResponseSuccessType . GET_NETWORK_SUCCESS ) ) ,
2021 switchNetwork : jest . fn ( ) . mockResolvedValue ( makeResponseMessage ( WalletResponseSuccessType . SWITCH_NETWORK_SUCCESS ) ) ,
2122 addNetwork : jest . fn ( ) . mockResolvedValue ( makeResponseMessage ( WalletResponseSuccessType . ADD_NETWORK_SUCCESS ) ) ,
2223 signTransaction : jest . fn ( ) . mockResolvedValue ( makeResponseMessage ( WalletResponseSuccessType . SIGN_SUCCESS ) ) ,
@@ -41,6 +42,8 @@ export const addEstablishRejectMock = makeResponseMessage(WalletResponseRejectTy
4142
4243export const getAccountSuccessMock = makeResponseMessage ( WalletResponseSuccessType . GET_ACCOUNT_SUCCESS ) ;
4344
45+ export const getNetworkSuccessMock = makeResponseMessage ( WalletResponseSuccessType . GET_NETWORK_SUCCESS ) ;
46+
4447export const switchNetworkSuccessMock = makeResponseMessage ( WalletResponseSuccessType . SWITCH_NETWORK_SUCCESS ) ;
4548
4649export const addNetworkSuccessMock = makeResponseMessage < void > ( WalletResponseSuccessType . ADD_NETWORK_SUCCESS ) ;
Original file line number Diff line number Diff line change 1+ import { mockWalletProvider } from '../../__mocks__/mock-wallet-provider' ;
2+ import { getNetwork } from '../../methods' ;
3+ import { WalletResponseSuccessType } from '../../types' ;
4+ import { GetNetworkResponse } from '../../types/methods' ;
5+ import { makeResponseMessage } from '../../utils' ;
6+
7+ describe ( 'getNetwork' , ( ) => {
8+ afterEach ( ( ) => {
9+ jest . clearAllMocks ( ) ;
10+ } ) ;
11+
12+ it ( 'should call getNetwork and return the response' , async ( ) => {
13+ const mockResponse : GetNetworkResponse = makeResponseMessage ( WalletResponseSuccessType . GET_NETWORK_SUCCESS , {
14+ chainId : 'chainId' ,
15+ networkName : 'networkName' ,
16+ addressPrefix : 'g' ,
17+ rpcUrl : 'rpcUrl' ,
18+ indexerUrl : null ,
19+ } ) ;
20+
21+ mockWalletProvider . getNetwork . mockResolvedValue ( mockResponse ) ;
22+
23+ const response = await getNetwork ( mockWalletProvider ) ;
24+
25+ expect ( mockWalletProvider . getNetwork ) . toHaveBeenCalled ( ) ;
26+ expect ( response ) . toEqual ( mockResponse ) ;
27+ } ) ;
28+
29+ it ( 'should handle errors when getNetwork fails' , async ( ) => {
30+ const mockError = new Error ( 'Failed to get network' ) ;
31+ mockWalletProvider . getNetwork . mockRejectedValue ( mockError ) ;
32+
33+ await expect ( getNetwork ( mockWalletProvider ) ) . rejects . toThrow ( 'Failed to get network' ) ;
34+ } ) ;
35+ } ) ;
Original file line number Diff line number Diff line change 1+ import { WalletProvider } from '../providers' ;
2+ import { GetNetworkResponse } from '../types/methods' ;
3+
4+ export const getNetwork = ( walletProvider : WalletProvider ) : Promise < GetNetworkResponse > => {
5+ return walletProvider . getNetwork ( ) ;
6+ } ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ export * from './connect';
55export * from './disconnect' ;
66export * from './get-account' ;
77export * from './get-connection-state' ;
8+ export * from './get-network' ;
89export * from './is-connected' ;
910export * from './off-connection-change' ;
1011export * from './on-change-account' ;
Original file line number Diff line number Diff line change 66 BroadcastTransactionOptions ,
77 BroadcastTransactionResponse ,
88 GetAccountResponse ,
9+ GetNetworkResponse ,
910 IsConnectedResponse ,
1011 OnChangeAccountOptions ,
1112 OnChangeAccountResponse ,
@@ -34,6 +35,13 @@ export interface WalletProvider {
3435 */
3536 getAccount : ( ) => Promise < GetAccountResponse > ;
3637
38+ /**
39+ * Fetch information about the current connected network
40+ * @async
41+ * @returns Original Adena response with the network information
42+ */
43+ getNetwork : ( ) => Promise < GetNetworkResponse > ;
44+
3745 /**
3846 * Switches the Adena network to the given chain ID
3947 * @async
Original file line number Diff line number Diff line change 11export * from './account.types' ;
22export * from './config.types' ;
3+ export * from './network.types' ;
34export * from './transaction.types' ;
45export * from './wallet.types' ;
Original file line number Diff line number Diff line change 1+ import { NetworkInfo } from '../network.types' ;
2+ import { WalletResponse } from '../wallet.types' ;
3+
4+ export type GetNetworkResponse = WalletResponse < NetworkInfo > ;
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ export * from './broadcast-transaction.types';
44export * from './connect.types' ;
55export * from './disconnect.types' ;
66export * from './get-account.types' ;
7+ export * from './get-network.types' ;
78export * from './is-connected.types' ;
89export * from './off-connection-change' ;
910export * from './on-change-account.types' ;
Original file line number Diff line number Diff line change 1+ export type NetworkInfo = {
2+ chainId : string ;
3+ networkName : string ;
4+ addressPrefix : string ;
5+ rpcUrl : string ;
6+ indexerUrl : string | null ;
7+ } ;
You can’t perform that action at this time.
0 commit comments