|
6 | 6 | webAuthnOptions
|
7 | 7 | } from '@cometh/connect-sdk'
|
8 | 8 | import { toHex } from 'viem'
|
9 |
| -import { Chain, Connector, ConnectorNotFoundError } from 'wagmi' |
| 9 | +import { Chain, Connector } from 'wagmi' |
10 | 10 |
|
11 | 11 | import {
|
12 | 12 | ConnectClient,
|
@@ -51,6 +51,7 @@ export class ComethConnectConnector extends Connector<
|
51 | 51 | ready = false
|
52 | 52 | wallet?: ComethWallet
|
53 | 53 | client?: ConnectClient
|
| 54 | + supportedChains?: SupportedNetworks[] |
54 | 55 |
|
55 | 56 | protected shimDisconnectKey = `${this.id}.shimDisconnect`
|
56 | 57 |
|
@@ -145,27 +146,38 @@ export class ComethConnectConnector extends Connector<
|
145 | 146 | throw new Error('Network not supported')
|
146 | 147 |
|
147 | 148 | return providedChainId
|
148 |
| - } else { |
149 |
| - const supportedChains = this.chains.reduce( |
150 |
| - (chains: SupportedNetworks[], chain: Chain) => { |
151 |
| - const chainId = toHex(chain.id) |
152 |
| - if (!isSupportedNetwork(chainId)) { |
153 |
| - console.warn(`${chain.name} not yet supported by cometh connect`) |
154 |
| - } else { |
155 |
| - chains.push(chainId) |
156 |
| - } |
157 |
| - return chains |
158 |
| - }, |
159 |
| - [] |
160 |
| - ) |
161 |
| - if (supportedChains.length === 0) |
162 |
| - throw new Error('Cometh Connect does not support the provided chains') |
| 149 | + } |
163 | 150 |
|
164 |
| - if (supportedChains.length > 1) |
165 |
| - console.warn(`Cometh connect does not support multichain`) |
| 151 | + // if autoconnect already provided |
| 152 | + if (this.supportedChains) { |
| 153 | + return this.supportedChains[0] |
| 154 | + } else { |
| 155 | + return this._getConfigChain() |
| 156 | + } |
| 157 | + } |
166 | 158 |
|
167 |
| - return supportedChains[0] |
| 159 | + private _getConfigChain() { |
| 160 | + this.supportedChains = this.chains.reduce( |
| 161 | + (chains: SupportedNetworks[], chain: Chain) => { |
| 162 | + const chainId = toHex(chain.id) |
| 163 | + if (!isSupportedNetwork(chainId)) { |
| 164 | + console.warn(`${chain.name} not yet supported by cometh connect`) |
| 165 | + } else { |
| 166 | + chains.push(chainId) |
| 167 | + } |
| 168 | + return chains |
| 169 | + }, |
| 170 | + [] |
| 171 | + ) |
| 172 | + if (this.supportedChains.length === 0) { |
| 173 | + console.warn('Cometh Connect does not support any the provided chains') |
| 174 | + throw new Error('Cometh Connect does not support the provided chains') |
168 | 175 | }
|
| 176 | + |
| 177 | + if (this.supportedChains.length > 1) |
| 178 | + console.warn(`Cometh connect does not support multichain`) |
| 179 | + |
| 180 | + return this.supportedChains[0] |
169 | 181 | }
|
170 | 182 | disconnect(): Promise<void> {
|
171 | 183 | if (!this.wallet) throw new Error('no')
|
@@ -203,8 +215,8 @@ export class ComethConnectConnector extends Connector<
|
203 | 215 | )
|
204 | 216 | return false
|
205 | 217 |
|
206 |
| - const provider = await this.getProvider() |
207 |
| - if (!provider) throw new ConnectorNotFoundError() |
| 218 | + this._getConfigChain() |
| 219 | + |
208 | 220 | return true
|
209 | 221 | } catch {
|
210 | 222 | return false
|
|
0 commit comments