Skip to content

Commit

Permalink
Update providers
Browse files Browse the repository at this point in the history
  • Loading branch information
mgavrila committed Dec 11, 2024
1 parent 6d93f6d commit aab270d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 58 deletions.
10 changes: 1 addition & 9 deletions src/core/providers-strategy/CrossWindowProviderStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { isBrowserWithPopupConfirmation } from 'constants/browser.constants';
import {
IProvider,
ProviderTypeEnum
} from 'core/providers/types/providerFactory.types';
import { IProvider } from 'core/providers/types/providerFactory.types';
import { CrossWindowProvider } from 'lib/sdkWebWalletCrossWindowProvider';
import { networkSelector } from 'store/selectors/networkSelectors';
import { getState } from 'store/store';
Expand Down Expand Up @@ -47,12 +44,7 @@ export class CrossWindowProviderStrategy {

private buildProvider = () => {
const provider = this.provider as unknown as IProvider;
provider.getType = this.getType;

return provider;
};

private getType = (): ProviderTypeEnum.crossWindow => {
return ProviderTypeEnum.crossWindow;
};
}
20 changes: 9 additions & 11 deletions src/core/providers-strategy/CustomProviderStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ export class CustomProviderStrategy {
private type: string = '';
private config: IProviderConfig = {};

constructor(
type: string,
customProvider: ICustomProvider<ProviderTypeEnum>,
config: IProviderConfig
) {
constructor({
type,
customProvider,
config
}: {
type: string;
customProvider: ICustomProvider<ProviderTypeEnum>;
config: IProviderConfig;
}) {
this.type = type;
this.provider = customProvider;
this.config = config;
Expand All @@ -37,12 +41,6 @@ export class CustomProviderStrategy {
throw new Error('Provider is not initialized');
}

provider.getType = this.getType;

return provider;
};

private getType = () => {
return this.type;
};
}
10 changes: 1 addition & 9 deletions src/core/providers-strategy/ExtensionProviderStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { ExtensionProvider } from '@multiversx/sdk-extension-provider/out/extensionProvider';
import {
IProvider,
ProviderTypeEnum
} from 'core/providers/types/providerFactory.types';
import { IProvider } from 'core/providers/types/providerFactory.types';

export class ExtensionProviderStrategy {
private provider: ExtensionProvider | null = null;
Expand All @@ -18,12 +15,7 @@ export class ExtensionProviderStrategy {

private buildProvider = () => {
const provider = this.provider as unknown as IProvider;
provider.getType = this.getType;

return provider;
};

private getType = (): ProviderTypeEnum.extension => {
return ProviderTypeEnum.extension;
};
}
19 changes: 1 addition & 18 deletions src/core/providers-strategy/IFrameProviderStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { IframeProvider } from '@multiversx/sdk-web-wallet-iframe-provider/out';
import { IframeLoginTypes } from '@multiversx/sdk-web-wallet-iframe-provider/out/constants';
import {
IProvider,
ProviderTypeEnum
} from 'core/providers/types/providerFactory.types';
import { IProvider } from 'core/providers/types/providerFactory.types';
import { networkSelector } from 'store/selectors/networkSelectors';
import { getState } from 'store/store';

Expand Down Expand Up @@ -33,25 +30,11 @@ export class IFrameProviderStrategy {

private buildProvider = () => {
const provider = this.provider as unknown as IProvider;
provider.getType = this.getType;

if (this.address) {
provider.setAccount({ address: this.address });
}

return provider;
};

private getType = ():
| ProviderTypeEnum.metamask
| ProviderTypeEnum.passkey => {
switch (this.type) {
case IframeLoginTypes.metamask:
return ProviderTypeEnum.metamask;
case IframeLoginTypes.passkey:
return ProviderTypeEnum.passkey;
default:
throw new Error(`Unsupported type: ${this.type}`);
}
};
}
10 changes: 2 additions & 8 deletions src/core/providers-strategy/WalletConnectProviderStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { WalletConnectStateManager } from 'core/providers/helpers/walletConnect/
import {
IEventBus,
IProvider,
IProviderConfig,
ProviderTypeEnum
IProviderConfig
} from 'core/providers/types/providerFactory.types';
import { defineCustomElements, WalletConnectModal } from 'lib/sdkDappCoreUi';
import { logoutAction } from 'store/actions';
Expand Down Expand Up @@ -82,7 +81,6 @@ export class WalletConnectProviderStrategy {

private buildProvider = () => {
const provider = { ...this.provider } as unknown as IProvider;
provider.getType = this.getType;
provider.login = this.login;
provider.logout = this.logout;

Expand All @@ -91,7 +89,7 @@ export class WalletConnectProviderStrategy {

private validateConfig = () => {
if (!this.config?.walletConnectV2ProjectId) {
throw new Error('Invalid walletConnect config');
throw new Error(WalletConnectV2Error.invalidConfig);
}
};

Expand Down Expand Up @@ -178,10 +176,6 @@ export class WalletConnectProviderStrategy {
}
};

private getType = (): ProviderTypeEnum.walletConnect => {
return ProviderTypeEnum.walletConnect;
};

private login = async (options?: {
callbackUrl?: string;
token?: string;
Expand Down
7 changes: 4 additions & 3 deletions src/core/providers/ProviderFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ export class ProviderFactory {

default: {
for (const customProvider of this._customProviders) {
const providerInstance = new CustomProviderStrategy(
type as string,
const providerInstance = new CustomProviderStrategy({
type: type as ProviderTypeEnum,
customProvider,
config
);
});

createdProvider = await providerInstance.createProvider();
}
Expand All @@ -124,6 +124,7 @@ export class ProviderFactory {
throw new Error('Unable to create provider');
}

createdProvider.getType = () => type;
const dappProvider = new DappProvider(createdProvider);

setAccountProvider(dappProvider);
Expand Down

0 comments on commit aab270d

Please sign in to comment.