Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Frankfurt committed Jul 5, 2023
1 parent 1e6ec37 commit 0268a71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/cosmos/useProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MetaMask } from '@web3-react/metamask'
import { Connector } from '@web3-react/types'
import { WalletConnect } from '@web3-react/walletconnect-v2'
import { L1_CHAIN_IDS, L2_CHAIN_IDS, SupportedChainId } from 'constants/chains'
import { JSON_RPC_FALLBACK_ENDPOINTS } from 'constants/jsonRpcEndpoints'
import { useEffect, useState } from 'react'

import useOption from './useOption'
Expand All @@ -19,6 +20,10 @@ const [walletConnect] = initializeConnector<WalletConnect>(
new WalletConnect({
actions,
options: {
rpcMap: Object.entries(JSON_RPC_FALLBACK_ENDPOINTS).reduce((rpcMap, [chainId, rpcUrls]) => ({
...rpcMap,
[chainId]: rpcUrls.slice(0, 1),
})),
showQrModal: true,
projectId: WALLET_CONNECT_PROJECT_ID,
chains: [SupportedChainId.MAINNET],
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/web3/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function useWeb3ReactConnectors({ defaultChainId, provider, jsonRpcUrlMap }: Pro
showQrModal: true,
},
defaultChainId,
onError: console.warn,
onError: console.error,
}),
[defaultChainId, walletConnectDefaultOptions]
)
Expand All @@ -191,7 +191,7 @@ function useWeb3ReactConnectors({ defaultChainId, provider, jsonRpcUrlMap }: Pro
showQrModal: false,
},
defaultChainId,
onError: console.warn,
onError: console.error,
}),
[defaultChainId, walletConnectDefaultOptions]
)
Expand Down
18 changes: 15 additions & 3 deletions src/utils/WalletConnect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'setimmediate'

import { URI_AVAILABLE, WalletConnect, WalletConnectConstructorArgs } from '@web3-react/walletconnect-v2'
import QRCode from 'qrcode'

Expand All @@ -14,21 +16,31 @@ export class WalletConnectPopup extends WalletConnect {

export class WalletConnectQR extends WalletConnect {
static SVG_AVAILABLE = 'svg_available'

svg?: string
constructor(config: WalletConnectConstructorArgs) {
super(config)

constructor({ actions, options, defaultChainId, timeout, onError }: WalletConnectConstructorArgs) {
super({ actions, options: { ...options, showQrModal: false }, defaultChainId, timeout, onError })

this.events.once(URI_AVAILABLE, () => {
this.provider?.events.on('disconnect', this.deactivate)
})

this.events.on(URI_AVAILABLE, async (uri) => {
this.svg = undefined
if (!uri) return

this.svg = await QRCode.toString(uri, {
// Leave a margin to increase contrast in dark mode.
margin: 1,
// Use 55*2=110 for the width to prevent distortion. The generated viewbox is "0 0 55 55".
width: 110,
type: 'svg',
})
this.events.emit(WalletConnectQR.SVG_AVAILABLE, this.svg)
})
this.events.emit(WalletConnectQR.SVG_AVAILABLE, this.svg)
}

deactivate() {
this.events.emit(URI_AVAILABLE)
return super.deactivate()
Expand Down

0 comments on commit 0268a71

Please sign in to comment.