Skip to content

Commit

Permalink
pr review
Browse files Browse the repository at this point in the history
  • Loading branch information
JFrankfurt committed Aug 2, 2023
1 parent 2972b90 commit 4fad2fd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/walletconnect-v2/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ describe('WalletConnect', () => {
expect(store.getState().chainId).toEqual(2)
})

test.skip('should throw an error when activating with an unknown chain', async () => {
test('should throw an error when activating with an unknown chain', async () => {
const { connector } = createTestEnvironment({ chains })
await expect(connector.activate(99)).rejects.toThrow()
})

test.skip('should throw an error when using optional chain as default', async () => {
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()
})
Expand Down
32 changes: 14 additions & 18 deletions packages/walletconnect-v2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,25 @@ export class WalletConnect extends Connector {
public provider?: WalletConnectProvider
public readonly events = new EventEmitter3()

private readonly defaultChainId?: number
private eagerConnection?: Promise<WalletConnectProvider>
private readonly options: Omit<WalletConnectOptions, 'rpcMap'>
private readonly options: Omit<WalletConnectOptions, 'rpcMap' | 'chains'>

private readonly rpcMap?: Record<number, string | string[]>
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, defaultChainId, options, timeout = DEFAULT_TIMEOUT, onError }: WalletConnectConstructorArgs) {
super(actions, onError)

const { rpcMap, rpc, ...rest } = options
this.defaultChainId = defaultChainId
const { rpcMap, rpc, chains, optionalChains, ...rest } = options

this.chains = chains
this.optionalChains = optionalChains
this.options = rest
this.defaultChainId = defaultChainId
this.rpcMap = rpcMap || rpc
this.timeout = timeout
}
Expand Down Expand Up @@ -118,21 +125,10 @@ export class WalletConnect extends Connector {
return this.handleProviderEvents(this.provider)
}

// Helper function to reorder chains array based on desiredChainId
private reorderChainsBasedOnId(
chains: number[] | undefined,
desiredChainId: number | undefined
): number[] | undefined {
if (!chains || !desiredChainId || !chains.includes(desiredChainId) || chains.length === 0) {
return chains
}
return getChainsWithDefault(chains, desiredChainId)
}

private getChainProps(desiredChainId: number | undefined = this.defaultChainId): ChainsProps {
// Reorder chains and optionalChains if necessary
const orderedChains = this.reorderChainsBasedOnId(this.options.chains, desiredChainId)
const orderedOptionalChains = this.reorderChainsBasedOnId(this.options.optionalChains, desiredChainId)
const orderedChains = getChainsWithDefault(this.chains, desiredChainId)
const orderedOptionalChains = getChainsWithDefault(this.optionalChains, desiredChainId)

// Validate and return the result
if (isArrayOneOrMore(orderedChains)) {
Expand Down
8 changes: 7 additions & 1 deletion packages/walletconnect-v2/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,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(
Expand Down

0 comments on commit 4fad2fd

Please sign in to comment.