Skip to content

Commit fd5cb22

Browse files
authored
feat: add a function to retrieve current network information (#14)
1 parent 3ffe803 commit fd5cb22

File tree

16 files changed

+129
-1
lines changed

16 files changed

+129
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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

141153
Switches the wallet to a different network.

packages/sdk/src/core/__mocks__/mock-wallet-provider.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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

4243
export const getAccountSuccessMock = makeResponseMessage(WalletResponseSuccessType.GET_ACCOUNT_SUCCESS);
4344

45+
export const getNetworkSuccessMock = makeResponseMessage(WalletResponseSuccessType.GET_NETWORK_SUCCESS);
46+
4447
export const switchNetworkSuccessMock = makeResponseMessage(WalletResponseSuccessType.SWITCH_NETWORK_SUCCESS);
4548

4649
export const addNetworkSuccessMock = makeResponseMessage<void>(WalletResponseSuccessType.ADD_NETWORK_SUCCESS);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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+
};

packages/sdk/src/core/methods/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './connect';
55
export * from './disconnect';
66
export * from './get-account';
77
export * from './get-connection-state';
8+
export * from './get-network';
89
export * from './is-connected';
910
export * from './off-connection-change';
1011
export * from './on-change-account';

packages/sdk/src/core/providers/wallet.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
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
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './account.types';
22
export * from './config.types';
3+
export * from './network.types';
34
export * from './transaction.types';
45
export * from './wallet.types';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { NetworkInfo } from '../network.types';
2+
import { WalletResponse } from '../wallet.types';
3+
4+
export type GetNetworkResponse = WalletResponse<NetworkInfo>;

packages/sdk/src/core/types/methods/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './broadcast-transaction.types';
44
export * from './connect.types';
55
export * from './disconnect.types';
66
export * from './get-account.types';
7+
export * from './get-network.types';
78
export * from './is-connected.types';
89
export * from './off-connection-change';
910
export * from './on-change-account.types';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export type NetworkInfo = {
2+
chainId: string;
3+
networkName: string;
4+
addressPrefix: string;
5+
rpcUrl: string;
6+
indexerUrl: string | null;
7+
};

0 commit comments

Comments
 (0)