Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(refactor): VaultAccountsProvider to w3ux #2082

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@substrate/connect": "0.7.35",
"@w3ux/extension-assets": "0.2.6",
"@w3ux/hooks": "^0.0.3",
"@w3ux/react-connect-kit": "0.1.8",
"@w3ux/react-connect-kit": "^0.1.12",
"@w3ux/react-odometer": "^0.0.3",
"@w3ux/react-polkicon": "^0.0.2",
"@w3ux/utils": "^0.0.2",
Expand Down
13 changes: 5 additions & 8 deletions src/Providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
ExtensionsProvider,
ExtensionAccountsProvider,
LedgerAccountsProvider,
VaultAccountsProvider,
} from '@w3ux/react-connect-kit';
import { FastUnstakeProvider } from 'contexts/FastUnstake';
import { FiltersProvider } from 'contexts/Filters';
import { LedgerHardwareProvider } from 'contexts/Hardware/Ledger/LedgerHardware';
import { VaultAccountsProvider } from 'contexts/Hardware/Vault/VaultAccounts';
import { LedgerHardwareProvider } from 'contexts/LedgerHardware';
import { HelpProvider } from 'contexts/Help';
import { MenuProvider } from 'contexts/Menu';
import { MigrateProvider } from 'contexts/Migrate';
Expand Down Expand Up @@ -47,26 +47,23 @@ import { OverlayProvider } from 'kits/Overlay/Provider';
import { JoinPoolsProvider } from 'contexts/Pools/JoinPools';

export const Providers = () => {
const {
network,
networkData: { ss58 },
} = useNetwork();
const { network } = useNetwork();
const { activeAccount, setActiveAccount } = useActiveAccounts();

// !! Provider order matters.
const providers: Provider[] = [
UIProvider,
[APIProvider, { network }],
VaultAccountsProvider,
LedgerHardwareProvider,
[
ExtensionsProvider,
{ options: { chainSafeSnapEnabled: true, polkagateSnapEnabled: true } },
],
[
ExtensionAccountsProvider,
{ dappName: DappName, network, ss58, activeAccount, setActiveAccount },
{ dappName: DappName, network, activeAccount, setActiveAccount },
],
[VaultAccountsProvider, { network }],
[LedgerAccountsProvider, { network }],
ExternalAccountsProvider,
OtherAccountsProvider,
Expand Down
2 changes: 1 addition & 1 deletion src/config/ledger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024 @paritytech/polkadot-staking-dashboard authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { LedgerApp } from 'contexts/Hardware/Ledger/types';
import type { LedgerApp } from 'contexts/LedgerHardware/types';
import KusamaSVG from 'img/appIcons/kusama.svg?react';
import PolkadotSVG from 'img/appIcons/polkadot.svg?react';

Expand Down
10 changes: 9 additions & 1 deletion src/contexts/Connect/ImportedAccounts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { ImportedAccountsContextInterface } from './types';
import { useOtherAccounts } from '../OtherAccounts';
import { BalancesController } from 'controllers/BalancesController';
import { useApi } from 'contexts/Api';
import { useNetwork } from 'contexts/Network';

export const ImportedAccountsContext =
createContext<ImportedAccountsContextInterface>(
Expand All @@ -30,8 +31,15 @@ export const ImportedAccountsProvider = ({
children: ReactNode;
}) => {
const { isReady, api } = useApi();
const {
networkData: { ss58 },
} = useNetwork();
const { otherAccounts } = useOtherAccounts();
const { extensionAccounts } = useExtensionAccounts();
const { getExtensionAccounts } = useExtensionAccounts();

// Get the imported extension accounts formatted with the current network's ss58 prefix.
const extensionAccounts = getExtensionAccounts(ss58);

const allAccounts = extensionAccounts.concat(otherAccounts);

// Stringify account addresses and account names to determine if they have changed. Ignore other properties including `signer` and `source`.
Expand Down
6 changes: 2 additions & 4 deletions src/contexts/Connect/OtherAccounts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

import type { ReactNode } from 'react';
import { createContext, useContext, useEffect, useRef, useState } from 'react';
import {
getLocalLedgerAccounts,
getLocalVaultAccounts,
} from 'contexts/Hardware/Utils';
import type { MaybeAddress, NetworkName } from 'types';
import { setStateWithRef } from '@w3ux/utils';
import { useNetwork } from 'contexts/Network';
Expand All @@ -22,6 +18,8 @@ import { useEventListener } from 'usehooks-ts';
import { useExtensionAccounts, useExtensions } from '@w3ux/react-connect-kit';
import type { ImportedAccount } from '@w3ux/react-connect-kit/types';
import { useEffectIgnoreInitial } from '@w3ux/hooks';
import { getLocalVaultAccounts } from '@w3ux/react-connect-kit/VaultAccountsProvider/utils';
import { getLocalLedgerAccounts } from '@w3ux/react-connect-kit/LedgerAccountsProvider/utils';

export const OtherAccountsContext =
createContext<OtherAccountsContextInterface>(defaultOtherAccountsContext);
Expand Down
168 changes: 0 additions & 168 deletions src/contexts/Hardware/Vault/VaultAccounts.tsx

This file was deleted.

14 changes: 0 additions & 14 deletions src/contexts/Hardware/Vault/defaults.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/contexts/Hardware/Vault/types.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@

import { localStorageOrDefault } from '@w3ux/utils';
import { LedgerApps } from 'config/ledger';
import type { MaybeString } from 'types';
import type {
LedgerAccount,
VaultAccount,
} from '@w3ux/react-connect-kit/types';
import type { LedgerAddress } from './Ledger/types';
import type { LedgerAddress } from './types';

// Ledger error keyed by type of error.
const LedgerErrorsByType = {
Expand Down Expand Up @@ -61,57 +56,3 @@ export const getLocalLedgerAddresses = (network?: string) => {
? localAddresses.filter((a) => a.network === network)
: localAddresses;
};

// Gets imported Ledger accounts from local storage.
export const getLocalLedgerAccounts = (network?: string): LedgerAccount[] => {
const localAddresses = localStorageOrDefault(
'ledger_accounts',
[],
true
) as LedgerAccount[];

return network
? localAddresses.filter((a) => a.network === network)
: localAddresses;
};

// Renames a record from local ledger addresses.
export const renameLocalLedgerAddress = (
address: string,
name: string,
network: string
) => {
const localLedger = (
localStorageOrDefault('ledger_addresses', [], true) as LedgerAddress[]
)?.map((i) =>
!(i.address === address && i.network === network)
? i
: {
...i,
name,
}
);
if (localLedger) {
localStorage.setItem('ledger_addresses', JSON.stringify(localLedger));
}
};

// Gets imported Vault accounts from local storage.
export const getLocalVaultAccounts = (network?: string) => {
const localAddresses = localStorageOrDefault(
'polkadot_vault_accounts',
[],
true
) as VaultAccount[];

return network
? localAddresses.filter((a) => a.network === network)
: localAddresses;
};

// Gets whether an address is a local network address.
export const isLocalNetworkAddress = (
chain: string,
a: { address: MaybeString; network: string },
address: string
) => a.address === address && a.network === chain;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { createContext, useContext, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import type { AnyJson, MaybeString } from 'types';
import { useApi } from 'contexts/Api';
import { getLedgerErrorType } from '../Utils';
import { getLedgerErrorType } from './Utils';
import { defaultFeedback, defaultLedgerHardwareContext } from './defaults';
import type {
FeedbackMessage,
Expand Down
Loading
Loading