Skip to content

Commit

Permalink
bug: :fix: fix circular dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmacedo committed Feb 16, 2024
1 parent 9a1b626 commit 7be55cc
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ada/connect.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
2 changes: 1 addition & 1 deletion src/ada/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/substrate/connect.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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)
})
Expand Down
3 changes: 2 additions & 1 deletion src/substrate/connect.ts
Original file line number Diff line number Diff line change
@@ -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<SubstrateAccountWithMeta[]> {
if (!(window as any).injectedWeb3)
if (!web3Window.injectedWeb3)
throw new NotInjectedError()

const isInjected = await web3Enable(appName)
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ export type Web3Window = {
injectedWeb3: any
cardano: any
} & Window & typeof globalThis

export const web3Window = (window as Web3Window)
4 changes: 1 addition & 3 deletions src/web3-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,5 +38,3 @@ export class Web3Provider {
}
}
}

export const web3Window = (window as Web3Window)

0 comments on commit 7be55cc

Please sign in to comment.