-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathwagmi.ts
More file actions
160 lines (135 loc) · 4.81 KB
/
wagmi.ts
File metadata and controls
160 lines (135 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { createClient, type FallbackTransport, type HttpTransport, type Transport } from 'viem'
import { createConfig, createStorage, fallback, http } from 'wagmi'
import { holesky, localhost, mainnet, sepolia } from 'wagmi/chains'
import { ccipRequest } from '@ensdomains/ensjs/utils'
import {
holeskyWithEns,
localhostWithEns,
mainnetWithEns,
sepoliaWithEns,
} from '@app/constants/chains'
import { WC_PROJECT_ID } from '../constants'
import { getDefaultWallets } from '../getDefaultWallets'
import { isInsideSafe } from '../safe'
const isLocalProvider = !!process.env.NEXT_PUBLIC_PROVIDER
const connectors = getDefaultWallets({
appName: 'ENS',
projectId: WC_PROJECT_ID,
})
const infuraKey = process.env.NEXT_PUBLIC_INFURA_KEY || 'cfa6ae2501cc4354a74e20432507317c'
const tenderlyKey = process.env.NEXT_PUBLIC_TENDERLY_KEY || '4imxc4hQfRjxrVB2kWKvTo'
const drpcKey = process.env.NEXT_PUBLIC_DRPC_KEY || 'AnmpasF2C0JBqeAEzxVO8aRuvzLTrWcR75hmDonbV6cR'
export const infuraUrl = (chainName: string) => `https://${chainName}.infura.io/v3/${infuraKey}`
const tenderlyUrl = (chainName: string) => `https://${chainName}.gateway.tenderly.co/${tenderlyKey}`
const drpcUrl = (chainName: string) =>
`https://lb.drpc.org/ogrpc?network=${
chainName === 'mainnet' ? 'ethereum' : chainName
}&dkey=${drpcKey}`
type SupportedUrlFunc = typeof infuraUrl | typeof drpcUrl | typeof tenderlyUrl
const initialiseTransports = <const UrlFuncArray extends SupportedUrlFunc[]>(
chainName: string,
urlFuncArray: UrlFuncArray,
) => {
const transportArray: HttpTransport[] = []
for (const urlFunc of urlFuncArray) {
// eslint-disable-next-line no-continue
if (urlFunc === infuraUrl && process.env.NEXT_PUBLIC_IPFS) continue
transportArray.push(http(urlFunc(chainName)))
}
return fallback(transportArray)
}
const prefix = 'wagmi'
const localStorageWithInvertMiddleware = (): Storage | undefined => {
if (typeof window === 'undefined') return undefined
const storage = window.localStorage
const isMatchingKey = (key: string) => {
if (!key.startsWith(prefix)) return false
if (!key.endsWith('.disconnected')) return false
return true
}
return {
...storage,
getItem: (key_) => {
if (!isMatchingKey(key_)) return storage.getItem(key_)
const key = key_.replace('.disconnected', '.connected')
const connectedStatus = storage.getItem(key)
return connectedStatus ? null : 'true'
},
removeItem: (key_) => {
if (!isMatchingKey(key_)) return storage.removeItem(key_)
const key = key_.replace('.disconnected', '.connected')
storage.setItem(key, 'true')
},
setItem: (key_, value) => {
if (!isMatchingKey(key_)) return storage.setItem(key_, value)
const key = key_.replace('.disconnected', '.connected')
storage.removeItem(key)
},
}
}
const chains = [
...(isLocalProvider ? ([localhostWithEns] as const) : ([] as const)),
mainnetWithEns,
sepoliaWithEns,
holeskyWithEns,
] as const
const transports = {
...(isLocalProvider
? ({
[localhost.id]: http(process.env.NEXT_PUBLIC_PROVIDER!) as unknown as FallbackTransport,
} as const)
: ({} as unknown as {
// this is a hack to make the types happy, dont remove pls
[localhost.id]: HttpTransport
})),
[mainnet.id]: initialiseTransports('mainnet', [drpcUrl, infuraUrl, tenderlyUrl]),
[sepolia.id]: initialiseTransports('sepolia', [drpcUrl, infuraUrl, tenderlyUrl]),
[holesky.id]: initialiseTransports('holesky', [drpcUrl, tenderlyUrl]),
} as const
const wagmiConfig_ = createConfig({
connectors,
ssr: true,
multiInjectedProviderDiscovery: !isInsideSafe(),
storage: createStorage({ storage: localStorageWithInvertMiddleware(), key: prefix }),
chains,
client: ({ chain }) => {
const chainId = chain.id
return createClient<Transport, typeof chain>({
chain,
batch: {
multicall: {
batchSize: 8196,
wait: 50,
},
},
transport: (params) => transports[chainId]({ ...params }),
ccipRead: {
request: ccipRequest(chain),
},
})
},
})
const isSupportedChain = (chainId: number): chainId is (typeof chains)[number]['id'] =>
chains.some((c) => c.id === chainId)
// hotfix for wagmi bug
wagmiConfig_.subscribe(
({ connections, current }) => (current ? connections.get(current)?.chainId : undefined),
(chainId_) => {
const chainId = chainId_ || chains[0].id
// If chain is not configured, then don't switch over to it.
const isChainConfigured = isSupportedChain(chainId)
if (!isChainConfigured) return
return wagmiConfig_.setState((x) => ({
...x,
chainId: chainId ?? x.chainId,
}))
},
)
export const wagmiConfig = wagmiConfig_ as typeof wagmiConfig_ & {
_isEns: true
}
declare module 'wagmi' {
interface Register {
config: typeof wagmiConfig
}
}