Skip to content

Commit

Permalink
refactor to validate chainProps on init
Browse files Browse the repository at this point in the history
  • Loading branch information
JFrankfurt committed Aug 2, 2023
1 parent 4fad2fd commit 9bd1a89
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions packages/walletconnect-v2/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 13 additions & 7 deletions packages/walletconnect-v2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -114,7 +116,7 @@ export class WalletConnect extends Connector {
): Promise<WalletConnectProvider> {
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,
Expand All @@ -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<number> | undefined,
optionalChains: number[] | ArrayOneOrMore<number> | 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)) {
Expand Down

0 comments on commit 9bd1a89

Please sign in to comment.