Skip to content

Commit

Permalink
retrieve chain id from api instead of hardcoding (#1370)
Browse files Browse the repository at this point in the history
  • Loading branch information
scastleman-immutable authored Jan 29, 2024
1 parent adfff27 commit bf67f11
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/passport/sdk/src/Passport.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ describe('Passport', () => {
mockSigninSilent.mockResolvedValueOnce(mockOidcUserZkevm);
useMswHandlers([
mswHandlers.counterfactualAddress.success,
mswHandlers.api.chains.success,
]);

const zkEvmProvider = getZkEvmProvider();
Expand All @@ -160,6 +161,7 @@ describe('Passport', () => {
mockSigninSilent.mockResolvedValue(mockOidcUserZkevm);
useMswHandlers([
mswHandlers.counterfactualAddress.internalServerError,
mswHandlers.api.chains.success,
]);

const zkEvmProvider = getZkEvmProvider();
Expand Down
13 changes: 13 additions & 0 deletions packages/passport/sdk/src/mocks/zkEvm/msw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ export const mswHandlers = {
success: rest.post('https://api.sandbox.immutable.com/guardian/v1/transactions/evm/evaluate', (req, res, ctx) => res(ctx.status(200))),
},
},
api: {
chains: {
success: rest.get('https://api.sandbox.immutable.com/v1/chains', async (req, res, ctx) => res(ctx.json({
result: [
{
id: 'eip155:13473',
name: 'Immutable zkEVM Test',
rpc_url: 'https://rpc.testnet.immutable.com',
},
],
}))),
},
},
};

let mswWorker: SetupServer;
Expand Down
12 changes: 12 additions & 0 deletions packages/passport/sdk/src/test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@ export const mockLinkedAddresses = {
],
},
};

export const mockListChains = {
data: {
result: [
{
id: 'eip155:13473',
name: 'Immutable zkEVM Test',
rpc_url: 'https://rpc.testnet.immutable.com',
},
],
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MultiRollupApiClients } from '@imtbl/generated-clients';
import { ChainId, ChainName } from 'network/chains';
import { registerZkEvmUser } from './registerZkEvmUser';
import AuthManager from '../../authManager';
import { mockUser, mockUserZkEvm } from '../../test/mocks';
import { mockListChains, mockUser, mockUserZkEvm } from '../../test/mocks';

jest.mock('@ethersproject/providers');
jest.mock('@imtbl/toolkit');
Expand All @@ -23,6 +23,9 @@ describe('registerZkEvmUser', () => {
passportApi: {
createCounterfactualAddressV2: jest.fn(),
},
chainsApi: {
listChains: jest.fn(),
},
};
const jsonRPCProvider = {
ready: {
Expand All @@ -41,14 +44,14 @@ describe('registerZkEvmUser', () => {
getSignerMock.mockReturnValue(ethSignerMock);
ethSignerMock.getAddress.mockResolvedValue(ethereumAddress);
(signRaw as jest.Mock).mockResolvedValue(ethereumSignature);
multiRollupApiClients.chainsApi.listChains.mockImplementation(() => mockListChains);
});

describe('when createCounterfactualAddressV2 doesn\'t return a 201', () => {
it('should throw an error', async () => {
multiRollupApiClients.passportApi.createCounterfactualAddressV2.mockImplementation(() => {
throw new Error('Internal server error');
});

await expect(async () => registerZkEvmUser({
authManager: authManager as unknown as AuthManager,
magicProvider,
Expand Down
6 changes: 4 additions & 2 deletions packages/passport/sdk/src/zkEvm/user/registerZkEvmUser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExternalProvider, JsonRpcProvider, Web3Provider } from '@ethersproject/providers';
import { MultiRollupApiClients } from '@imtbl/generated-clients';
import { signRaw } from '@imtbl/toolkit';
import { CHAIN_NAME_MAP } from 'network/constants';
import { getEip155ChainId } from 'zkEvm/walletHelpers';
import { UserZkEvm } from '../../types';
import AuthManager from '../../authManager';
import { JsonRpcError, RpcErrorCode } from '../JsonRpcError';
Expand Down Expand Up @@ -34,9 +34,11 @@ export async function registerZkEvmUser({
const headers = { Authorization: `Bearer ${accessToken}` };

const { chainId } = await jsonRpcProvider.ready;
const eipChainId = getEip155ChainId(chainId);

try {
const chainName = CHAIN_NAME_MAP.get(chainId);
const chainList = (await multiRollupApiClients.chainsApi.listChains()).data.result;
const chainName = chainList.find((chain) => chain.id === eipChainId)?.name;
if (!chainName) {
throw new JsonRpcError(RpcErrorCode.INTERNAL_ERROR, `Chain name does not exist on for chain id ${chainId}`);
}
Expand Down

0 comments on commit bf67f11

Please sign in to comment.