Skip to content

Commit

Permalink
refactor: remove connector warnings (#3558)
Browse files Browse the repository at this point in the history
* refactor: connector warnings

* fix: catch reconnect

* chore: next bundle analyzer

* chore: changeset

* test: up
  • Loading branch information
tmm committed Feb 5, 2024
1 parent f5667c2 commit 895f28e
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 41 deletions.
6 changes: 6 additions & 0 deletions .changeset/empty-gifts-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@wagmi/connectors": patch
"@wagmi/core": patch
---

Fixed connector warnings.
6 changes: 4 additions & 2 deletions packages/connectors/src/coinbaseWallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
type CoinbaseWalletProvider,
CoinbaseWalletSDK,
type CoinbaseWalletSDK,
} from '@coinbase/wallet-sdk'
import {
ChainNotConfiguredError,
Expand Down Expand Up @@ -110,10 +110,12 @@ export function coinbaseWallet(parameters: CoinbaseWalletParameters) {
},
async getChainId() {
const provider = await this.getProvider()
return normalizeChainId(provider.chainId)
const chainId = await provider.request<number>({ method: 'eth_chainId' })
return normalizeChainId(chainId)
},
async getProvider() {
if (!walletProvider) {
const { CoinbaseWalletSDK } = await import('@coinbase/wallet-sdk')
sdk = new CoinbaseWalletSDK({ reloadOnDisconnect, ...parameters })

// Mock implementations to retrieve private `walletExtension` method from the Coinbase Wallet SDK.
Expand Down
4 changes: 0 additions & 4 deletions packages/connectors/src/exports/index.test-d.ts

This file was deleted.

5 changes: 3 additions & 2 deletions packages/connectors/src/metaMask.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
EventType,
MetaMaskSDK,
type MetaMaskSDK,
type MetaMaskSDKOptions,
SDKProvider,
type SDKProvider,
} from '@metamask/sdk'
import {
ChainNotConfiguredError,
Expand Down Expand Up @@ -174,6 +174,7 @@ export function metaMask(parameters: MetaMaskParameters = {}) {
async getProvider() {
if (!walletProvider) {
if (!sdk || !sdk?.isInitialized()) {
const { MetaMaskSDK } = await import('@metamask/sdk')
sdk = new MetaMaskSDK({
enableDebug: false,
dappMetadata: { name: 'wagmi' },
Expand Down
27 changes: 17 additions & 10 deletions packages/connectors/src/safe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SafeAppProvider } from '@safe-global/safe-apps-provider'
import { type Opts, default as SafeAppsSDK } from '@safe-global/safe-apps-sdk'
import { type SafeAppProvider } from '@safe-global/safe-apps-provider'
import { type Opts } from '@safe-global/safe-apps-sdk'
import {
ProviderNotFoundError,
createConnector,
Expand Down Expand Up @@ -31,14 +31,6 @@ export function safe(parameters: SafeParameters = {}) {
type StorageItem = { 'safe.disconnected': true }

let provider_: Provider | undefined
let SDK: typeof SafeAppsSDK.default
if (
typeof SafeAppsSDK !== 'function' &&
typeof SafeAppsSDK.default === 'function'
)
SDK = SafeAppsSDK.default
else SDK = SafeAppsSDK as unknown as typeof SafeAppsSDK.default
const sdk = new SDK(parameters)

return createConnector<Provider, Properties, StorageItem>((config) => ({
id: 'safe',
Expand Down Expand Up @@ -82,8 +74,23 @@ export function safe(parameters: SafeParameters = {}) {
if (!isIframe) return

if (!provider_) {
const { default: SafeAppsSDK } = await import(
'@safe-global/safe-apps-sdk'
)
let SDK: typeof SafeAppsSDK.default
if (
typeof SafeAppsSDK !== 'function' &&
typeof SafeAppsSDK.default === 'function'
)
SDK = SafeAppsSDK.default
else SDK = SafeAppsSDK as unknown as typeof SafeAppsSDK.default
const sdk = new SDK(parameters)

const safe = await sdk.safe.getInfo()
if (!safe) throw new Error('Could not load Safe information')
const { SafeAppProvider } = await import(
'@safe-global/safe-apps-provider'
)
provider_ = new SafeAppProvider(safe, sdk)
}
return provider_
Expand Down
11 changes: 9 additions & 2 deletions packages/connectors/src/walletConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import {
createConnector,
normalizeChainId,
} from '@wagmi/core'
import type { Evaluate, ExactPartial, Omit } from '@wagmi/core/internal'
import { EthereumProvider } from '@walletconnect/ethereum-provider'
import {
type Evaluate,
type ExactPartial,
type Omit,
} from '@wagmi/core/internal'
import { type EthereumProvider } from '@walletconnect/ethereum-provider'
import {
type Address,
type ProviderConnectInfo,
Expand Down Expand Up @@ -197,6 +201,9 @@ export function walletConnect(parameters: WalletConnectParameters) {
async function initProvider() {
const optionalChains = config.chains.map((x) => x.id) as [number]
if (!optionalChains.length) return
const { EthereumProvider } = await import(
'@walletconnect/ethereum-provider'
)
return await EthereumProvider.init({
...parameters,
disableProviderPing: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/connectors/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.test.ts"],
"exclude": ["src/**/*.test.ts", "src/**/*.test-d.ts"],
"compilerOptions": {
"sourceMap": true
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/actions/reconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export async function reconnect(
} else connectors.push(...config.connectors)

// Try recently-used connectors first
const recentConnectorId = await config.storage?.getItem('recentConnectorId')
let recentConnectorId
try {
recentConnectorId = await config.storage?.getItem('recentConnectorId')
} catch {}
const scores: Record<string, number> = {}
for (const [, connection] of config.state.connections) {
scores[connection.connector.id] = 1
Expand Down
4 changes: 1 addition & 3 deletions packages/react/src/hooks/useBlockTransactionCount.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chain, testClient } from '@wagmi/test'
import { chain } from '@wagmi/test'
import { renderHook, waitFor } from '@wagmi/test/react'
import { expect, test } from 'vitest'

Expand Down Expand Up @@ -47,8 +47,6 @@ test('default', async () => {
})

test('parameters: chainId', async () => {
await testClient.mainnet2.mine({ blocks: 1 })

const { result } = renderHook(() =>
useBlockTransactionCount({ chainId: chain.mainnet2.id }),
)
Expand Down
8 changes: 7 additions & 1 deletion playgrounds/next/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import bundleAnalyzer from '@next/bundle-analyzer'

const withBundleAnalyzer = bundleAnalyzer({
enabled: process.env.ANALYZE === 'true',
})

/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
Expand All @@ -11,4 +17,4 @@ const nextConfig = {
},
}

export default nextConfig
export default withBundleAnalyzer(nextConfig)
5 changes: 3 additions & 2 deletions playgrounds/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"build": "ANALYZE=true next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@next/bundle-analyzer": "^14.1.0",
"@tanstack/react-query": "5.0.5",
"next": "13.5.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"next": "13.5.3",
"viem": "2.0.0",
"wagmi": "workspace:*"
},
Expand Down
Loading

0 comments on commit 895f28e

Please sign in to comment.