forked from liteflow-labs/starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectors.ts
138 lines (128 loc) · 3.51 KB
/
connectors.ts
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
import { UniversalWalletConnector } from '@magiclabs/wagmi-connector'
import {
connectorsForWallets,
Wallet,
WalletList,
} from '@rainbow-me/rainbowkit'
import {
braveWallet,
coinbaseWallet,
injectedWallet,
metaMaskWallet,
rainbowWallet,
walletConnectWallet,
} from '@rainbow-me/rainbowkit/wallets'
import type { Chain, Config } from 'wagmi'
import { configureChains, createConfig } from 'wagmi'
import { alchemyProvider } from 'wagmi/providers/alchemy'
import { publicProvider } from 'wagmi/providers/public'
import { Environment } from './environment'
type ClientWithChain = {
client: Config<any>
chains: Chain[]
}
function connectors(environment: Environment): ClientWithChain {
const providers = [publicProvider()]
// add alchemy provider as fallback if ALCHEMY_API_KEY is set
if (environment.ALCHEMY_API_KEY)
providers.push(alchemyProvider({ apiKey: environment.ALCHEMY_API_KEY }))
const { chains, publicClient } = configureChains<Chain>(
environment.CHAINS,
providers,
)
// Copied from https://github.com/rainbow-me/rainbowkit/blob/main/packages/rainbowkit/src/wallets/getDefaultWallets.ts#L11
// Only added the shimDisconnect option
const getDefaultWallets = ({
appName,
chains,
projectId,
shimDisconnect,
}: {
appName: string
chains: Chain[]
projectId: string
shimDisconnect?: boolean
}): {
connectors: ReturnType<typeof connectorsForWallets>
wallets: WalletList
} => {
const wallets: WalletList = [
{
groupName: 'Popular',
wallets: [
injectedWallet({ chains, shimDisconnect }),
rainbowWallet({ chains, projectId, shimDisconnect }),
coinbaseWallet({ appName, chains }),
metaMaskWallet({ chains, projectId, shimDisconnect }),
walletConnectWallet({ chains, projectId }),
braveWallet({ chains, shimDisconnect }),
environment.MAGIC_API_KEY
? emailConnector({
chains,
apiKey: environment.MAGIC_API_KEY,
alchemyApiKey: environment.ALCHEMY_API_KEY,
})
: undefined,
].filter(Boolean),
},
]
return {
connectors: connectorsForWallets(wallets)(),
wallets,
}
}
const { connectors } = getDefaultWallets({
appName: environment.META_TITLE,
projectId: environment.WALLET_CONNECT_PROJECT_ID,
chains,
shimDisconnect: true,
})
const client = createConfig({
autoConnect: true,
connectors: connectors,
publicClient,
})
return {
client,
chains,
}
}
function emailConnector({
chains,
apiKey,
alchemyApiKey,
}: {
chains: Chain[]
apiKey: string
alchemyApiKey: string | undefined
}): Wallet {
if (!alchemyApiKey)
throw new Error(
'env NEXT_PUBLIC_ALCHEMY_API_KEY is required for email connector',
)
return {
id: 'magic',
name: 'Magic',
iconUrl: '/magic.svg',
iconBackground: '#fff',
createConnector: () => {
const connector = new UniversalWalletConnector({
chains: chains,
options: {
apiKey: apiKey,
networks: chains.map((chain) => ({
chainId: chain.id,
rpcUrl:
chain.id === 1 // the default provider for Ethereum Mainnet is not working with Magic, only Alchemy is
? 'https://eth-mainnet.g.alchemy.com/v2/' + alchemyApiKey
: chain.rpcUrls.default.http[0]!,
})),
},
})
return {
connector,
}
},
}
}
export default connectors