Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
73 changes: 27 additions & 46 deletions packages/plugin-infra/src/web3-state/Provider.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -13,10 +13,8 @@ import type { Account, WalletProvider, ProviderState as Web3ProviderState } from
import type { Plugin } from '../types'

export interface ProviderStorage<Account, ProviderType extends string> {
/** Providers map to account settings. */
accounts: Record<ProviderType, Account>
/** Sites map to provider type. */
providers: Record<EnhanceableSite | ExtensionSite, ProviderType>
account: Account
providerType: ProviderType
}

export class ProviderState<
Expand Down Expand Up @@ -48,24 +46,16 @@ export class ProviderState<
getNetworkTypeFromChainId(chainId: ChainId): NetworkType
},
) {
const defaultValue: ProviderStorage<Account<ChainId>, ProviderType> = {
accounts: Object.fromEntries(
Object.keys(providers).map((x) => [
x,
{
account: '',
chainId: options.getDefaultChainId(),
},
]),
) as Record<ProviderType, Account<ChainId>>,
providers: Object.fromEntries(
[...getEnumAsArray(EnhanceableSite), ...getEnumAsArray(ExtensionSite)].map((x) => [
x.value,
options.getDefaultProviderType(x.value),
]),
) as Record<EnhanceableSite | ExtensionSite, ProviderType>,
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()
Expand All @@ -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),
)
}

Expand Down Expand Up @@ -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())
})
})
}
Expand All @@ -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))
Expand All @@ -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,
})
}
}
Expand All @@ -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)
}
}

Expand Down
23 changes: 10 additions & 13 deletions packages/plugins/EVM/src/state/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,29 @@ export class Provider extends ProviderState<ChainId, ProviderType, NetworkType,
}

override setupSubscriptions() {
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, 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)
},
)
}
Expand Down