From 13a60999129e5e6c58e1669636018c1bd3e3bb0c Mon Sep 17 00:00:00 2001 From: nuanyang233 Date: Tue, 14 Jun 2022 20:33:56 +0800 Subject: [PATCH 1/2] refactor: provider storage --- .../plugin-infra/src/web3-state/Provider.ts | 73 +++++++------------ packages/plugins/EVM/src/state/Provider.ts | 23 +++--- 2 files changed, 37 insertions(+), 59 deletions(-) diff --git a/packages/plugin-infra/src/web3-state/Provider.ts b/packages/plugin-infra/src/web3-state/Provider.ts index fc9c1e5464b3..ae34590eaa5c 100644 --- a/packages/plugin-infra/src/web3-state/Provider.ts +++ b/packages/plugin-infra/src/web3-state/Provider.ts @@ -1,6 +1,6 @@ import { clone, first } from 'lodash-unified' import type { Subscription } from 'use-subscription' -import { delay, getEnumAsArray } from '@dimensiondev/kit' +import { delay } from '@dimensiondev/kit' import { EnhanceableSite, ExtensionSite, @@ -13,10 +13,8 @@ import type { Account, WalletProvider, ProviderState as Web3ProviderState } from import type { Plugin } from '../types' export interface ProviderStorage { - /** Providers map to account settings. */ - accounts: Record - /** Sites map to provider type. */ - providers: Record + account: Account + providerType: ProviderType } export class ProviderState< @@ -48,24 +46,16 @@ export class ProviderState< getNetworkTypeFromChainId(chainId: ChainId): NetworkType }, ) { - const defaultValue: ProviderStorage, ProviderType> = { - accounts: Object.fromEntries( - Object.keys(providers).map((x) => [ - x, - { - account: '', - chainId: options.getDefaultChainId(), - }, - ]), - ) as Record>, - providers: Object.fromEntries( - [...getEnumAsArray(EnhanceableSite), ...getEnumAsArray(ExtensionSite)].map((x) => [ - x.value, - options.getDefaultProviderType(x.value), - ]), - ) as Record, + const site = getSiteType() + const defaultValue = { + account: { + account: '', + chainId: options.getDefaultChainId(), + }, + providerType: options.getDefaultProviderType(site), } - const { storage } = this.context.createKVStorage('memory', {}).createSubScope('Provider', defaultValue) + + const { storage } = this.context.createKVStorage('memory', {}).createSubScope(site ?? 'Provider', defaultValue) this.storage = storage this.setupSubscriptions() @@ -76,19 +66,18 @@ export class ProviderState< const site = this.site if (!site) return - this.providerType = mapSubscription(this.storage.providers.subscription, (providers) => providers[site]) + this.providerType = mapSubscription(this.storage.providerType.subscription, (provider) => provider) this.chainId = mapSubscription( - mergeSubscription(this.providerType, this.storage.accounts.subscription), - ([providerType, accounts]) => accounts[providerType].chainId, + mergeSubscription(this.storage.account.subscription), + ([account]) => account.chainId, ) this.account = mapSubscription( - mergeSubscription(this.providerType, this.storage.accounts.subscription), - ([providerType, accounts]) => accounts[providerType].account, + mergeSubscription(this.storage.account.subscription), + ([account]) => account.account, ) - this.networkType = mapSubscription( - mergeSubscription(this.providerType, this.storage.accounts.subscription), - ([providerType, accounts]) => this.options.getNetworkTypeFromChainId(accounts[providerType].chainId), + this.networkType = mapSubscription(mergeSubscription(this.storage.account.subscription), ([account]) => + this.options.getNetworkTypeFromChainId(account.chainId), ) } @@ -120,10 +109,7 @@ export class ProviderState< const siteType = getSiteType() if (!siteType) return - this.storage.providers.setValue({ - ...this.storage.providers.value, - [siteType]: this.options.getDefaultProviderType(), - }) + this.storage.providerType.setValue(this.options.getDefaultProviderType()) }) }) } @@ -132,7 +118,7 @@ export class ProviderState< const siteType = getSiteType() if (!siteType) return - const account_ = this.storage.accounts.value[providerType] + const account_ = this.storage.account.value const accountCopied = clone(account) if (accountCopied.account !== '' && !this.options.isValidAddress(accountCopied.account)) @@ -144,12 +130,10 @@ export class ProviderState< const needToUpdateChainId = accountCopied.chainId && account_.chainId !== accountCopied.chainId if (needToUpdateAccount || needToUpdateChainId) { - await this.storage.accounts.setValue({ - ...this.storage.accounts.value, - [providerType]: { - ...account_, - ...accountCopied, - }, + await this.storage.providerType.setValue(providerType) + await this.storage.account.setValue({ + ...account_, + ...accountCopied, }) } } @@ -158,13 +142,10 @@ export class ProviderState< const siteType = getSiteType() if (!siteType) return - const needToUpdateProviderType = this.storage.providers.value[siteType] !== providerType + const needToUpdateProviderType = this.storage.providerType.value !== providerType if (needToUpdateProviderType) { - await this.storage.providers.setValue({ - ...this.storage.providers.value, - [siteType]: providerType, - }) + await this.storage.providerType.setValue(providerType) } } diff --git a/packages/plugins/EVM/src/state/Provider.ts b/packages/plugins/EVM/src/state/Provider.ts index 6bed3a8cc4cc..a2f3b496138f 100644 --- a/packages/plugins/EVM/src/state/Provider.ts +++ b/packages/plugins/EVM/src/state/Provider.ts @@ -33,32 +33,29 @@ export class Provider extends ProviderState providers[site]) + this.providerType = mapSubscription(this.storage.providerType.subscription, (provider) => provider) this.chainId = mapSubscription( - mergeSubscription(this.providerType, this.storage.accounts.subscription, this.context.chainId), - ([providerType, accounts, chainId]) => { + mergeSubscription(this.providerType, this.storage.account.subscription, this.context.chainId), + ([providerType, account, chainId]) => { if (providerType === ProviderType.MaskWallet) return chainId - return accounts[providerType].chainId + return account.chainId }, ) this.account = mapSubscription( - mergeSubscription(this.providerType, this.storage.accounts.subscription, this.context.account), - ([providerType, accounts, maskAccount]) => { + mergeSubscription(this.providerType, this.storage.account.subscription, this.context.account), + ([providerType, account, maskAccount]) => { if (providerType === ProviderType.MaskWallet) return maskAccount - return accounts[providerType].account + return account.account }, ) this.networkType = mapSubscription( - mergeSubscription(this.providerType, this.storage.accounts.subscription, this.context.chainId), - ([providerType, accounts, chainId]) => { + mergeSubscription(this.providerType, this.storage.account.subscription, this.context.chainId), + ([providerType, account, chainId]) => { if (providerType === ProviderType.MaskWallet) return this.options.getNetworkTypeFromChainId(chainId) - return this.options.getNetworkTypeFromChainId(accounts[providerType].chainId) + return this.options.getNetworkTypeFromChainId(account.chainId) }, ) } From e750a86cb312a18b9303a8f3f371c0f7f2c0930b Mon Sep 17 00:00:00 2001 From: nuanyang233 Date: Tue, 14 Jun 2022 20:43:16 +0800 Subject: [PATCH 2/2] fix: popup route --- .../extension/popups/pages/Personas/AccountDetail/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/mask/src/extension/popups/pages/Personas/AccountDetail/index.tsx b/packages/mask/src/extension/popups/pages/Personas/AccountDetail/index.tsx index 1f8063fb87ca..27908b64be69 100644 --- a/packages/mask/src/extension/popups/pages/Personas/AccountDetail/index.tsx +++ b/packages/mask/src/extension/popups/pages/Personas/AccountDetail/index.tsx @@ -35,7 +35,7 @@ const AccountDetail = memo(() => { showSnackbar(t('popups_disconnect_success'), { variant: 'success', }) - navigate(PopupRoutes.SocialAccounts) + navigate(-1) } catch { showSnackbar(t('popups_disconnect_failed'), { variant: 'error', @@ -74,7 +74,7 @@ const AccountDetail = memo(() => { showSnackbar(t('popups_disconnect_success'), { variant: 'success', }) - navigate(PopupRoutes.SocialAccounts) + navigate(-1) } catch { showSnackbar(t('popups_disconnect_failed'), { variant: 'error',