From 7be55cc7172038247acb5eb9c90f8ae1b1f620fc Mon Sep 17 00:00:00 2001 From: Wil Macedo Date: Fri, 16 Feb 2024 11:46:12 -0300 Subject: [PATCH] bug: :fix: fix circular dependencies --- src/ada/connect.ts | 2 +- src/ada/index.ts | 2 +- src/substrate/connect.spec.ts | 5 +++-- src/substrate/connect.ts | 3 ++- src/types.ts | 2 ++ src/web3-provider.ts | 4 +--- 6 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/ada/connect.ts b/src/ada/connect.ts index 8cea31d..de58df6 100644 --- a/src/ada/connect.ts +++ b/src/ada/connect.ts @@ -1,7 +1,7 @@ import { NotInjectedError } from '@/errors'; import { NoAvailableAccountsError } from '@/errors/no-accounts-available-error'; import { NoProviderAvailableError } from '@/errors/no-provider-available-error'; -import { web3Window } from '@/web3-provider'; +import { web3Window } from '@/types'; import { availableWallets } from './available-wallets'; import type { CardanoUsedAddress } from './types'; diff --git a/src/ada/index.ts b/src/ada/index.ts index e219e60..ca37939 100644 --- a/src/ada/index.ts +++ b/src/ada/index.ts @@ -1,7 +1,7 @@ -import { connect } from '@/ada/connect'; import type { ProviderEntity } from '@/entities/provider-entity'; import type { Account, Address, Balance } from '@/types'; import { CardanoWallet } from './available-wallets'; +import { connect } from './connect'; import type { CardanoProviderProps } from './types'; export class CardanoProvider implements ProviderEntity { diff --git a/src/substrate/connect.spec.ts b/src/substrate/connect.spec.ts index 8194982..3753a89 100644 --- a/src/substrate/connect.spec.ts +++ b/src/substrate/connect.spec.ts @@ -1,6 +1,7 @@ import { NotInjectedError } from '@/errors'; import { NoAvailableAccountsError } from '@/errors/no-accounts-available-error'; import { NoProviderAvailableError } from '@/errors/no-provider-available-error'; +import { web3Window } from '@/types'; import * as extension from '@polkadot/extension-dapp'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { connect } from './connect'; @@ -17,11 +18,11 @@ describe('Connect wallet use case', () => { vi.resetAllMocks() appName = 'Web3 Hub'; - (window as any).injectedWeb3 = {} + web3Window.injectedWeb3 = {} }) it('should be able to throw error when window dont have web3 object', async () => { - delete (window as any).injectedWeb3 + delete web3Window.injectedWeb3 await expect(connect(appName)).rejects.toThrow(NotInjectedError) }) diff --git a/src/substrate/connect.ts b/src/substrate/connect.ts index 9103197..660d749 100644 --- a/src/substrate/connect.ts +++ b/src/substrate/connect.ts @@ -1,11 +1,12 @@ import { NotInjectedError } from '@/errors'; import { NoAvailableAccountsError } from '@/errors/no-accounts-available-error'; import { NoProviderAvailableError } from '@/errors/no-provider-available-error'; +import { web3Window } from '@/types'; import { web3Accounts, web3Enable } from '@polkadot/extension-dapp'; import type { SubstrateAccountWithMeta } from './types'; export async function connect(appName: string): Promise { - if (!(window as any).injectedWeb3) + if (!web3Window.injectedWeb3) throw new NotInjectedError() const isInjected = await web3Enable(appName) diff --git a/src/types.ts b/src/types.ts index cc7f370..f612473 100644 --- a/src/types.ts +++ b/src/types.ts @@ -20,3 +20,5 @@ export type Web3Window = { injectedWeb3: any cardano: any } & Window & typeof globalThis + +export const web3Window = (window as Web3Window) diff --git a/src/web3-provider.ts b/src/web3-provider.ts index 167e919..7ea3a8a 100644 --- a/src/web3-provider.ts +++ b/src/web3-provider.ts @@ -7,7 +7,7 @@ import { getNetworkKeyById, isValidNetwork } from './networks'; import { PolkadotProvider } from './substrate/dot'; import { KusamaProvider } from './substrate/ksm'; import type { SubstrateProviderProps } from './substrate/types'; -import type { ProviderBuilderProps, Web3Window } from './types'; +import type { ProviderBuilderProps } from './types'; export class Web3Provider { network: NetworkKey @@ -38,5 +38,3 @@ export class Web3Provider { } } } - -export const web3Window = (window as Web3Window)