Skip to content

Commit

Permalink
refactor: add connector entrypoints (#52)
Browse files Browse the repository at this point in the history
* refactor: add connector entrypoints

* chore: add changeset
  • Loading branch information
tmm authored Jan 8, 2022
1 parent c21c361 commit da7a3a6
Show file tree
Hide file tree
Showing 37 changed files with 206 additions and 351 deletions.
16 changes: 16 additions & 0 deletions .changeset/fifty-toes-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'wagmi-private': minor
'wagmi': minor
'wagmi-testing': minor
---

Moves connectors to their own entrypoints to reduce bundle size.

```ts
// old - WalletLinkConnector unused, but still in final bundle
import { InjectedConnector, WalletConnectConnector } from 'wagmi'

// new - WalletLinkConnector not in final bundle
import { InjectedConnector } from 'wagmi/connectors/injected'
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect'
```
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Feel free to join the [discussions on GitHub](https://github.com/tmm/wagmi/discu

- awkweb.eth
- [GitHub Sponsors](https://github.com/sponsors/tmm)
- [Sign the guestbook](https://github.com/tmm/wagmi/discussions/2)
- Sign the [guestbook](https://github.com/tmm/wagmi/discussions/2)

## Thanks

Expand Down
48 changes: 28 additions & 20 deletions docs/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@ import type { AppProps } from 'next/app'
import { ThemeProvider } from 'degen'
import { providers } from 'ethers'
import Head from 'next/head'
import {
InjectedConnector,
Provider,
WalletConnectConnector,
WalletLinkConnector,
chain,
defaultChains,
} from 'wagmi'

import 'nextra-theme-docs/style.css'
import '../styles/globals.css'
Expand All @@ -19,16 +11,27 @@ import '../styles/globals.css'
import 'degen/styles'
/* eslint-enable import/no-unresolved */

// Imports
import { Connector, Provider, chain, defaultChains } from 'wagmi'
import { InjectedConnector } from 'wagmi/connectors/injected'
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect'
import { WalletLinkConnector } from 'wagmi/connectors/walletLink'

// Get environment variables
const alchemy = process.env.NEXT_PUBLIC_ALCHEMY_ID as string
const etherscan = process.env.NEXT_PUBLIC_ETHERSCAN_API_KEY as string
const infuraId = process.env.NEXT_PUBLIC_INFURA_ID as string

// Pick chains
const chains = defaultChains
const defaultChain = chain.mainnet

type Config = { chainId?: number }
const connectors = ({ chainId }: Config) => {
// Set up connectors
type ConnectorsConfig = { chainId?: number }
const connectors = ({ chainId }: ConnectorsConfig) => {
const rpcUrl =
chains.find((x) => x.id === chainId)?.rpcUrls?.[0] ??
chain.mainnet.rpcUrls[0]
defaultChain.rpcUrls[0]
return [
new InjectedConnector({ chains }),
new WalletConnectConnector({
Expand All @@ -46,25 +49,30 @@ const connectors = ({ chainId }: Config) => {
]
}

// Set up providers
type ProviderConfig = { chainId?: number; connector?: Connector }
const isChainSupported = (chainId?: number) =>
chains.some((x) => x.id === chainId)
const provider = ({ chainId }: Config) =>

const provider = ({ chainId }: ProviderConfig) =>
providers.getDefaultProvider(
isChainSupported(chainId) ? chainId : chain.mainnet.id,
{ alchemy, etherscan, infuraId },
)
const webSocketProvider = ({ chainId }: Config) =>
new providers.InfuraWebSocketProvider(
isChainSupported(chainId) ? chainId : chain.mainnet.id,
infuraId,
isChainSupported(chainId) ? chainId : defaultChain.id,
{
alchemy,
etherscan,
infuraId,
},
)
const webSocketProvider = ({ chainId }: ProviderConfig) =>
isChainSupported(chainId)
? new providers.InfuraWebSocketProvider(chainId, infuraId)
: undefined

const App = ({ Component, pageProps }: AppProps) => {
return (
<>
<Provider
autoConnect
connectorStorageKey="wagmi.wallet"
connectors={connectors}
provider={provider}
webSocketProvider={webSocketProvider}
Expand Down
25 changes: 14 additions & 11 deletions docs/pages/docs/connectors.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ The following built-in connectors are part of wagmi's core and support most wall
The **InjectedConnector** supports wallets that _inject_ an Ethereum provider into the browser or window. The [MetaMask](https://metamask.io) browser extension is the most popular example of this.

```tsx
import { InjectedConnector } from 'wagmi'
import { InjectedConnector } from 'wagmi/connectors/injected'
```

#### Usage

```tsx
import { InjectedConnector } from 'wagmi'
import { InjectedConnector } from 'wagmi/connectors/injected'

const connector = new InjectedConnector()
```
Expand All @@ -31,7 +31,8 @@ const connector = new InjectedConnector()
Chains supported by app. Defaults to `defaultChains`.

```tsx highlight='4'
import { InjectedConnector, defaultChains, defaultL2Chains } from 'wagmi'
import { defaultChains, defaultL2Chains } from 'wagmi'
import { InjectedConnector } from 'wagmi/connectors/injected'

const connector = new InjectedConnector({
chains: [...defaultChains, ...defaultL2Chains],
Expand All @@ -43,13 +44,13 @@ const connector = new InjectedConnector({
The **WalletConnectConnector** wraps the [WalletConnect](https://walletconnect.com) Ethereum provider and supports its configuration options. This is a great option for adding support for many wallets to your app.

```tsx
import { WalletConnectConnector } from 'wagmi'
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect'
```

#### Usage

```tsx
import { WalletConnectConnector } from 'wagmi'
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect'

const connector = new WalletConnectConnector({
options: {
Expand All @@ -65,7 +66,8 @@ const connector = new WalletConnectConnector({
Chains supported by app. Defaults to `defaultChains`.

```tsx highlight='4'
import { WalletConnectConnector, defaultChains, defaultL2Chains } from 'wagmi'
import { defaultChains, defaultL2Chains } from 'wagmi'
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect'

const connector = new WalletConnectConnector({
chains: [...defaultChains, ...defaultL2Chains],
Expand All @@ -77,7 +79,7 @@ const connector = new WalletConnectConnector({
Options to pass to the WalletConnect [Ethereum Provider](https://docs.walletconnect.com/quick-start/dapps/ethereum-provider).

```tsx highlight='4-6'
import { WalletConnectConnector } from 'wagmi'
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect'

const connector = new WalletConnectConnector({
options: {
Expand All @@ -91,13 +93,13 @@ const connector = new WalletConnectConnector({
The **WalletLinkConnector** wraps the [WalletLink](https://docs.cloud.coinbase.com/walletlink/docs) provider and supports connecting with Coinbase Wallet.

```tsx
import { WalletLinkConnector } from 'wagmi'
import { WalletLinkConnector } from 'wagmi/connectors/walletLink'
```

#### Usage

```tsx
import { WalletLinkConnector } from 'wagmi'
import { WalletLinkConnector } from 'wagmi/connectors/walletLink'

const connector = new WalletLinkConnector({
options: {
Expand All @@ -114,7 +116,8 @@ const connector = new WalletLinkConnector({
Chains supported by app. Defaults to `defaultChains`.

```tsx highlight='4'
import { WalletLinkConnector, defaultChains, defaultL2Chains } from 'wagmi'
import { defaultChains, defaultL2Chains } from 'wagmi'
import { WalletLinkConnector } from 'wagmi/connectors/walletLink'

const connector = new WalletLinkConnector({
chains: [...defaultChains, ...defaultL2Chains],
Expand All @@ -126,7 +129,7 @@ const connector = new WalletLinkConnector({
Options to pass to the WalletConnect [Ethereum Provider](https://docs.walletconnect.com/quick-start/dapps/ethereum-provider).

```tsx highlight='4-7'
import { WalletLinkConnector } from 'wagmi'
import { WalletLinkConnector } from 'wagmi/connectors/walletLink'

const connector = new WalletLinkConnector({
options: {
Expand Down
9 changes: 3 additions & 6 deletions docs/pages/docs/provider.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,9 @@ const App = () => (
Connectors used for linking accounts. Defaults to `[new InjectedConnector()]`.

```tsx highlight='8-17,20'
import {
InjectedConnector,
Provider,
WalletConnectConnector,
defaultChains,
} from 'wagmi'
import { Provider, defaultChains } from 'wagmi'
import { InjectedConnector } from 'wagmi/connectors/injected'
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect'

const connectors = [
new InjectedConnector({ chains: defaultChains }),
Expand Down
12 changes: 4 additions & 8 deletions docs/pages/guides/connect-wallet.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ The example below uses [`useConnect`](/docs/hooks/useConnect) and [`useAccount`]
First, we want to set up our [`Provider`](/docs/provider) with the Injected (MetaMask), WalletConnect, and WalletLink (Coinbase Wallet) connectors:

```tsx
import {
InjectedConnector,
Provider,
WalletConnectConnector,
WalletLinkConnector,
chain,
defaultChains,
} from 'wagmi'
import { Provider, chain, defaultChains } from 'wagmi'
import { InjectedConnector } from 'wagmi/connectors/injected'
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect'
import { WalletLinkConnector } from 'wagmi/connectors/walletLink'

import { Example } from './components'

Expand Down
7 changes: 6 additions & 1 deletion docs/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ Feel free to join the [discussions on GitHub](https://github.com/tmm/wagmi/discu

- awkweb.eth
- [GitHub Sponsors](https://github.com/sponsors/tmm)
- [Sign the guestbook](https://github.com/tmm/wagmi/discussions/2)
- Sign the [guestbook](https://github.com/tmm/wagmi/discussions/2)

## Thanks

- [ricmoo.eth](https://twitter.com/ricmoo) for creating and continued work on [ethers.js](https://github.com/ethers-io/ethers.js)
- [Mirror](https://mirror.xyz) for creating space to do good work
2 changes: 2 additions & 0 deletions examples/next/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
NEXT_PUBLIC_ALCHEMY_ID=
NEXT_PUBLIC_ETHERSCAN_API_KEY=
NEXT_PUBLIC_INFURA_ID=
29 changes: 0 additions & 29 deletions examples/next/src/components/Account.tsx

This file was deleted.

28 changes: 0 additions & 28 deletions examples/next/src/components/Connect.tsx

This file was deleted.

27 changes: 0 additions & 27 deletions examples/next/src/components/NetworkSwitcher.tsx

This file was deleted.

43 changes: 0 additions & 43 deletions examples/next/src/components/SignMessage.tsx

This file was deleted.

Loading

1 comment on commit da7a3a6

@vercel
Copy link

@vercel vercel bot commented on da7a3a6 Jan 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

Please sign in to comment.