-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
defaultConfig.ts
124 lines (104 loc) · 2.92 KB
/
defaultConfig.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
import '@web3modal/polyfills'
import type { Chain, Metadata, Provider, ProviderType } from '@web3modal/scaffold-utils/ethers'
import { CoinbaseWalletSDK, type ProviderInterface } from '@coinbase/wallet-sdk'
import type { SocialProvider } from '@web3modal/scaffold-utils'
export interface ConfigOptions {
enableEIP6963?: boolean
enableCoinbase?: boolean
auth?: {
email?: boolean
socials?: SocialProvider[]
showWallets?: boolean
walletFeatures?: boolean
}
enableInjected?: boolean
/**
* @deprecated this doesn't do anything, use `chains` instead
*/
rpcUrl?: string
defaultChainId?: number
metadata: Metadata
chains?: Chain[]
coinbasePreference?: 'all' | 'smartWalletOnly' | 'eoaOnly'
}
export function defaultConfig(options: ConfigOptions) {
const defaultAuth = {
email: true,
showWallets: true,
walletFeatures: true,
socials: [
'google',
'x',
'discord',
'farcaster',
'github',
'apple',
'facebook'
] as SocialProvider[]
}
const {
enableEIP6963 = true,
enableCoinbase = true,
enableInjected = true,
auth,
metadata
} = options
let injectedProvider: Provider | undefined = undefined
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
let coinbaseProvider: ProviderInterface | undefined = undefined
const providers: ProviderType = { metadata }
function getInjectedProvider() {
if (injectedProvider) {
return injectedProvider
}
if (typeof window === 'undefined') {
return undefined
}
if (!window.ethereum) {
return undefined
}
// @ts-expect-error window.ethereum satisfies Provider
injectedProvider = window.ethereum
return injectedProvider
}
function getCoinbaseProvider() {
if (coinbaseProvider) {
return coinbaseProvider
}
if (typeof window === 'undefined') {
return undefined
}
const coinbaseWallet = new CoinbaseWalletSDK({
appName: metadata.name,
appLogoUrl: metadata.icons[0],
appChainIds: options.chains?.map(chain => chain.chainId) || [1, 84532]
})
coinbaseProvider = coinbaseWallet.makeWeb3Provider({
/**
* Determines which wallet options to display in Coinbase Wallet SDK.
* @property options
* - `all`: Show both smart wallet and EOA options.
* - `smartWalletOnly`: Show only smart wallet options.
* - `eoaOnly`: Show only EOA options.
* @see https://www.smartwallet.dev/sdk/v3-to-v4-changes#parameters
*/
options: options.coinbasePreference || 'all'
})
return coinbaseProvider
}
if (enableCoinbase) {
providers.coinbase = getCoinbaseProvider()
}
if (enableInjected) {
providers.injected = getInjectedProvider()
}
if (enableEIP6963) {
providers.EIP6963 = true
}
const mergedAuth = {
...defaultAuth,
...auth
}
providers.auth = mergedAuth
return providers
}