Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(SNAP): refactor starkNet_getCurrentNetwork api #396

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions packages/starknet-snap/src/getCurrentNetwork.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/starknet-snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { declareContract } from './declareContract';
import { estimateAccDeployFee } from './estimateAccountDeployFee';
import { estimateFees } from './estimateFees';
import { extractPublicKey } from './extractPublicKey';
import { getCurrentNetwork } from './getCurrentNetwork';
import { getErc20TokenBalance } from './getErc20TokenBalance';
import { getStarkName } from './getStarkName';
import { getStoredErc20Tokens } from './getStoredErc20Tokens';
Expand Down Expand Up @@ -48,6 +47,7 @@ import {
switchNetwork,
getDeploymentData,
watchAsset,
getCurrentNetwork,
} from './rpcs';
import { sendTransaction } from './sendTransaction';
import { signDeployAccountTransaction } from './signDeployAccountTransaction';
Expand Down Expand Up @@ -241,7 +241,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
);

case 'starkNet_getCurrentNetwork':
return await getCurrentNetwork(apiParams);
return await getCurrentNetwork.execute(null);

case 'starkNet_getStoredNetworks':
return await getStoredNetworks(apiParams);
Expand Down
38 changes: 38 additions & 0 deletions packages/starknet-snap/src/rpcs/get-current-network.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { NetworkStateManager } from '../state/network-state-manager';
import type { Network } from '../types/snapState';
import { STARKNET_MAINNET_NETWORK } from '../utils/constants';
import { getCurrentNetwork } from './get-current-network';

jest.mock('../utils/logger');

describe('getCurrentNetwork', () => {
const mockNetworkStateManager = ({
currentNetwork = STARKNET_MAINNET_NETWORK,
}: {
currentNetwork?: Network;
}) => {
const getCurrentNetworkSpy = jest.spyOn(
NetworkStateManager.prototype,
'getCurrentNetwork',
);

getCurrentNetworkSpy.mockResolvedValue(currentNetwork);

return { getCurrentNetworkSpy };
};

it('return the selected network', async () => {
const currentNetwork = STARKNET_MAINNET_NETWORK;
const { getCurrentNetworkSpy } = mockNetworkStateManager({
currentNetwork,
});

const result = await getCurrentNetwork.execute(null);

expect(getCurrentNetworkSpy).toHaveBeenCalled();
expect(result).toStrictEqual({
name: currentNetwork.name,
chainId: currentNetwork.chainId,
});
});
});
58 changes: 58 additions & 0 deletions packages/starknet-snap/src/rpcs/get-current-network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { constants } from 'starknet';
import type { Infer } from 'superstruct';
import { literal, string, object } from 'superstruct';

import { NetworkStateManager } from '../state/network-state-manager';
import { RpcController, ChainIdStruct } from '../utils';

export const GetCurrentNetworkRequestStruct = literal(null);

export const GetCurrentNetworkResponseStruct = object({
name: string(),
chainId: ChainIdStruct,
});

export type GetCurrentNetworkParams = Infer<
typeof GetCurrentNetworkRequestStruct
>;

export type GetCurrentNetworkResponse = Infer<
typeof GetCurrentNetworkResponseStruct
>;

/**
* The RPC handler to get the current network.
*/
export class GetCurrentNetworkRpc extends RpcController<
GetCurrentNetworkParams,
GetCurrentNetworkResponse
> {
protected requestStruct = GetCurrentNetworkRequestStruct;

protected responseStruct = GetCurrentNetworkResponseStruct;

/**
* Execute the get the current network.
*
* @param _
* @returns A promise that resolve to the current network.
*/
async execute(
_: GetCurrentNetworkParams,
): Promise<GetCurrentNetworkResponse> {
return super.execute(_);
}

protected async handleRequest(
_: GetCurrentNetworkParams,
): Promise<GetCurrentNetworkResponse> {
const networkStateMgr = new NetworkStateManager();
const network = await networkStateMgr.getCurrentNetwork();
return {
name: network.name,
chainId: network.chainId as unknown as constants.StarknetChainId,
};
}
}

export const getCurrentNetwork = new GetCurrentNetworkRpc();
1 change: 1 addition & 0 deletions packages/starknet-snap/src/rpcs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './verify-signature';
export * from './switch-network';
export * from './get-deployment-data';
export * from './watch-asset';
export * from './get-current-network';
56 changes: 0 additions & 56 deletions packages/starknet-snap/test/src/getCurrentNetwork.test.ts

This file was deleted.

Loading