Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Revert "fix: throttle balance state syncer (#3800)"
Browse files Browse the repository at this point in the history
This reverts commit e4dd1b6.
  • Loading branch information
katspaugh committed Apr 27, 2022
1 parent 1657e5f commit f9ad409
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 85 deletions.
26 changes: 0 additions & 26 deletions patches/bnc-onboard+1.38.2.patch

This file was deleted.

3 changes: 0 additions & 3 deletions src/logic/wallets/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,10 @@ const hasENSSupport = (chainId: ChainId): boolean => {
return getChains().some((chain) => chain.chainId === chainId && chain.features.includes(FEATURES.DOMAIN_LOOKUP))
}

export const BLOCK_POLLING_INTERVAL = 1000 * 60 * 60 // 1 hour

const getOnboard = (chainId: ChainId): API => {
const config: Initialization = {
networkId: parseInt(chainId, 10),
networkName: getNetworkName(chainId),
blockPollingInterval: BLOCK_POLLING_INTERVAL,
subscriptions: {
wallet: async (wallet) => {
store.dispatch(updateProviderWallet(wallet.name || ''))
Expand Down
47 changes: 8 additions & 39 deletions src/logic/wallets/pairing/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { IClientMeta } from '@walletconnect/types'
import { WalletModule } from 'bnc-onboard/dist/src/interfaces'
import UAParser from 'ua-parser-js'

import { APP_VERSION, INFURA_TOKEN, PUBLIC_URL, WC_BRIDGE } from 'src/utils/constants'
import { getRpcServiceUrl, _getChainId } from 'src/config'
import { getChains } from 'src/config/cache/chains'
import { BLOCK_POLLING_INTERVAL } from '../onboard'
import { APP_VERSION, PUBLIC_URL } from 'src/utils/constants'
import { getWCWalletInterface, getWalletConnectProvider } from 'src/logic/wallets/walletConnect/utils'
import { _getChainId } from 'src/config'

// Modified version of the built in WC module in Onboard v1.35.5
// https://github.com/blocknative/onboard/blob/release/1.35.5/src/modules/select/wallets/wallet-connect.ts
Expand Down Expand Up @@ -37,21 +36,11 @@ const getClientMeta = (): IClientMeta => {

const createPairingProvider = (): WalletConnectProvider => {
const STORAGE_ID = 'SAFE__pairingProvider'

const rpc = getChains().reduce((map, { chainId, rpcUri }) => {
return {
...map,
[parseInt(chainId, 10)]: getRpcServiceUrl(rpcUri),
}
}, {})
const clientMeta = getClientMeta()

const provider = new WalletConnectProvider({
bridge: WC_BRIDGE,
pollingInterval: BLOCK_POLLING_INTERVAL,
infuraId: INFURA_TOKEN,
rpc,
chainId: parseInt(_getChainId(), 10),
// Successful pairing does not use chainId of provider but that of the pairee
// so we can use any chainId here
const provider = getWalletConnectProvider(_getChainId(), {
storageId: STORAGE_ID,
qrcode: false, // Don't show QR modal
clientMeta,
Expand All @@ -61,8 +50,6 @@ const createPairingProvider = (): WalletConnectProvider => {
;(provider.wc as any).clientMeta = clientMeta
;(provider.wc as any)._clientMeta = clientMeta

provider.autoRefreshOnNetworkChange = false

return provider
}

Expand All @@ -76,6 +63,7 @@ export const getPairingProvider = (): WalletConnectProvider => {
return _pairingProvider
}

// Note: this shares a lot of similarities with the patchedWalletConnect module
const getPairingModule = (): WalletModule => {
const name = PAIRING_MODULE_NAME
const provider = getPairingProvider()
Expand All @@ -97,26 +85,7 @@ const getPairingModule = (): WalletModule => {
return {
provider,
interface: {
address: {
onChange: (func) => {
provider.send('eth_accounts').then((accounts: string[]) => accounts[0] && func(accounts[0]))
provider.on('accountsChanged', (accounts: string[]) => func(accounts[0]))
},
},
network: {
onChange: (func) => {
provider.send('eth_chainId').then(func)
provider.on('chainChanged', func)
},
},
// We never access the balance via onboard
balance: {},
disconnect: () => {
// Only disconnect if connected
if (provider.wc.peerId) {
provider.disconnect()
}
},
...getWCWalletInterface(provider),
name,
},
}
Expand Down
103 changes: 103 additions & 0 deletions src/logic/wallets/patchedWalletConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import WalletConnectProvider from '@walletconnect/web3-provider'
import { IRPCMap } from '@walletconnect/types'
import { WalletModule, Helpers } from 'bnc-onboard/dist/src/interfaces'

import { getRpcServiceUrl } from 'src/config'
import { getChains } from 'src/config/cache/chains'
import { INFURA_TOKEN, WC_BRIDGE } from 'src/utils/constants'
import { ChainId } from 'src/config/chain'

// Modified version of the built in WC module in Onboard v1.35.5, including:
// https://github.com/blocknative/onboard/blob/release/1.35.5/src/modules/select/wallets/wallet-connect.ts

// - No `balance` subscription as `eth_getBalance` is otherwise constantly requested
// but we do not request the balance from anywhere and is therefore not needed
// - A high polling interval to prevent unnecessary `eth_getBlockByNumber` polling
// https://github.com/WalletConnect/walletconnect-monorepo/issues/357#issuecomment-789663540

const walletConnectIcon = `
<svg
height="25"
viewBox="0 0 40 25"
width="40"
xmlns="http://www.w3.org/2000/svg"
>
<path d="m8.19180572 4.83416816c6.52149658-6.38508884 17.09493158-6.38508884 23.61642788 0l.7848727.76845565c.3260748.31925442.3260748.83686816 0 1.15612272l-2.6848927 2.62873374c-.1630375.15962734-.4273733.15962734-.5904108 0l-1.0800779-1.05748639c-4.5495589-4.45439756-11.9258514-4.45439756-16.4754105 0l-1.1566741 1.13248068c-.1630376.15962721-.4273735.15962721-.5904108 0l-2.68489263-2.62873375c-.32607483-.31925456-.32607483-.83686829 0-1.15612272zm29.16903948 5.43649934 2.3895596 2.3395862c.3260732.319253.3260751.8368636.0000041 1.1561187l-10.7746894 10.5494845c-.3260726.3192568-.8547443.3192604-1.1808214.0000083-.0000013-.0000013-.0000029-.0000029-.0000042-.0000043l-7.6472191-7.4872762c-.0815187-.0798136-.2136867-.0798136-.2952053 0-.0000006.0000005-.000001.000001-.0000015.0000014l-7.6470562 7.4872708c-.3260715.3192576-.8547434.319263-1.1808215.0000116-.0000019-.0000018-.0000039-.0000037-.0000059-.0000058l-10.7749893-10.5496247c-.32607469-.3192544-.32607469-.8368682 0-1.1561226l2.38956395-2.3395823c.3260747-.31925446.85474652-.31925446 1.18082136 0l7.64733029 7.4873809c.0815188.0798136.2136866.0798136.2952054 0 .0000012-.0000012.0000023-.0000023.0000035-.0000032l7.6469471-7.4873777c.3260673-.31926181.8547392-.31927378 1.1808214-.0000267.0000046.0000045.0000091.000009.0000135.0000135l7.6473203 7.4873909c.0815186.0798135.2136866.0798135.2952053 0l7.6471967-7.4872433c.3260748-.31925458.8547465-.31925458 1.1808213 0z"
fill="#3b99fc"/>
</svg>
`

export const getRpcMap = (): IRPCMap => {
return getChains().reduce((map, { chainId, rpcUri }) => {
return {
...map,
[parseInt(chainId, 10)]: getRpcServiceUrl(rpcUri),
}
}, {})
}

export const WALLET_CONNECT_MODULE_NAME = 'WalletConnect'
const patchedWalletConnect = (chainId: ChainId): WalletModule => {
return {
name: WALLET_CONNECT_MODULE_NAME,
svg: walletConnectIcon,
wallet: async ({ resetWalletState }: Helpers) => {
const provider = new WalletConnectProvider({
infuraId: INFURA_TOKEN,
rpc: getRpcMap(),
chainId: parseInt(chainId, 10),
bridge: WC_BRIDGE,
// Prevent `eth_getBlockByNumber` polling every 4 seconds
pollingInterval: 60_000 * 60, // 1 hour
})

provider.autoRefreshOnNetworkChange = false

provider.wc.on('disconnect', () => {
resetWalletState({ disconnected: true, walletName: WALLET_CONNECT_MODULE_NAME })
})

return {
provider,
interface: {
name: WALLET_CONNECT_MODULE_NAME,
connect: () =>
new Promise((resolve, reject) => {
provider
.enable()
.then(resolve)
.catch(() =>
reject({
message: 'This dapp needs access to your account information.',
}),
)
}),
address: {
onChange: (func) => {
provider.send('eth_accounts').then((accounts: string[]) => accounts[0] && func(accounts[0]))
provider.on('accountsChanged', (accounts: string[]) => func(accounts[0]))
},
},
network: {
onChange: (func) => {
provider.send('eth_chainId').then(func)
provider.on('chainChanged', func)
},
},
// Prevent continuous `eth_getBalance` requests
balance: {},
disconnect: () => {
provider.wc.killSession()
provider.stop()
},
},
}
},
type: 'sdk',
desktop: true,
mobile: true,
preferred: true,
}
}

export default patchedWalletConnect
4 changes: 1 addition & 3 deletions src/logic/wallets/store/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import { WALLET_EVENTS } from 'src/utils/events/wallet'
import { instantiateSafeContracts } from 'src/logic/contracts/safeContracts'
import { resetWeb3, setWeb3 } from 'src/logic/wallets/getWeb3'
import onboard, { removeLastUsedProvider, saveLastUsedProvider } from 'src/logic/wallets/onboard'
import { WALLET_CONNECT_MODULE_NAME } from 'src/logic/wallets/patchedWalletConnect'
import { checksumAddress } from 'src/utils/checksumAddress'
import { shouldSwitchNetwork } from 'src/logic/wallets/utils/network'

const UNKNOWN_PEER = 'Unknown'

// https://github.com/blocknative/web3-onboard/blob/1cfcd6fb6f7b599f01b51ef56a2ec38250854938/src/modules/select/wallets/wallet-connect.ts#L30
const WALLET_CONNECT_MODULE_NAME = 'WalletConnect'

const providerMiddleware =
(store: ReturnType<typeof reduxStore>) =>
(next: Dispatch) =>
Expand Down
24 changes: 10 additions & 14 deletions src/logic/wallets/utils/walletList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { WalletInitOptions, WalletModule, WalletSelectModuleOptions } from 'bnc-

import { getRpcServiceUrl, getDisabledWallets, getChainById } from 'src/config'
import { ChainId, WALLETS } from 'src/config/chain.d'
import { FORTMATIC_KEY, PORTIS_ID, WC_BRIDGE } from 'src/utils/constants'
import { FORTMATIC_KEY, PORTIS_ID } from 'src/utils/constants'
import getPairingModule from 'src/logic/wallets/pairing/module'
import { isPairingSupported } from 'src/logic/wallets/pairing/utils'
import { getChains } from 'src/config/cache/chains'
import getPatchedWCModule from 'src/logic/wallets/walletConnect/module'

type Wallet = (WalletInitOptions | WalletModule) & {
desktop: boolean // Whether wallet supports desktop app
Expand All @@ -19,18 +19,8 @@ const wallets = (chainId: ChainId): Wallet[] => {

return [
{ walletName: WALLETS.METAMASK, preferred: true, desktop: false },
{
walletName: WALLETS.WALLET_CONNECT,
rpc: getChains().reduce((map, { chainId, rpcUri }) => {
return {
...map,
[chainId]: getRpcServiceUrl(rpcUri),
}
}, {}),
bridge: WC_BRIDGE,
preferred: true,
desktop: true,
},
// A patched version of WalletConnect is spliced in at this index
// { preferred: true, desktop: true }
{
walletName: WALLETS.TREZOR,
appUrl: 'gnosis-safe.io',
Expand Down Expand Up @@ -96,6 +86,12 @@ export const getSupportedWallets = (chainId: ChainId): WalletSelectModuleOptions
})
.map(({ desktop: _, ...rest }) => rest)

if (isSupportedWallet(WALLETS.WALLET_CONNECT)) {
const wc = getPatchedWCModule(chainId)
// Inset patched WC module at index 1
supportedWallets?.splice(1, 0, wc)
}

// Pairing must be 1st in list (to hide via CSS)
return isPairingSupported() ? [getPairingModule(), ...supportedWallets] : supportedWallets
}
60 changes: 60 additions & 0 deletions src/logic/wallets/walletConnect/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { WalletModule, Helpers } from 'bnc-onboard/dist/src/interfaces'

import { ChainId } from 'src/config/chain'
import { getWCWalletInterface, getWalletConnectProvider } from 'src/logic/wallets/walletConnect/utils'

const walletConnectIcon = `
<svg
height="25"
viewBox="0 0 40 25"
width="40"
xmlns="http://www.w3.org/2000/svg"
>
<path d="m8.19180572 4.83416816c6.52149658-6.38508884 17.09493158-6.38508884 23.61642788 0l.7848727.76845565c.3260748.31925442.3260748.83686816 0 1.15612272l-2.6848927 2.62873374c-.1630375.15962734-.4273733.15962734-.5904108 0l-1.0800779-1.05748639c-4.5495589-4.45439756-11.9258514-4.45439756-16.4754105 0l-1.1566741 1.13248068c-.1630376.15962721-.4273735.15962721-.5904108 0l-2.68489263-2.62873375c-.32607483-.31925456-.32607483-.83686829 0-1.15612272zm29.16903948 5.43649934 2.3895596 2.3395862c.3260732.319253.3260751.8368636.0000041 1.1561187l-10.7746894 10.5494845c-.3260726.3192568-.8547443.3192604-1.1808214.0000083-.0000013-.0000013-.0000029-.0000029-.0000042-.0000043l-7.6472191-7.4872762c-.0815187-.0798136-.2136867-.0798136-.2952053 0-.0000006.0000005-.000001.000001-.0000015.0000014l-7.6470562 7.4872708c-.3260715.3192576-.8547434.319263-1.1808215.0000116-.0000019-.0000018-.0000039-.0000037-.0000059-.0000058l-10.7749893-10.5496247c-.32607469-.3192544-.32607469-.8368682 0-1.1561226l2.38956395-2.3395823c.3260747-.31925446.85474652-.31925446 1.18082136 0l7.64733029 7.4873809c.0815188.0798136.2136866.0798136.2952054 0 .0000012-.0000012.0000023-.0000023.0000035-.0000032l7.6469471-7.4873777c.3260673-.31926181.8547392-.31927378 1.1808214-.0000267.0000046.0000045.0000091.000009.0000135.0000135l7.6473203 7.4873909c.0815186.0798135.2136866.0798135.2952053 0l7.6471967-7.4872433c.3260748-.31925458.8547465-.31925458 1.1808213 0z"
fill="#3b99fc"/>
</svg>
`

// Modified version of the built in WC module in Onboard v1.35.5, including:
// https://github.com/blocknative/onboard/blob/release/1.35.5/src/modules/select/wallets/wallet-connect.ts

const getPatchedWCModule = (chainId: ChainId): WalletModule => {
const MODULE_NAME = 'WalletConnect'

return {
name: MODULE_NAME,
svg: walletConnectIcon,
wallet: async ({ resetWalletState }: Helpers) => {
const provider = getWalletConnectProvider(chainId)

provider.wc.on('disconnect', () => {
resetWalletState({ disconnected: true, walletName: MODULE_NAME })
})

return {
provider,
interface: {
...getWCWalletInterface(provider),
name: MODULE_NAME,
connect: () =>
new Promise((resolve, reject) => {
provider
.enable()
.then(resolve)
.catch(() =>
reject({
message: 'This dapp needs access to your account information.',
}),
)
}),
},
}
},
type: 'sdk',
desktop: true,
mobile: true,
preferred: true,
}
}

export default getPatchedWCModule
Loading

0 comments on commit f9ad409

Please sign in to comment.