-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: upgrades
@walletconnect/ethereum-provider
and `@walletconnect/…
…modal` (#868) * fix: update wcv2 to remove polyfills, fix crypto.decode errors, allow empty required chains, improve deep link ux * update to newer package version * enforce new rules on optional/required chains parameters * fix test types * fix defaultChainId * remove unnecessary changes * assign this.provider * fiddle with tests * add test for optional chains config * pr review * refactor to validate chainProps on init * Update packages/walletconnect-v2/src/index.ts Co-authored-by: Zach Pomerantz <[email protected]> * Update packages/walletconnect-v2/src/index.ts Co-authored-by: Zach Pomerantz <[email protected]> * Update packages/walletconnect-v2/src/index.ts Co-authored-by: Zach Pomerantz <[email protected]> * Update packages/walletconnect-v2/src/index.ts Co-authored-by: Zach Pomerantz <[email protected]> * pr review --------- Co-authored-by: Zach Pomerantz <[email protected]>
- Loading branch information
1 parent
634d146
commit 4d4180d
Showing
7 changed files
with
184 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ import type { Actions, ProviderRpcError } from '@web3-react/types' | |
import { Connector } from '@web3-react/types' | ||
import EventEmitter3 from 'eventemitter3' | ||
|
||
import { getBestUrlMap, getChainsWithDefault } from './utils' | ||
import { ArrayOneOrMore, getBestUrlMap, getChainsWithDefault, isArrayOneOrMore } from './utils' | ||
|
||
export const URI_AVAILABLE = 'URI_AVAILABLE' | ||
const DEFAULT_TIMEOUT = 5000 | ||
|
@@ -23,6 +23,19 @@ export type WalletConnectOptions = Omit<Parameters<typeof WalletConnectProvider. | |
rpc?: { [chainId: number]: string | string[] } | ||
} | ||
|
||
/** | ||
* Necessary type to interface with @walletconnect/[email protected] which is currently unexported | ||
*/ | ||
type ChainsProps = | ||
| { | ||
chains: ArrayOneOrMore<number> | ||
optionalChains?: number[] | ||
} | ||
| { | ||
chains?: number[] | ||
optionalChains: ArrayOneOrMore<number> | ||
} | ||
|
||
/** | ||
* Options to configure the WalletConnect connector. | ||
*/ | ||
|
@@ -51,22 +64,26 @@ export class WalletConnect extends Connector { | |
private readonly options: Omit<WalletConnectOptions, 'rpcMap' | 'chains'> | ||
|
||
private readonly rpcMap?: Record<number, string | string[]> | ||
private readonly chains: number[] | ||
private readonly chains: number[] | ArrayOneOrMore<number> | undefined | ||
private readonly optionalChains: number[] | ArrayOneOrMore<number> | undefined | ||
private readonly defaultChainId?: number | ||
private readonly timeout: number | ||
|
||
private eagerConnection?: Promise<WalletConnectProvider> | ||
|
||
constructor({ actions, options, defaultChainId, timeout = DEFAULT_TIMEOUT, onError }: WalletConnectConstructorArgs) { | ||
constructor({ actions, defaultChainId, options, timeout = DEFAULT_TIMEOUT, onError }: WalletConnectConstructorArgs) { | ||
super(actions, onError) | ||
|
||
const { rpcMap, rpc, chains, ...rest } = options | ||
const { rpcMap, rpc, ...rest } = options | ||
|
||
this.options = rest | ||
this.chains = chains | ||
this.defaultChainId = defaultChainId | ||
this.rpcMap = rpcMap || rpc | ||
this.timeout = timeout | ||
|
||
const { chains, optionalChains } = this.getChainProps(rest.chains, rest.optionalChains, defaultChainId) | ||
this.chains = chains | ||
this.optionalChains = optionalChains | ||
} | ||
|
||
private disconnectListener = (error: ProviderRpcError) => { | ||
|
@@ -86,27 +103,51 @@ export class WalletConnect extends Connector { | |
this.events.emit(URI_AVAILABLE, uri) | ||
} | ||
|
||
private async initializeProvider( | ||
desiredChainId: number | undefined = this.defaultChainId | ||
): Promise<WalletConnectProvider> { | ||
const rpcMap = this.rpcMap ? getBestUrlMap(this.rpcMap, this.timeout) : undefined | ||
const chainProps = this.getChainProps(this.chains, this.optionalChains, desiredChainId) | ||
|
||
const ethProviderModule = await import('@walletconnect/ethereum-provider') | ||
this.provider = await ethProviderModule.default.init({ | ||
...this.options, | ||
...chainProps, | ||
rpcMap: await rpcMap, | ||
}) | ||
|
||
return this.provider | ||
.on('disconnect', this.disconnectListener) | ||
.on('chainChanged', this.chainChangedListener) | ||
.on('accountsChanged', this.accountsChangedListener) | ||
.on('display_uri', this.URIListener) | ||
} | ||
|
||
private getChainProps( | ||
chains: number[] | ArrayOneOrMore<number> | undefined, | ||
optionalChains: number[] | ArrayOneOrMore<number> | undefined, | ||
desiredChainId: number | undefined = this.defaultChainId | ||
): ChainsProps { | ||
// Reorder chains and optionalChains if necessary | ||
const orderedChains = getChainsWithDefault(chains, desiredChainId) | ||
const orderedOptionalChains = getChainsWithDefault(optionalChains, desiredChainId) | ||
|
||
// Validate and return the result. | ||
// Type discrimination requires that we use these typeguard checks to guarantee a valid return type. | ||
if (isArrayOneOrMore(orderedChains)) { | ||
return { chains: orderedChains, optionalChains: orderedOptionalChains } | ||
} else if (isArrayOneOrMore(orderedOptionalChains)) { | ||
return { chains: orderedChains, optionalChains: orderedOptionalChains } | ||
} | ||
|
||
throw new Error('Either chains or optionalChains must have at least one item.') | ||
} | ||
|
||
private isomorphicInitialize( | ||
desiredChainId: number | undefined = this.defaultChainId | ||
): Promise<WalletConnectProvider> { | ||
if (this.eagerConnection) return this.eagerConnection | ||
|
||
const rpcMap = this.rpcMap ? getBestUrlMap(this.rpcMap, this.timeout) : undefined | ||
const chains = desiredChainId ? getChainsWithDefault(this.chains, desiredChainId) : this.chains | ||
|
||
return (this.eagerConnection = import('@walletconnect/ethereum-provider').then(async (ethProviderModule) => { | ||
const provider = (this.provider = await ethProviderModule.default.init({ | ||
...this.options, | ||
chains, | ||
rpcMap: await rpcMap, | ||
})) | ||
|
||
return provider | ||
.on('disconnect', this.disconnectListener) | ||
.on('chainChanged', this.chainChangedListener) | ||
.on('accountsChanged', this.accountsChangedListener) | ||
.on('display_uri', this.URIListener) | ||
})) | ||
return (this.eagerConnection = this.initializeProvider(desiredChainId)) | ||
} | ||
|
||
/** {@inheritdoc Connector.connectEagerly} */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
/** | ||
* Necessary type to interface with @walletconnect/[email protected] which is currently unexported | ||
*/ | ||
export type ArrayOneOrMore<T> = { | ||
0: T | ||
} & Array<T> | ||
|
||
/** | ||
* This is a type guard for ArrayOneOrMore | ||
*/ | ||
export function isArrayOneOrMore<T>(input: T[] = []): input is ArrayOneOrMore<T> { | ||
return input.length > 0 | ||
} | ||
|
||
/** | ||
* @param rpcMap - Map of chainIds to rpc url(s). | ||
* @param timeout - Timeout, in milliseconds, after which to consider network calls failed. | ||
|
@@ -85,7 +99,13 @@ async function getBestUrl(urls: string | string[], timeout: number): Promise<str | |
* @param chains - An array of chain IDs. | ||
* @param defaultChainId - The chain ID to treat as the default (it will be the first element in the returned array). | ||
*/ | ||
export function getChainsWithDefault(chains: number[], defaultChainId: number) { | ||
export function getChainsWithDefault( | ||
chains: number[] | ArrayOneOrMore<number> | undefined, | ||
defaultChainId: number | undefined | ||
) { | ||
if (!chains || !defaultChainId || chains.length === 0) { | ||
return chains | ||
} | ||
const idx = chains.indexOf(defaultChainId) | ||
if (idx === -1) { | ||
throw new Error( | ||
|
Oops, something went wrong.
4d4180d
There was a problem hiding this comment.
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:
web3-react – ./
web3-react-uniswap.vercel.app
web3-react-mu.vercel.app
web3-react-git-main-uniswap.vercel.app
4d4180d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great