Skip to content

Commit

Permalink
refactor: resolve conflicts, use safe local storage on all adapters a…
Browse files Browse the repository at this point in the history
…nd base
  • Loading branch information
enesozturk committed Sep 4, 2024
1 parent 3738207 commit 64c833a
Show file tree
Hide file tree
Showing 25 changed files with 15,667 additions and 11,921 deletions.
22 changes: 12 additions & 10 deletions packages/adapters/ethers/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { AppKitOptions } from '@web3modal/base'
import {
NetworkUtil,
SafeLocalStorage,
SafeLocalStorageKeys,
type AdapterType,
type CaipAddress,
type CaipNetwork,
Expand Down Expand Up @@ -335,7 +337,7 @@ export class EVMEthersClient {
}

// Common cleanup actions
localStorage.removeItem(WcConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID)
this.appKit?.resetAccount('eip155')
},
signMessage: async (message: string) => {
Expand Down Expand Up @@ -502,7 +504,7 @@ export class EVMEthersClient {
}

private getApprovedCaipNetworksData() {
const walletId = localStorage.getItem(WcConstantsUtil.WALLET_ID)
const walletId = SafeLocalStorage.getItem(SafeLocalStorageKeys.WALLET_ID)

if (!walletId) {
return {
Expand Down Expand Up @@ -533,8 +535,8 @@ export class EVMEthersClient {
}

private checkActiveProviders(config: ProviderType) {
const walletId = localStorage.getItem(WcConstantsUtil.WALLET_ID)
const walletName = localStorage.getItem(WcConstantsUtil.WALLET_NAME)
const walletId = SafeLocalStorage.getItem(SafeLocalStorageKeys.WALLET_ID)
const walletName = SafeLocalStorage.getItem(SafeLocalStorageKeys.WALLET_NAME)

if (!walletId) {
return
Expand Down Expand Up @@ -566,9 +568,9 @@ export class EVMEthersClient {
} else {
const walletId = providerId

window?.localStorage.setItem(WcConstantsUtil.WALLET_ID, walletId)
SafeLocalStorage.setItem(SafeLocalStorageKeys.WALLET_ID, walletId)
if (name) {
window?.localStorage.setItem(WcConstantsUtil.WALLET_NAME, name)
SafeLocalStorage.setItem(SafeLocalStorageKeys.WALLET_NAME, name)
}

if (provider) {
Expand All @@ -595,7 +597,7 @@ export class EVMEthersClient {
}

private async setAuthProvider() {
window?.localStorage.setItem(WcConstantsUtil.WALLET_ID, ConstantsUtil.AUTH_CONNECTOR_ID)
SafeLocalStorage.setItem(SafeLocalStorageKeys.WALLET_ID, ConstantsUtil.AUTH_CONNECTOR_ID)

if (this.authProvider) {
this.appKit?.setLoading(true)
Expand Down Expand Up @@ -657,7 +659,7 @@ export class EVMEthersClient {

private setupProviderListeners(provider: Provider, providerId: ProviderId) {
const disconnectHandler = () => {
localStorage.removeItem(WcConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID)
this.removeListeners(provider)
}

Expand All @@ -676,7 +678,7 @@ export class EVMEthersClient {
if (providerId === ConstantsUtil.EIP6963_CONNECTOR_ID) {
this.appKit?.setAllAccounts([], this.chainNamespace)
}
localStorage.removeItem(WcConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID)
this.appKit?.resetAccount('eip155')
}
}
Expand Down Expand Up @@ -920,7 +922,7 @@ export class EVMEthersClient {
}

private syncConnectedWalletInfo() {
const currentActiveWallet = window?.localStorage.getItem(WcConstantsUtil.WALLET_ID)
const currentActiveWallet = SafeLocalStorage.getItem(SafeLocalStorageKeys.WALLET_ID)
const providerType = ProviderUtil.state.providerIds['eip155']

if (providerType === ConstantsUtil.EIP6963_CONNECTOR_ID) {
Expand Down
22 changes: 12 additions & 10 deletions packages/adapters/ethers5/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { AppKitOptions } from '@web3modal/base'
import {
NetworkUtil,
SafeLocalStorage,
SafeLocalStorageKeys,
type AdapterType,
type CaipAddress,
type CaipNetwork,
Expand Down Expand Up @@ -335,7 +337,7 @@ export class EVMEthers5Client {
}

// Common cleanup actions
localStorage.removeItem(WcConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID)
this.appKit?.resetAccount('eip155')
},
signMessage: async (message: string) => {
Expand Down Expand Up @@ -511,7 +513,7 @@ export class EVMEthers5Client {
}

private getApprovedCaipNetworksData() {
const walletId = localStorage.getItem(WcConstantsUtil.WALLET_ID)
const walletId = SafeLocalStorage.getItem(SafeLocalStorageKeys.WALLET_ID)

if (!walletId) {
return {
Expand Down Expand Up @@ -542,8 +544,8 @@ export class EVMEthers5Client {
}

private checkActiveProviders(config: ProviderType) {
const walletId = localStorage.getItem(WcConstantsUtil.WALLET_ID)
const walletName = localStorage.getItem(WcConstantsUtil.WALLET_NAME)
const walletId = SafeLocalStorage.getItem(SafeLocalStorageKeys.WALLET_ID)
const walletName = SafeLocalStorage.getItem(SafeLocalStorageKeys.WALLET_NAME)

if (!walletId) {
return
Expand Down Expand Up @@ -575,9 +577,9 @@ export class EVMEthers5Client {
} else {
const walletId = providerId

window?.localStorage.setItem(WcConstantsUtil.WALLET_ID, walletId)
SafeLocalStorage.setItem(SafeLocalStorageKeys.WALLET_ID, walletId)
if (name) {
window?.localStorage.setItem(WcConstantsUtil.WALLET_NAME, name)
SafeLocalStorage.setItem(SafeLocalStorageKeys.WALLET_NAME, name)
}

if (provider) {
Expand All @@ -604,7 +606,7 @@ export class EVMEthers5Client {
}

private async setAuthProvider() {
window?.localStorage.setItem(WcConstantsUtil.WALLET_ID, ConstantsUtil.AUTH_CONNECTOR_ID)
SafeLocalStorage.setItem(SafeLocalStorageKeys.WALLET_ID, ConstantsUtil.AUTH_CONNECTOR_ID)

if (this.authProvider) {
this.appKit?.setLoading(true)
Expand Down Expand Up @@ -666,7 +668,7 @@ export class EVMEthers5Client {

private setupProviderListeners(provider: Provider, providerId: ProviderId) {
const disconnectHandler = () => {
localStorage.removeItem(WcConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID)
this.removeListeners(provider)
}

Expand All @@ -685,7 +687,7 @@ export class EVMEthers5Client {
if (providerId === ConstantsUtil.EIP6963_CONNECTOR_ID) {
this.appKit?.setAllAccounts([], this.chainNamespace)
}
localStorage.removeItem(WcConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID)
this.appKit?.resetAccount('eip155')
}
}
Expand Down Expand Up @@ -929,7 +931,7 @@ export class EVMEthers5Client {
}

private syncConnectedWalletInfo() {
const currentActiveWallet = window?.localStorage.getItem(WcConstantsUtil.WALLET_ID)
const currentActiveWallet = SafeLocalStorage.getItem(SafeLocalStorageKeys.WALLET_ID)
const providerType = ProviderUtil.state.providerIds['eip155']

if (providerType === ConstantsUtil.EIP6963_CONNECTOR_ID) {
Expand Down
65 changes: 36 additions & 29 deletions packages/adapters/solana/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
NetworkController,
OptionsController
} from '@web3modal/core'
import { ConstantsUtil as CommonConstantsUtil } from '@web3modal/common'
import {
ConstantsUtil as CommonConstantsUtil,
SafeLocalStorage,
SafeLocalStorageKeys
} from '@web3modal/common'

import { SolConstantsUtil } from './utils/SolanaConstantsUtil.js'
import { SolHelpersUtil } from './utils/SolanaHelpersUtils.js'
Expand All @@ -17,7 +21,12 @@ import { SolStoreUtil } from './utils/SolanaStoreUtil.js'
import type { BaseWalletAdapter } from '@solana/wallet-adapter-base'
import { PublicKey, type Commitment, type ConnectionConfig } from '@solana/web3.js'
import UniversalProvider, { type UniversalProviderOpts } from '@walletconnect/universal-provider'
import type { ConnectionControllerClient, NetworkControllerClient } from '@web3modal/core'
import type {
ChainAdapter,
ConnectionControllerClient,
NetworkControllerClient,
Connector
} from '@web3modal/core'
import type { AdapterType, CaipAddress, CaipNetwork, CaipNetworkId } from '@web3modal/common'
import type { ChainNamespace } from '@web3modal/common'

Expand All @@ -32,11 +41,10 @@ import {
type W3mFrameTypes
} from '@web3modal/wallet'
import { withSolanaNamespace } from './utils/withSolanaNamespace.js'
import { SafeLocalStorage } from '@web3modal/base'
import type { AppKit } from '@web3modal/base'
import type { AppKitOptions } from '@web3modal/base'
import { ProviderUtil } from '@web3modal/base/store'
import { WcConstantsUtil } from '@web3modal/base/utils'

export interface AdapterOptions {
connectionSettings?: Commitment | ConnectionConfig
defaultCaipNetwork?: CaipNetwork
Expand All @@ -46,7 +54,7 @@ export interface AdapterOptions {
export type Web3ModalOptions = Omit<AdapterOptions, '_sdkVersion' | 'isUniversalProvider'>

// -- Client --------------------------------------------------------------------
export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipNetwork> {
export class SolanaWeb3JsClient implements ChainAdapter {
private appKit: AppKit | undefined = undefined

public options: AppKitOptions | undefined = undefined
Expand Down Expand Up @@ -97,18 +105,13 @@ export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipN
throw new Error('Solana:construct - projectId is undefined')
}

this.defaultChain = defaultChain
? SolHelpersUtil.getChainFromCaip(
this.chains,
SafeLocalStorage.getItem(SolConstantsUtil.CAIP_CHAIN_ID) || defaultChain.chainId
)
: undefined
this.defaultSolanaChain = this.chains.find(c => c.chainId === defaultChain?.chainId)

this.networkControllerClient = {
switchCaipNetwork: async caipNetwork => {
if (caipNetwork) {
localStorage.setItem(WcConstantsUtil.ACTIVE_CAIPNETWORK, JSON.stringify(caipNetwork))
SafeLocalStorage.setItem(
SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK,
JSON.stringify(caipNetwork)
)
try {
await this.switchNetwork(caipNetwork)
} catch (error) {
Expand Down Expand Up @@ -159,7 +162,7 @@ export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipN
disconnect: async () => {
await ProviderUtil.getProvider<Provider>('solana')?.disconnect()

this.appKit?.resetAccount('solana')
this.appKit?.resetAccount(this.chainNamespace)
},

signMessage: async (message: string) => {
Expand Down Expand Up @@ -203,7 +206,7 @@ export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipN
ProviderUtil.subscribeProviders(providers => {
if (providers['solana'] && providers['solana'] instanceof UniversalProvider) {
const walletConnectProvider = this.getSolanaWalletConnectProvider(providers['solana'])
ProviderUtil.setProvider('solana', walletConnectProvider)
ProviderUtil.setProvider(this.chainNamespace, walletConnectProvider)
}
})

Expand All @@ -217,14 +220,15 @@ export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipN

const caipNetwork = SolHelpersUtil.getChainFromCaip(
caipNetworks,
typeof window === 'object' ? localStorage.getItem(SolConstantsUtil.CAIP_CHAIN_ID) : ''
typeof window === 'object'
? SafeLocalStorage.getItem(SafeLocalStorageKeys.SOLANA_CAIP_CHAIN)
: ''
)

this.defaultCaipNetwork = caipNetwork
this.syncRequestedNetworks(caipNetworks)

this.syncNetwork()
this.syncRequestedNetworks(chains, this.options?.chainImages)
this.syncRequestedNetworks(caipNetworks)

AssetController.subscribeNetworkImages(() => {
this.syncNetwork()
Expand All @@ -240,7 +244,10 @@ export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipN
}

if (NetworkController.state.caipNetwork && this.appKit?.getIsConnectedState()) {
localStorage.setItem(SolConstantsUtil.CAIP_CHAIN_ID, `solana:${newChain.chainId}`)
SafeLocalStorage.setItem(
SafeLocalStorageKeys.SOLANA_CAIP_CHAIN,
`solana:${newChain.chainId}`
)
ApiController.reFetchWallets()
}
})
Expand Down Expand Up @@ -280,7 +287,7 @@ export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipN
} else if (this.hasSyncedConnectedAccount) {
this.appKit?.resetWcConnection()
this.appKit?.resetNetwork()
this.appKit?.resetAccount('solana')
this.appKit?.resetAccount(this.chainNamespace)
}
}

Expand Down Expand Up @@ -323,7 +330,7 @@ export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipN

this.appKit?.setCaipNetwork(caipNetwork)

localStorage.setItem(SolConstantsUtil.CAIP_CHAIN_ID, caipNetwork.id)
SafeLocalStorage.setItem(SafeLocalStorageKeys.SOLANA_CAIP_CHAIN, caipNetwork.id)

await this.syncNetwork()
await this.syncAccount()
Expand Down Expand Up @@ -360,7 +367,7 @@ export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipN
try {
this.appKit?.setLoading(true)
const address = await provider.connect()
const caipChainId = localStorage.getItem('@w3m/solana_caip_chain')
const caipChainId = SafeLocalStorage.getItem(SafeLocalStorageKeys.SOLANA_CAIP_CHAIN)
let connectionChain: CaipNetwork | undefined = undefined

const activeCaipNetwork = this.appKit?.getCaipNetwork()
Expand All @@ -380,11 +387,11 @@ export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipN

await this.switchNetwork(connectionChain)

ProviderUtil.setProvider('solana', provider)
ProviderUtil.setProviderId('solana', 'walletConnect')
ProviderUtil.setProvider(this.chainNamespace, provider)
ProviderUtil.setProviderId(this.chainNamespace, 'walletConnect')
this.provider = provider

window?.localStorage.setItem(SolConstantsUtil.WALLET_ID, provider.name)
SafeLocalStorage.setItem(SafeLocalStorageKeys.WALLET_ID, provider.name)

await this.appKit?.setApprovedCaipNetworksData(this.chainNamespace)

Expand Down Expand Up @@ -466,7 +473,7 @@ export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipN

function disconnectHandler(appKit?: AppKit) {
appKit?.resetAccount('solana')
localStorage.removeItem(SolConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID)

provider.removeListener('disconnect', disconnectHandler)
provider.removeListener('accountsChanged', accountsChangedHandler)
Expand All @@ -482,7 +489,7 @@ export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipN
if (currentAccount && caipNetwork) {
appKit?.setCaipAddress(`solana:${caipNetwork.chainId}:${currentAccount}`, 'solana')
} else {
localStorage.removeItem(SolConstantsUtil.WALLET_ID)
SafeLocalStorage.removeItem(SafeLocalStorageKeys.WALLET_ID)
appKit?.resetAccount('solana')
}
}
Expand Down Expand Up @@ -533,7 +540,7 @@ export class SolanaWeb3JsClient implements ChainAdapter<SolStoreUtilState, CaipN
}

private addProvider(...providers: Provider[]) {
const activeProviderName = SafeLocalStorage.getItem(SolConstantsUtil.WALLET_ID)
const activeProviderName = SafeLocalStorage.getItem(SafeLocalStorageKeys.WALLET_ID)

for (const provider of providers) {
this.availableProviders = this.availableProviders.filter(p => p.name !== provider.name)
Expand Down
Loading

0 comments on commit 64c833a

Please sign in to comment.