-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathwagmi.ts
More file actions
126 lines (105 loc) · 4.06 KB
/
wagmi.ts
File metadata and controls
126 lines (105 loc) · 4.06 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
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 { getChainsFromUrl, SupportedChain } from '@app/constants/chains'
import { isInsideSafe } from '../safe'
import { rainbowKitConnectors } from './wallets'
const isLocalProvider = !!process.env.NEXT_PUBLIC_PROVIDER
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)
}
export 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)
},
}
}
export 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 chains = getChainsFromUrl() as unknown as readonly [SupportedChain, ...SupportedChain[]]
const wagmiConfig_ = createConfig({
syncConnectedChain: false,
connectors: rainbowKitConnectors,
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),
},
})
},
})
export const wagmiConfig = wagmiConfig_ as typeof wagmiConfig_ & {
_isEns: true
}
declare module 'wagmi' {
interface Register {
config: typeof wagmiConfig
}
}