Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
Add chain switching to the Ledger connector
Browse files Browse the repository at this point in the history
  • Loading branch information
hlopes-ledger committed Mar 9, 2023
1 parent 55be166 commit ad2eeb4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .changeset/hot-peas-kiss.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
'@wagmi/connectors': patch
---

Added "ledger" to the regular expression that determines the wallets allowed to switch chains.
Added "ledger" to the regular expression that determines the wallets allowed
to switch chains and #switchChain method to the Ledger connector.
45 changes: 44 additions & 1 deletion packages/connectors/src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
Chain,
ProviderRpcError,
RpcError,
SwitchChainError,
UserRejectedRequestError,
normalizeChainId,
} from '@wagmi/core'
import { providers } from 'ethers'
import { getAddress } from 'ethers/lib/utils.js'
import { getAddress, hexValue } from 'ethers/lib/utils.js'

import type { ConnectorData } from './base'
import { Connector } from './base'
Expand Down Expand Up @@ -65,6 +66,9 @@ export class LedgerConnector extends Connector<
const id = await this.getChainId()
const unsupported = this.isChainUnsupported(id)

// Enable support for programmatic chain switching
this.switchChain = this.#switchChain

return {
account,
chain: { id, unsupported },
Expand Down Expand Up @@ -172,6 +176,45 @@ export class LedgerConnector extends Connector<
}
}

async #switchChain(chainId: number) {
const provider = await this.getProvider()
const id = hexValue(chainId)

try {
// Set up a race between `wallet_switchEthereumChain` & the `chainChanged` event
// to ensure the chain has been switched. This is because there could be a case
// where a wallet may not resolve the `wallet_switchEthereumChain` method, or
// resolves slower than `chainChanged`.
await Promise.race([
provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: id }],
}),
new Promise((res) =>
this.on('change', ({ chain }) => {
if (chain?.id === chainId) res(chainId)
}),
),
])
return (
this.chains.find((x) => x.id === chainId) ??
({
id: chainId,
name: `Chain ${id}`,
network: `${id}`,
nativeCurrency: { name: 'Ether', decimals: 18, symbol: 'ETH' },
rpcUrls: { default: { http: [''] }, public: { http: [''] } },
} as Chain)
)
} catch (error) {
const message =
typeof error === 'string' ? error : (error as ProviderRpcError)?.message
if (/user rejected request/i.test(message))
throw new UserRejectedRequestError(error)
throw new SwitchChainError(error)
}
}

protected onAccountsChanged = (accounts: string[]) => {
if (accounts.length === 0) this.emit('disconnect')
else this.emit('change', { account: getAddress(accounts[0] as string) })
Expand Down

0 comments on commit ad2eeb4

Please sign in to comment.