Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/development' into amg_provider_s…
Browse files Browse the repository at this point in the history
…trategy
  • Loading branch information
mgavrila committed Dec 13, 2024
2 parents 070d6bf + 6d6cfc5 commit cd32bcc
Show file tree
Hide file tree
Showing 11 changed files with 1,464 additions and 1,374 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ node_modules
.cache
*.log
.DS_Store
**/.DS_Store

# builds
build
Expand All @@ -18,7 +19,6 @@ out
.husky

# misc
.DS_Store
.env
.env.local
.env.development.local
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,29 @@ In your project, make sure to use the `preserveSymlinks` option in the server co
```

// TODO: DEMONSTRATE API


```mermaid
flowchart TB;
id1{index.tsx}
-- sdk-dpp-core config -->
F(
persistance
network
custom providers
)
-- await config -->
id2{/unlock}
-- user presses login -->
Provider.login
-- redirect -->
/dashboard
```
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"immer": "10.x"
},
"optionalDependencies": {
"@multiversx/sdk-dapp-core-ui": "file:../mx-sdk-dapp-core-ui"
"@multiversx/sdk-dapp-core-ui": "0.0.0"
},
"resolutions": {
"string-width": "4.1.0"
Expand Down Expand Up @@ -91,7 +91,6 @@
"glob": "10.3.14",
"immer": "^10.1.1",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"msw": "1.3.1",
"node-stdlib-browser": "1.2.0",
"prettier": "3.2.5",
Expand Down
31 changes: 8 additions & 23 deletions src/core/providers/ProviderFactory.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { IframeLoginTypes } from '@multiversx/sdk-web-wallet-iframe-provider/out/constants';
import { SECOND_LOGIN_ATTEMPT_ERROR } from 'constants/errorMessages.constants';
import { getAddress } from 'core/methods/account/getAddress';
import { getIsLoggedIn } from 'core/methods/account/getIsLoggedIn';
import { CrossWindowProviderStrategy } from 'core/providers-strategy/CrossWindowProviderStrategy';
import { ExtensionProviderStrategy } from 'core/providers-strategy/ExtensionProviderStrategy';
import { IFrameProviderStrategy } from 'core/providers-strategy/IFrameProviderStrategy';
import { WalletConnectProviderStrategy } from 'core/providers-strategy/WalletConnectProviderStrategy';
import { setProviderType } from 'store/actions/loginInfo/loginInfoActions';
import { setAccountProvider } from './accountProvider';
import { DappProvider } from './DappProvider/DappProvider';
import { getConfig } from './helpers/getConfig';
import { createLedgerProvider } from './helpers/ledger/createLedgerProvider';
import {
ICustomProvider,
Expand All @@ -27,11 +24,10 @@ export class ProviderFactory {

public static async create({
type,
config: userConfig
config
}: IProviderFactory): Promise<DappProvider> {
let createdProvider: IProvider | null = null;
const config = await getConfig(userConfig);
const { account, UI, walletConnect } = config;
const address = getAddress();

switch (type) {
case ProviderTypeEnum.extension: {
Expand All @@ -42,17 +38,15 @@ export class ProviderFactory {
}

case ProviderTypeEnum.crossWindow: {
const providerInstance = new CrossWindowProviderStrategy(
account?.address
);
const providerInstance = new CrossWindowProviderStrategy(address);

createdProvider = await providerInstance.createProvider();

break;
}

case ProviderTypeEnum.ledger: {
const ledgerProvider = await createLedgerProvider(UI.ledger.mount);
const ledgerProvider = await createLedgerProvider();

if (!ledgerProvider) {
throw new Error('Unable to create ledger provider');
Expand All @@ -62,13 +56,6 @@ export class ProviderFactory {

createdProvider.getType = () => ProviderTypeEnum.ledger;

const loggedIn = getIsLoggedIn();

if (loggedIn) {
console.warn('Already logged in with:', getAddress());
throw new Error(SECOND_LOGIN_ATTEMPT_ERROR);
}

await createdProvider.init?.();

break;
Expand All @@ -77,7 +64,7 @@ export class ProviderFactory {
case ProviderTypeEnum.metamask: {
const providerInstance = new IFrameProviderStrategy({
type: IframeLoginTypes.metamask,
address: account?.address
address
});

createdProvider = await providerInstance.createProvider();
Expand All @@ -88,7 +75,7 @@ export class ProviderFactory {
case ProviderTypeEnum.passkey: {
const providerInstance = new IFrameProviderStrategy({
type: IframeLoginTypes.passkey,
address: account?.address
address
});

createdProvider = await providerInstance.createProvider();
Expand All @@ -97,7 +84,7 @@ export class ProviderFactory {
}
case ProviderTypeEnum.walletConnect: {
const providerInstance = new WalletConnectProviderStrategy(
walletConnect
config.walletConnect
);

createdProvider = await providerInstance.createProvider();
Expand All @@ -108,9 +95,7 @@ export class ProviderFactory {
default: {
for (const customProvider of this._customProviders) {
if (customProvider.type === type) {
createdProvider = await customProvider.constructor(
config.account?.address
);
createdProvider = await customProvider.constructor(address);
createdProvider.getType = () => type;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
ISignTransactionsModalData,
SignEventsEnum
} from './signTransactionsModal.types';

export interface IEventBus {
publish(event: string, data: any): void;
}

const notInitializedError = () => new Error('Event bus not initialized');

export class SignTransactionsStateManager<T extends IEventBus = IEventBus> {
private static instance: SignTransactionsStateManager<IEventBus> | null =
null;
public readonly addressesPerPage = 10;

private eventBus: T = {
publish: notInitializedError,
subscribe: notInitializedError,
unsubscribe: notInitializedError
} as unknown as T;

// whole data to be sent on update events
private initialData: ISignTransactionsModalData = {
transaction: null
};

private data: ISignTransactionsModalData = { ...this.initialData };

private constructor(eventBus: T) {
this.eventBus = eventBus;
this.resetData();
}

public static getInstance<U extends IEventBus>(
eventBus?: U
): SignTransactionsStateManager<U> | null {
if (!eventBus) {
return null;
}
if (!SignTransactionsStateManager.instance) {
SignTransactionsStateManager.instance = new SignTransactionsStateManager(
eventBus
);
}
return SignTransactionsStateManager.instance as SignTransactionsStateManager<U>;
}

public updateTransaction(members: Partial<ISignTransactionsModalData>): void {
this.data = {
...this.data,
...members
};
this.notifyDataUpdate();
}

private resetData(): void {
this.data = { ...this.initialData };
}

public closeAndReset(): void {
this.data.shouldClose = true;
this.notifyDataUpdate();
this.resetData();
}

private notifyDataUpdate(): void {
this.eventBus.publish(SignEventsEnum.DATA_UPDATE, this.data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface ITransactionData {
receiver?: string;
value?: string;
}

export interface ISignTransactionsModalData {
transaction: ITransactionData | null;
shouldClose?: true;
}

export enum SignEventsEnum {
'SIGN_TRANSACTION' = 'SIGN_TRANSACTION',
'NEXT_PAGE' = 'NEXT_PAGE',
'PREV_PAGE' = 'PREV_PAGE',
'CLOSE' = 'CLOSE',
'DATA_UPDATE' = 'DATA_UPDATE'
}
68 changes: 0 additions & 68 deletions src/core/providers/helpers/getConfig.ts

This file was deleted.

Loading

0 comments on commit cd32bcc

Please sign in to comment.