From 9bd1a891ea87a32e07147b085a0b595808ff40ee Mon Sep 17 00:00:00 2001 From: Jordan Frankfurt Date: Wed, 2 Aug 2023 18:27:51 -0500 Subject: [PATCH] refactor to validate chainProps on init --- packages/walletconnect-v2/src/index.spec.ts | 6 +++--- packages/walletconnect-v2/src/index.ts | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/walletconnect-v2/src/index.spec.ts b/packages/walletconnect-v2/src/index.spec.ts index 5a36ecf1a..b318bb0f8 100644 --- a/packages/walletconnect-v2/src/index.spec.ts +++ b/packages/walletconnect-v2/src/index.spec.ts @@ -156,11 +156,11 @@ describe('WalletConnect', () => { await expect(connector.activate(99)).rejects.toThrow() }) - test('should throw an error when using optional chain as default', async () => { - const { connector } = createTestEnvironment({ chains, optionalChains: [8] }, 8) - await expect(connector.activate()).rejects.toThrow() + test('should throw an error when using optional chain as default', () => { + expect(() => createTestEnvironment({ chains, optionalChains: [8] }, 8)).toThrow('Invalid chainId 8. Make sure default chain is included in "chains" - chains specified in "optionalChains" may not be selected as the default, as they may not be supported by the wallet.') }) + test('should switch to an optional chain', async () => { const { connector, store } = createTestEnvironment({ chains, diff --git a/packages/walletconnect-v2/src/index.ts b/packages/walletconnect-v2/src/index.ts index f033bb0ba..506ff2a77 100644 --- a/packages/walletconnect-v2/src/index.ts +++ b/packages/walletconnect-v2/src/index.ts @@ -74,14 +74,16 @@ export class WalletConnect extends Connector { constructor({ actions, defaultChainId, options, timeout = DEFAULT_TIMEOUT, onError }: WalletConnectConstructorArgs) { super(actions, onError) - const { rpcMap, rpc, chains, optionalChains, ...rest } = options + const { rpcMap, rpc, ...rest } = options - this.chains = chains - this.optionalChains = optionalChains this.options = rest 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 handleProviderEvents(provider: WalletConnectProvider): WalletConnectProvider { @@ -114,7 +116,7 @@ export class WalletConnect extends Connector { ): Promise { const ethProviderModule = await import('@walletconnect/ethereum-provider') const rpcMap = this.rpcMap ? await getBestUrlMap(this.rpcMap, this.timeout) : undefined - const chainProps = this.getChainProps(desiredChainId) + const chainProps = this.getChainProps(this.chains, this.optionalChains, desiredChainId) this.provider = await ethProviderModule.default.init({ ...this.options, @@ -125,10 +127,14 @@ export class WalletConnect extends Connector { return this.handleProviderEvents(this.provider) } - private getChainProps(desiredChainId: number | undefined = this.defaultChainId): ChainsProps { + private getChainProps( + chains: number[] | ArrayOneOrMore | undefined, + optionalChains: number[] | ArrayOneOrMore | undefined, + desiredChainId: number | undefined = this.defaultChainId + ): ChainsProps { // Reorder chains and optionalChains if necessary - const orderedChains = getChainsWithDefault(this.chains, desiredChainId) - const orderedOptionalChains = getChainsWithDefault(this.optionalChains, desiredChainId) + const orderedChains = getChainsWithDefault(chains, desiredChainId) + const orderedOptionalChains = getChainsWithDefault(optionalChains, desiredChainId) // Validate and return the result if (isArrayOneOrMore(orderedChains)) {