Skip to content

Commit

Permalink
Extract storage creation
Browse files Browse the repository at this point in the history
  • Loading branch information
yivlad committed Jun 13, 2024
1 parent 702713f commit 2455792
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
37 changes: 3 additions & 34 deletions packages/app/src/config/wagmi/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getDefaultConfig } from '@rainbow-me/rainbowkit'
import { http, Chain, Transport } from 'viem'
import { gnosis, mainnet } from 'viem/chains'
import { Config, createStorage, noopStorage } from 'wagmi'
import { Config } from 'wagmi'

import { SandboxNetwork } from '@/domain/state/sandbox'
import { raise } from '@/utils/assert'
Expand All @@ -10,6 +10,7 @@ import { SUPPORTED_CHAINS } from '../chain/constants'
import { SupportedChainId } from '../chain/types'
import { VIEM_TIMEOUT_ON_FORKS } from './config.e2e'
import { getWallets } from './getWallets'
import { createWagmiStorage } from './storage'

const wallets = getWallets()

Expand All @@ -23,39 +24,7 @@ export function getConfig(sandboxNetwork?: SandboxNetwork): Config {
[gnosis.id]: http('https://rpc.ankr.com/gnosis'),
}

const defaultStorage = createStorage({
storage: typeof window !== 'undefined' && window.localStorage ? window.localStorage : noopStorage,
})
const storage: typeof defaultStorage = {
...defaultStorage,
getItem: async (key) => {
const originalValue = (await defaultStorage.getItem(key)) as any

if ((key as any) === 'store' && typeof originalValue === 'object') {
const persistedChainId = originalValue?.state?.chainId
const connections: Map<string, { chainId: number }> = originalValue?.state?.connections || new Map()

const filteredConnections = new Map(
[...connections.entries()].filter(([_, { chainId }]) => {
return SUPPORTED_CHAINS.some((chain) => chain.id === chainId)
}),
)
const newChainId = SUPPORTED_CHAINS.some((chain) => chain.id === persistedChainId)
? persistedChainId
: mainnet.id

return {
...originalValue,
state: {
...originalValue?.state,
connections: filteredConnections,
chainId: newChainId,
},
} as any
}
return originalValue
},
}
const storage = createWagmiStorage()

const config = getDefaultConfig({
appName: 'Spark',
Expand Down
43 changes: 43 additions & 0 deletions packages/app/src/config/wagmi/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { mainnet } from 'viem/chains'
import { createStorage, noopStorage } from 'wagmi'
import { SUPPORTED_CHAINS } from '../chain/constants'

export function createWagmiStorage(): ReturnType<typeof createStorage> {
const defaultStorage = createStorage({
storage: typeof window !== 'undefined' && window.localStorage ? window.localStorage : noopStorage,
})
const storage: typeof defaultStorage = {
...defaultStorage,
getItem: wrapGetItem(defaultStorage),
}

return storage
}

function wrapGetItem(defaultStorage: ReturnType<typeof createStorage>): any {
return async function getItem(key: 'recentConnectorId' | 'store'): Promise<any> {
const originalValue = (await defaultStorage.getItem(key as any)) as any

if ((key as any) === 'store' && typeof originalValue === 'object') {
const persistedChainId = originalValue?.state?.chainId
const connections: Map<string, { chainId: number }> = originalValue?.state?.connections || new Map()

const filteredConnections = new Map(
[...connections.entries()].filter(([_, { chainId }]) => {
return SUPPORTED_CHAINS.some((chain) => chain.id === chainId)
}),
)
const newChainId = SUPPORTED_CHAINS.some((chain) => chain.id === persistedChainId) ? persistedChainId : mainnet.id

return {
...originalValue,
state: {
...originalValue?.state,
connections: filteredConnections,
chainId: newChainId,
},
} as any
}
return originalValue
}
}

0 comments on commit 2455792

Please sign in to comment.