Skip to content

Commit

Permalink
tmm/switch chain response (#127)
Browse files Browse the repository at this point in the history
* feat: switch chain response type

* chore: changeset
  • Loading branch information
tmm authored Jan 30, 2022
1 parent 6f6a013 commit f05b031
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .changeset/wise-pears-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'wagmi-private': patch
'wagmi': patch
'wagmi-testing': patch
---

update switch chain return type
2 changes: 1 addition & 1 deletion docs/pages/docs/hooks/useNetwork.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ const App = () => {
### switchNetwork

```ts
(chainId: number) => Promise<{ data?: boolean; error?: Error }>
(chainId: number) => Promise<{ data?: Chain; error?: Error }>
```
2 changes: 1 addition & 1 deletion packages/private/src/connectors/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export abstract class Connector<
abstract getProvider(create?: boolean): Provider
abstract getSigner(): Promise<Signer>
abstract isAuthorized(): Promise<boolean>
switchChain?(chainId: number): Promise<void>
switchChain?(chainId: number): Promise<Chain | undefined>
watchAsset?(asset: {
address: string
image?: string
Expand Down
3 changes: 3 additions & 0 deletions packages/private/src/connectors/injected.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ExternalProvider, Web3Provider } from '@ethersproject/providers'

import { Chain } from '../types'
import { allChains } from '../constants'
import {
getAddress,
getInjectedName,
Expand Down Expand Up @@ -148,6 +149,8 @@ export class InjectedConnector extends Connector<
method: 'wallet_switchEthereumChain',
params: [{ chainId: id }],
})
const chains = [...this.chains, ...allChains]
return chains.find((x) => x.id === chainId)
} catch (error) {
// Indicates chain is not added to MetaMask
if ((<ProviderRpcError>error).code === 4902) {
Expand Down
3 changes: 3 additions & 0 deletions packages/private/src/connectors/walletConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ExternalProvider, Web3Provider } from '@ethersproject/providers'
import WalletConnectProvider from '@walletconnect/ethereum-provider'
import { IWCEthRpcConnectionOptions } from '@walletconnect/types'

import { allChains } from '../constants'
import { SwitchChainError, UserRejectedRequestError } from '../errors'
import { Chain } from '../types'
import { getAddress, hexValue, normalizeChainId } from '../utils'
Expand Down Expand Up @@ -111,6 +112,8 @@ export class WalletConnectConnector extends Connector<
method: 'wallet_switchEthereumChain',
params: [{ chainId: id }],
})
const chains = [...this.chains, ...allChains]
return chains.find((x) => x.id === chainId)
} catch (error) {
const message =
typeof error === 'string' ? error : (<ProviderRpcError>error)?.message
Expand Down
3 changes: 3 additions & 0 deletions packages/private/src/connectors/walletLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ExternalProvider, Web3Provider } from '@ethersproject/providers'
import { WalletLink, WalletLinkProvider } from 'walletlink'
import { WalletLinkOptions } from 'walletlink/dist/WalletLink'

import { allChains } from '../constants'
import { SwitchChainError, UserRejectedRequestError } from '../errors'
import { Chain } from '../types'
import { getAddress, hexValue, normalizeChainId } from '../utils'
Expand Down Expand Up @@ -118,6 +119,8 @@ export class WalletLinkConnector extends Connector<
method: 'wallet_switchEthereumChain',
params: [{ chainId: id }],
})
const chains = [...this.chains, ...allChains]
return chains.find((x) => x.id === chainId)
} catch (error) {
if (
/user rejected signature request/i.test(
Expand Down
2 changes: 2 additions & 0 deletions packages/private/src/constants/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ export const chain: Record<ChainName, Chain> = {
},
}

export const allChains: Chain[] = Object.values(chain)

export const defaultChains: Chain[] = [
chain.mainnet,
chain.ropsten,
Expand Down
1 change: 1 addition & 0 deletions packages/private/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { erc1155ABI, erc20ABI, erc721ABI } from './abis'

export {
chain,
allChains,
defaultChains,
defaultL2Chains,
developmentChains,
Expand Down
1 change: 1 addition & 0 deletions packages/private/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ it('should expose correct exports', () => {
"erc20ABI",
"erc721ABI",
"chain",
"allChains",
"defaultChains",
"defaultL2Chains",
"developmentChains",
Expand Down
1 change: 1 addition & 0 deletions packages/private/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
erc20ABI,
erc721ABI,
chain,
allChains,
defaultChains,
defaultL2Chains,
developmentChains,
Expand Down
26 changes: 25 additions & 1 deletion packages/react/src/hooks/accounts/useNetwork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,31 @@ describe('useNetwork', () => {
`)

await actHook(async () => {
await result.current.network[1]?.(4)
const res = await result.current.network[1]?.(4)
expect(res).toMatchInlineSnapshot(`
{
"data": {
"blockExplorers": [
{
"name": "Etherscan",
"url": "https://rinkeby.etherscan.io",
},
],
"id": 4,
"name": "Rinkeby",
"nativeCurrency": {
"decimals": 18,
"name": "Rinkeby Ether",
"symbol": "rETH",
},
"rpcUrls": [
"https://rinkeby.infura.io/v3",
],
"testnet": true,
},
"error": undefined,
}
`)
})

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
17 changes: 8 additions & 9 deletions packages/react/src/hooks/accounts/useNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { Chain, defaultChains, defaultL2Chains } from 'wagmi-private'
import { Chain, SwitchChainError, allChains } from 'wagmi-private'

import { useContext } from '../../context'

Expand All @@ -21,20 +21,19 @@ export const useNetwork = () => {
const chainId = data?.chain?.id
const unsupported = data?.chain?.unsupported
const activeChains = connector?.chains ?? []
const activeChain: Chain | undefined = [
...activeChains,
...defaultChains,
...defaultL2Chains,
].find((x) => x.id === chainId)
const activeChain: Chain | undefined = [...activeChains, ...allChains].find(
(x) => x.id === chainId,
)

const switchNetwork = React.useCallback(
async (chainId: number) => {
if (!connector?.switchChain) return false
if (!connector?.switchChain)
return { data: undefined, error: new SwitchChainError() }
try {
setState((x) => ({ ...x, error: undefined, loading: true }))
await connector.switchChain(chainId)
const chain = await connector.switchChain(chainId)
setState((x) => ({ ...x, loading: false }))
return { data: true, error: undefined }
return { data: chain, error: undefined }
} catch (error_) {
const error = <Error>error_
setState((x) => ({ ...x, error, loading: false }))
Expand Down
3 changes: 3 additions & 0 deletions packages/testing/src/connectors/mockConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getAddress } from 'ethers/lib/utils'
import {
Chain,
Connector,
allChains,
defaultChains,
normalizeChainId,
} from 'wagmi-private'
Expand Down Expand Up @@ -93,6 +94,8 @@ export class MockConnector extends Connector<
async switchChain(chainId: number) {
const provider = this.getProvider()
await provider.switchChain(chainId)
const chains = [...this.chains, ...allChains]
return chains.find((x) => x.id === chainId)
}

async watchAsset(asset: {
Expand Down

1 comment on commit f05b031

@vercel
Copy link

@vercel vercel bot commented on f05b031 Jan 30, 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.