Skip to content

Commit

Permalink
refactor: Move initialisation of guardianClient to root Passport object
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenfowler authored Feb 15, 2024
1 parent 70ea04a commit 837cb6c
Show file tree
Hide file tree
Showing 22 changed files with 78 additions and 115 deletions.
4 changes: 2 additions & 2 deletions packages/passport/sdk/src/Passport.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import {
mswHandlers,
} from './mocks/zkEvm/msw';
import { JsonRpcError, RpcErrorCode } from './zkEvm/JsonRpcError';
import GuardianClient from './guardian/guardian';
import GuardianClient from './guardian';
import { chainIdHex } from './test/mocks';

jest.mock('./guardian/guardian');
jest.mock('./guardian');

jest.mock('magic-sdk');
jest.mock('oidc-client-ts');
Expand Down
16 changes: 13 additions & 3 deletions packages/passport/sdk/src/Passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ConfirmationScreen } from './confirmation';
import { ZkEvmProvider } from './zkEvm';
import { Provider } from './zkEvm/types';
import TypedEventEmitter from './utils/typedEventEmitter';
import GuardianClient from './guardian';

const buildImxClientConfig = (passportModuleConfiguration: PassportModuleConfiguration) => {
if (passportModuleConfiguration.overrides) {
Expand Down Expand Up @@ -56,16 +57,21 @@ export const buildPrivateVars = (passportModuleConfiguration: PassportModuleConf
? passportModuleConfiguration.overrides.immutableXClient
: new IMXClient({ baseConfig: passportModuleConfiguration.baseConfig });

const guardianClient = new GuardianClient({
confirmationScreen,
config,
authManager,
});

const imxApiClients = buildImxApiClients(passportModuleConfiguration);

const passportImxProviderFactory = new PassportImxProviderFactory({
authManager,
config,
confirmationScreen,
immutableXClient,
magicAdapter,
passportEventEmitter,
imxApiClients,
guardianClient,
});

return {
Expand All @@ -77,6 +83,7 @@ export const buildPrivateVars = (passportModuleConfiguration: PassportModuleConf
multiRollupApiClients,
passportEventEmitter,
passportImxProviderFactory,
guardianClient,
};
};

Expand All @@ -97,6 +104,8 @@ export class Passport {

private readonly passportEventEmitter: TypedEventEmitter<PassportEventMap>;

private readonly guardianClient: GuardianClient;

constructor(passportModuleConfiguration: PassportModuleConfiguration) {
const privateVars = buildPrivateVars(passportModuleConfiguration);

Expand All @@ -108,6 +117,7 @@ export class Passport {
this.multiRollupApiClients = privateVars.multiRollupApiClients;
this.passportEventEmitter = privateVars.passportEventEmitter;
this.passportImxProviderFactory = privateVars.passportImxProviderFactory;
this.guardianClient = privateVars.guardianClient;

setPassportClientId(passportModuleConfiguration.clientId);
track('passport', 'initialised');
Expand All @@ -131,8 +141,8 @@ export class Passport {
authManager: this.authManager,
magicAdapter: this.magicAdapter,
config: this.config,
confirmationScreen: this.confirmationScreen,
multiRollupApiClients: this.multiRollupApiClients,
guardianClient: this.guardianClient,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as guardian from '@imtbl/guardian';
import { TransactionRequest } from '@ethersproject/providers';
import { ImmutableConfiguration } from '@imtbl/config';
import AuthManager from 'authManager';
import GuardianClient from './guardian';
import GuardianClient from './index';
import { mockUser, mockUserImx, mockUserZkEvm } from '../test/mocks';
import { JsonRpcError, RpcErrorCode } from '../zkEvm/JsonRpcError';
import { PassportConfiguration } from '../config';
Expand Down
File renamed without changes.
30 changes: 11 additions & 19 deletions packages/passport/sdk/src/starkEx/passportImxProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ import {
import { Web3Provider } from '@ethersproject/providers';
import { ImxApiClients } from '@imtbl/generated-clients';
import registerPassportStarkEx from './workflows/registration';
import { mockUser, mockUserImx, testConfig } from '../test/mocks';
import { mockUser, mockUserImx } from '../test/mocks';
import { PassportError, PassportErrorType } from '../errors/passportError';
import { PassportImxProvider } from './passportImxProvider';
import {
batchNftTransfer, cancelOrder, createOrder, createTrade, exchangeTransfer, transfer,
} from './workflows';
import { ConfirmationScreen } from '../confirmation';
import { PassportConfiguration } from '../config';
import { PassportEventMap, PassportEvents } from '../types';
import TypedEventEmitter from '../utils/typedEventEmitter';
import AuthManager from '../authManager';
import MagicAdapter from '../magicAdapter';
import { getStarkSigner } from './getStarkSigner';
import GuardianClient from '../guardian';

jest.mock('@ethersproject/providers');
jest.mock('./workflows');
Expand All @@ -48,8 +47,6 @@ describe('PassportImxProvider', () => {
}),
});

const confirmationScreen = new ConfirmationScreen({} as PassportConfiguration);

const mockAuthManager = {
login: jest.fn(),
getUser: jest.fn(),
Expand All @@ -70,6 +67,8 @@ describe('PassportImxProvider', () => {
login: jest.fn(),
};

const mockGuardianClient = {};

const getSignerMock = jest.fn();

let passportEventEmitter: TypedEventEmitter<PassportEventMap>;
Expand All @@ -91,9 +90,8 @@ describe('PassportImxProvider', () => {
passportImxProvider = new PassportImxProvider({
authManager: mockAuthManager as unknown as AuthManager,
magicAdapter: magicAdapterMock as unknown as MagicAdapter,
confirmationScreen,
guardianClient: mockGuardianClient as unknown as GuardianClient,
immutableXClient,
config: testConfig,
passportEventEmitter,
imxApiClients,
});
Expand Down Expand Up @@ -129,9 +127,8 @@ describe('PassportImxProvider', () => {
const pp = new PassportImxProvider({
authManager: mockAuthManager as unknown as AuthManager,
magicAdapter: magicAdapterMock as unknown as MagicAdapter,
confirmationScreen,
guardianClient: mockGuardianClient as unknown as GuardianClient,
immutableXClient,
config: testConfig,
passportEventEmitter: new TypedEventEmitter<PassportEventMap>(),
imxApiClients: new ImxApiClients({} as any),
});
Expand All @@ -154,8 +151,7 @@ describe('PassportImxProvider', () => {
user: mockUserImx,
starkSigner: mockStarkSigner,
transfersApi: immutableXClient.transfersApi,
// @ts-ignore
guardianClient: passportImxProvider.guardianClient,
guardianClient: mockGuardianClient,
});
expect(result)
.toEqual(returnValue);
Expand Down Expand Up @@ -198,8 +194,7 @@ describe('PassportImxProvider', () => {
user: mockUserImx,
starkSigner: mockStarkSigner,
ordersApi: immutableXClient.ordersApi,
// @ts-ignore
guardianClient: passportImxProvider.guardianClient,
guardianClient: mockGuardianClient,
});
expect(result)
.toEqual(returnValue);
Expand All @@ -220,8 +215,7 @@ describe('PassportImxProvider', () => {
user: mockUserImx,
starkSigner: mockStarkSigner,
ordersApi: immutableXClient.ordersApi,
// @ts-ignore
guardianClient: passportImxProvider.guardianClient,
guardianClient: mockGuardianClient,
});
expect(result)
.toEqual(returnValue);
Expand All @@ -242,8 +236,7 @@ describe('PassportImxProvider', () => {
user: mockUserImx,
starkSigner: mockStarkSigner,
tradesApi: immutableXClient.tradesApi,
// @ts-ignore
guardianClient: passportImxProvider.guardianClient,
guardianClient: mockGuardianClient,
});
expect(result)
.toEqual(returnValue);
Expand All @@ -264,8 +257,7 @@ describe('PassportImxProvider', () => {
user: mockUserImx,
starkSigner: mockStarkSigner,
transfersApi: immutableXClient.transfersApi,
// @ts-ignore
guardianClient: passportImxProvider.guardianClient,
guardianClient: mockGuardianClient,
});
expect(result)
.toEqual(returnValue);
Expand Down
16 changes: 4 additions & 12 deletions packages/passport/sdk/src/starkEx/passportImxProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,25 @@ import { Web3Provider } from '@ethersproject/providers';
import { ImxApiClients } from '@imtbl/generated-clients';
import TypedEventEmitter from '../utils/typedEventEmitter';
import AuthManager from '../authManager';
import GuardianClient from '../guardian/guardian';
import GuardianClient from '../guardian';
import {
PassportEventMap, PassportEvents, UserImx, User, IMXSigners,
} from '../types';
import { PassportError, PassportErrorType } from '../errors/passportError';
import {
batchNftTransfer, cancelOrder, createOrder, createTrade, exchangeTransfer, transfer,
} from './workflows';
import { ConfirmationScreen } from '../confirmation';
import { PassportConfiguration } from '../config';
import registerOffchain from './workflows/registerOffchain';
import MagicAdapter from '../magicAdapter';
import { getStarkSigner } from './getStarkSigner';

export interface PassportImxProviderOptions {
authManager: AuthManager;
immutableXClient: IMXClient;
confirmationScreen: ConfirmationScreen;
config: PassportConfiguration;
passportEventEmitter: TypedEventEmitter<PassportEventMap>;
magicAdapter: MagicAdapter;
imxApiClients: ImxApiClients;
guardianClient: GuardianClient;
}

type AuthenticatedUserAndSigners = {
Expand Down Expand Up @@ -84,21 +81,16 @@ export class PassportImxProvider implements IMXProvider {
constructor({
authManager,
immutableXClient,
confirmationScreen,
config,
passportEventEmitter,
magicAdapter,
imxApiClients,
guardianClient,
}: PassportImxProviderOptions) {
this.authManager = authManager;
this.immutableXClient = immutableXClient;
this.guardianClient = new GuardianClient({
confirmationScreen,
config,
authManager,
});
this.magicAdapter = magicAdapter;
this.imxApiClients = imxApiClients;
this.guardianClient = guardianClient;
this.initialiseSigners();

passportEventEmitter.on(PassportEvents.LOGGED_OUT, this.handleLogout);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { IMXClient } from '@imtbl/x-client';
import { ImxApiClients } from '@imtbl/generated-clients';
import { ConfirmationScreen } from '../confirmation';
import registerPassportStarkEx from './workflows/registration';
import { PassportImxProviderFactory } from './passportImxProviderFactory';
import MagicAdapter from '../magicAdapter';
import AuthManager from '../authManager';
import { PassportError, PassportErrorType } from '../errors/passportError';
import { PassportEventMap } from '../types';
import { mockUserImx, testConfig } from '../test/mocks';
import { mockUserImx } from '../test/mocks';
import TypedEventEmitter from '../utils/typedEventEmitter';
import { PassportImxProvider } from './passportImxProvider';
import GuardianClient from '../guardian';

jest.mock('./workflows/registration');
jest.mock('./passportImxProvider');
Expand All @@ -27,17 +27,15 @@ describe('PassportImxProviderFactory', () => {
const immutableXClient = {
usersApi: {},
} as IMXClient;
const confirmationScreen = {} as ConfirmationScreen;
const config = testConfig;
const guardianClient = {} as GuardianClient;
const passportEventEmitter = new TypedEventEmitter<PassportEventMap>();
const passportImxProviderFactory = new PassportImxProviderFactory({
config,
confirmationScreen,
immutableXClient,
authManager: mockAuthManager as unknown as AuthManager,
magicAdapter: mockMagicAdapter as unknown as MagicAdapter,
passportEventEmitter,
imxApiClients,
guardianClient,
});
const mockPassportImxProvider = {};

Expand Down Expand Up @@ -89,10 +87,9 @@ describe('PassportImxProviderFactory', () => {
magicAdapter: mockMagicAdapter,
authManager: mockAuthManager,
immutableXClient,
config,
confirmationScreen,
passportEventEmitter,
imxApiClients: new ImxApiClients({} as any),
guardianClient,
});
});
});
Expand Down
21 changes: 7 additions & 14 deletions packages/passport/sdk/src/starkEx/passportImxProviderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@ import { IMXClient } from '@imtbl/x-client';
import { IMXProvider } from '@imtbl/x-provider';
import { ImxApiClients } from '@imtbl/generated-clients';
import { PassportError, PassportErrorType } from '../errors/passportError';
import { PassportConfiguration } from '../config';
import AuthManager from '../authManager';
import { ConfirmationScreen } from '../confirmation';
import MagicAdapter from '../magicAdapter';
import { PassportEventMap, User } from '../types';
import TypedEventEmitter from '../utils/typedEventEmitter';
import { PassportImxProvider } from './passportImxProvider';
import GuardianClient from '../guardian';

export type PassportImxProviderFactoryInput = {
authManager: AuthManager;
config: PassportConfiguration;
confirmationScreen: ConfirmationScreen;
immutableXClient: IMXClient;
magicAdapter: MagicAdapter;
passportEventEmitter: TypedEventEmitter<PassportEventMap>;
imxApiClients: ImxApiClients;
guardianClient: GuardianClient;
};

export class PassportImxProviderFactory {
private readonly authManager: AuthManager;

private readonly config: PassportConfiguration;

private readonly confirmationScreen: ConfirmationScreen;

private readonly immutableXClient: IMXClient;

private readonly magicAdapter: MagicAdapter;
Expand All @@ -35,22 +29,22 @@ export class PassportImxProviderFactory {

public readonly imxApiClients: ImxApiClients;

private readonly guardianClient: GuardianClient;

constructor({
authManager,
config,
confirmationScreen,
immutableXClient,
magicAdapter,
passportEventEmitter,
imxApiClients,
guardianClient,
}: PassportImxProviderFactoryInput) {
this.authManager = authManager;
this.config = config;
this.confirmationScreen = confirmationScreen;
this.immutableXClient = immutableXClient;
this.magicAdapter = magicAdapter;
this.passportEventEmitter = passportEventEmitter;
this.imxApiClients = imxApiClients;
this.guardianClient = guardianClient;
}

public async getProvider(): Promise<IMXProvider> {
Expand Down Expand Up @@ -85,13 +79,12 @@ export class PassportImxProviderFactory {
}

return new PassportImxProvider({
config: this.config,
authManager: this.authManager,
immutableXClient: this.immutableXClient,
confirmationScreen: this.confirmationScreen,
passportEventEmitter: this.passportEventEmitter,
magicAdapter: this.magicAdapter,
imxApiClients: this.imxApiClients,
guardianClient: this.guardianClient,
});
}
}
4 changes: 2 additions & 2 deletions packages/passport/sdk/src/starkEx/workflows/order.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ETHAmount, OrdersApi, UnsignedOrderRequest } from '@imtbl/core-sdk';
import GuardianClient from '../../guardian/guardian';
import GuardianClient from '../../guardian';
import { PassportError, PassportErrorType } from '../../errors/passportError';
import { mockErrorMessage, mockStarkSignature, mockUserImx } from '../../test/mocks';
import { cancelOrder, createOrder } from './order';

jest.mock('../../guardian/guardian');
jest.mock('../../guardian');

describe('order', () => {
const mockGuardianClient = new GuardianClient({} as any);
Expand Down
Loading

0 comments on commit 837cb6c

Please sign in to comment.