Skip to content

Commit

Permalink
feat: add a function to retrieve current network information (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss authored Nov 4, 2024
1 parent 3ffe803 commit fd5cb22
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ adenaSDK.getAccount().then((account) => {
});
```

### `getNetwork`

Retrieves network information from the connected wallet.

**Example:**

```
adenaSDK.getNetwork().then((network) => {
console.log('Network:', network);
});
```

### `switchNetwork`

Switches the wallet to a different network.
Expand Down
3 changes: 3 additions & 0 deletions packages/sdk/src/core/__mocks__/mock-wallet-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const mockWalletProvider: jest.Mocked<WalletProvider> = {
isConnected: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.CONNECTION_SUCCESS)),
addEstablish: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.CONNECTION_SUCCESS)),
getAccount: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.GET_ACCOUNT_SUCCESS)),
getNetwork: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.GET_NETWORK_SUCCESS)),
switchNetwork: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.SWITCH_NETWORK_SUCCESS)),
addNetwork: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.ADD_NETWORK_SUCCESS)),
signTransaction: jest.fn().mockResolvedValue(makeResponseMessage(WalletResponseSuccessType.SIGN_SUCCESS)),
Expand All @@ -41,6 +42,8 @@ export const addEstablishRejectMock = makeResponseMessage(WalletResponseRejectTy

export const getAccountSuccessMock = makeResponseMessage(WalletResponseSuccessType.GET_ACCOUNT_SUCCESS);

export const getNetworkSuccessMock = makeResponseMessage(WalletResponseSuccessType.GET_NETWORK_SUCCESS);

export const switchNetworkSuccessMock = makeResponseMessage(WalletResponseSuccessType.SWITCH_NETWORK_SUCCESS);

export const addNetworkSuccessMock = makeResponseMessage<void>(WalletResponseSuccessType.ADD_NETWORK_SUCCESS);
Expand Down
35 changes: 35 additions & 0 deletions packages/sdk/src/core/__tests__/methods/get-network.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { mockWalletProvider } from '../../__mocks__/mock-wallet-provider';
import { getNetwork } from '../../methods';
import { WalletResponseSuccessType } from '../../types';
import { GetNetworkResponse } from '../../types/methods';
import { makeResponseMessage } from '../../utils';

describe('getNetwork', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should call getNetwork and return the response', async () => {
const mockResponse: GetNetworkResponse = makeResponseMessage(WalletResponseSuccessType.GET_NETWORK_SUCCESS, {
chainId: 'chainId',
networkName: 'networkName',
addressPrefix: 'g',
rpcUrl: 'rpcUrl',
indexerUrl: null,
});

mockWalletProvider.getNetwork.mockResolvedValue(mockResponse);

const response = await getNetwork(mockWalletProvider);

expect(mockWalletProvider.getNetwork).toHaveBeenCalled();
expect(response).toEqual(mockResponse);
});

it('should handle errors when getNetwork fails', async () => {
const mockError = new Error('Failed to get network');
mockWalletProvider.getNetwork.mockRejectedValue(mockError);

await expect(getNetwork(mockWalletProvider)).rejects.toThrow('Failed to get network');
});
});
6 changes: 6 additions & 0 deletions packages/sdk/src/core/methods/get-network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { WalletProvider } from '../providers';
import { GetNetworkResponse } from '../types/methods';

export const getNetwork = (walletProvider: WalletProvider): Promise<GetNetworkResponse> => {
return walletProvider.getNetwork();
};
1 change: 1 addition & 0 deletions packages/sdk/src/core/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './connect';
export * from './disconnect';
export * from './get-account';
export * from './get-connection-state';
export * from './get-network';
export * from './is-connected';
export * from './off-connection-change';
export * from './on-change-account';
Expand Down
8 changes: 8 additions & 0 deletions packages/sdk/src/core/providers/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BroadcastTransactionOptions,
BroadcastTransactionResponse,
GetAccountResponse,
GetNetworkResponse,
IsConnectedResponse,
OnChangeAccountOptions,
OnChangeAccountResponse,
Expand Down Expand Up @@ -34,6 +35,13 @@ export interface WalletProvider {
*/
getAccount: () => Promise<GetAccountResponse>;

/**
* Fetch information about the current connected network
* @async
* @returns Original Adena response with the network information
*/
getNetwork: () => Promise<GetNetworkResponse>;

/**
* Switches the Adena network to the given chain ID
* @async
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/core/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './account.types';
export * from './config.types';
export * from './network.types';
export * from './transaction.types';
export * from './wallet.types';
4 changes: 4 additions & 0 deletions packages/sdk/src/core/types/methods/get-network.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { NetworkInfo } from '../network.types';
import { WalletResponse } from '../wallet.types';

export type GetNetworkResponse = WalletResponse<NetworkInfo>;
1 change: 1 addition & 0 deletions packages/sdk/src/core/types/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './broadcast-transaction.types';
export * from './connect.types';
export * from './disconnect.types';
export * from './get-account.types';
export * from './get-network.types';
export * from './is-connected.types';
export * from './off-connection-change';
export * from './on-change-account.types';
Expand Down
7 changes: 7 additions & 0 deletions packages/sdk/src/core/types/network.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type NetworkInfo = {
chainId: string;
networkName: string;
addressPrefix: string;
rpcUrl: string;
indexerUrl: string | null;
};
14 changes: 14 additions & 0 deletions packages/sdk/src/core/types/wallet.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type WalletResponseType =
export enum WalletResponseSuccessType {
CONNECTION_SUCCESS = 'CONNECTION_SUCCESS',
GET_ACCOUNT_SUCCESS = 'GET_ACCOUNT',
GET_NETWORK_SUCCESS = 'GET_NETWORK',
SIGN_SUCCESS = 'SIGN_TX',
ADD_NETWORK_SUCCESS = 'ADD_NETWORK_SUCCESS',
SWITCH_NETWORK_SUCCESS = 'SWITCH_NETWORK_SUCCESS',
Expand Down Expand Up @@ -65,6 +66,7 @@ export enum WalletResponseExecuteType {
ADD_ESTABLISH = 'ADD_ESTABLISH',
DO_CONTRACT = 'DO_CONTRACT',
GET_ACCOUNT = 'GET_ACCOUNT',
GET_NETWORK = 'GET_NETWORK',
SIGN_AMINO = 'SIGN_AMINO',
SIGN_TX = 'SIGN_TX',
ADD_NETWORK = 'ADD_NETWORK',
Expand All @@ -87,6 +89,12 @@ const WalletSuccessMessageInfo: Record<
type: WalletResponseSuccessType.GET_ACCOUNT_SUCCESS,
message: 'Account information has been successfully returned.',
},
GET_NETWORK: {
code: 0,
status: WalletResponseStatus.SUCCESS,
type: WalletResponseSuccessType.GET_NETWORK_SUCCESS,
message: 'Network information has been successfully returned.',
},
SIGN_TX: {
code: 0,
status: WalletResponseStatus.SUCCESS,
Expand Down Expand Up @@ -267,6 +275,12 @@ const WalletExecuteMessageInfo: Record<
type: WalletResponseExecuteType.GET_ACCOUNT,
message: 'Get Account Information.',
},
GET_NETWORK: {
code: 0,
status: WalletResponseStatus.SUCCESS,
type: WalletResponseExecuteType.GET_NETWORK,
message: 'Get Network Information.',
},
SIGN_AMINO: {
code: 0,
status: WalletResponseStatus.SUCCESS,
Expand Down
11 changes: 10 additions & 1 deletion packages/sdk/src/providers/adena-wallet/adena-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Tx } from '@gnolang/tm2-js-client';

import { makeResponseMessage } from '../../core';
import { WalletProvider } from '../../core/providers';
import { AccountInfo, WalletResponseFailureType, WalletResponseSuccessType } from '../../core/types';
import { AccountInfo, NetworkInfo, WalletResponseFailureType, WalletResponseSuccessType } from '../../core/types';
import {
AddEstablishOptions,
AddEstablishResponse,
Expand All @@ -11,6 +11,7 @@ import {
BroadcastTransactionOptions,
BroadcastTransactionResponse,
GetAccountResponse,
GetNetworkResponse,
IsConnectedResponse,
OnChangeAccountOptions,
OnChangeAccountResponse,
Expand Down Expand Up @@ -66,6 +67,14 @@ export class AdenaWalletProvider implements WalletProvider {
return mapResponseByAdenaResponse<AccountInfo>(response, accountInfo);
}

async getNetwork(): Promise<GetNetworkResponse> {
const adena = this.getAdena();
const response = await adena.GetNetwork();
const networkInfo: NetworkInfo = response.data;

return mapResponseByAdenaResponse<NetworkInfo>(response, networkInfo);
}

async switchNetwork(options: SwitchNetworkOptions): Promise<SwitchNetworkResponse> {
const adena = this.getAdena();
const response = await adena.SwitchNetwork(options.chainId);
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/providers/adena-wallet/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './adena-wallet';
export * from './types';
3 changes: 3 additions & 0 deletions packages/sdk/src/providers/adena-wallet/types/adena.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AdenaAddNetwork,
AdenaDoContract,
AdenaGetAccount,
AdenaGetNetwork,
AdenaOnEvent,
AdenaSignTx,
AdenaSwitchNetwork,
Expand All @@ -14,6 +15,8 @@ export type AdenaWallet = {

GetAccount: AdenaGetAccount;

GetNetwork: AdenaGetNetwork;

// Network
SwitchNetwork: AdenaSwitchNetwork;

Expand Down
18 changes: 18 additions & 0 deletions packages/sdk/src/providers/adena-wallet/types/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,21 @@ export type GetAccountResponseData = {
type GetAccountResponse = AdenaResponse<GetAccountResponseType, GetAccountResponseData>;

export type AdenaGetAccount = () => Promise<GetAccountResponse>;

enum GetNetworkResponseType {
GET_NETWORK_SUCCESS = 'GET_NETWORK_SUCCESS',
NO_ACCOUNT = 'NO_ACCOUNT',
WALLET_LOCKED = 'WALLET_LOCKED',
}

export type GetNetworkResponseData = {
chainId: string;
networkName: string;
addressPrefix: string;
rpcUrl: string;
indexerUrl: string | null;
};

type GetNetworkResponse = AdenaResponse<GetNetworkResponseType, GetNetworkResponseData>;

export type AdenaGetNetwork = () => Promise<GetNetworkResponse>;
5 changes: 5 additions & 0 deletions packages/sdk/src/providers/gno-wallet/gno-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
BroadcastTransactionOptions,
BroadcastTransactionResponse,
GetAccountResponse,
GetNetworkResponse,
IsConnectedResponse,
OnChangeAccountResponse,
OnChangeNetworkResponse,
Expand Down Expand Up @@ -89,6 +90,10 @@ export class GnoWalletProvider implements TM2WalletProvider {
}
}

async getNetwork(): Promise<GetNetworkResponse> {
throw new Error('not supported');
}

async switchNetwork(): Promise<SwitchNetworkResponse> {
throw new Error('not supported');
}
Expand Down

0 comments on commit fd5cb22

Please sign in to comment.