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

Commit f9ad409

Browse files
committed
Revert "fix: throttle balance state syncer (#3800)"
This reverts commit e4dd1b6.
1 parent 1657e5f commit f9ad409

File tree

8 files changed

+246
-85
lines changed

8 files changed

+246
-85
lines changed

patches/bnc-onboard+1.38.2.patch

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/logic/wallets/onboard.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,10 @@ const hasENSSupport = (chainId: ChainId): boolean => {
4848
return getChains().some((chain) => chain.chainId === chainId && chain.features.includes(FEATURES.DOMAIN_LOOKUP))
4949
}
5050

51-
export const BLOCK_POLLING_INTERVAL = 1000 * 60 * 60 // 1 hour
52-
5351
const getOnboard = (chainId: ChainId): API => {
5452
const config: Initialization = {
5553
networkId: parseInt(chainId, 10),
5654
networkName: getNetworkName(chainId),
57-
blockPollingInterval: BLOCK_POLLING_INTERVAL,
5855
subscriptions: {
5956
wallet: async (wallet) => {
6057
store.dispatch(updateProviderWallet(wallet.name || ''))

src/logic/wallets/pairing/module.ts

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { IClientMeta } from '@walletconnect/types'
33
import { WalletModule } from 'bnc-onboard/dist/src/interfaces'
44
import UAParser from 'ua-parser-js'
55

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

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

3837
const createPairingProvider = (): WalletConnectProvider => {
3938
const STORAGE_ID = 'SAFE__pairingProvider'
40-
41-
const rpc = getChains().reduce((map, { chainId, rpcUri }) => {
42-
return {
43-
...map,
44-
[parseInt(chainId, 10)]: getRpcServiceUrl(rpcUri),
45-
}
46-
}, {})
4739
const clientMeta = getClientMeta()
4840

49-
const provider = new WalletConnectProvider({
50-
bridge: WC_BRIDGE,
51-
pollingInterval: BLOCK_POLLING_INTERVAL,
52-
infuraId: INFURA_TOKEN,
53-
rpc,
54-
chainId: parseInt(_getChainId(), 10),
41+
// Successful pairing does not use chainId of provider but that of the pairee
42+
// so we can use any chainId here
43+
const provider = getWalletConnectProvider(_getChainId(), {
5544
storageId: STORAGE_ID,
5645
qrcode: false, // Don't show QR modal
5746
clientMeta,
@@ -61,8 +50,6 @@ const createPairingProvider = (): WalletConnectProvider => {
6150
;(provider.wc as any).clientMeta = clientMeta
6251
;(provider.wc as any)._clientMeta = clientMeta
6352

64-
provider.autoRefreshOnNetworkChange = false
65-
6653
return provider
6754
}
6855

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

66+
// Note: this shares a lot of similarities with the patchedWalletConnect module
7967
const getPairingModule = (): WalletModule => {
8068
const name = PAIRING_MODULE_NAME
8169
const provider = getPairingProvider()
@@ -97,26 +85,7 @@ const getPairingModule = (): WalletModule => {
9785
return {
9886
provider,
9987
interface: {
100-
address: {
101-
onChange: (func) => {
102-
provider.send('eth_accounts').then((accounts: string[]) => accounts[0] && func(accounts[0]))
103-
provider.on('accountsChanged', (accounts: string[]) => func(accounts[0]))
104-
},
105-
},
106-
network: {
107-
onChange: (func) => {
108-
provider.send('eth_chainId').then(func)
109-
provider.on('chainChanged', func)
110-
},
111-
},
112-
// We never access the balance via onboard
113-
balance: {},
114-
disconnect: () => {
115-
// Only disconnect if connected
116-
if (provider.wc.peerId) {
117-
provider.disconnect()
118-
}
119-
},
88+
...getWCWalletInterface(provider),
12089
name,
12190
},
12291
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import WalletConnectProvider from '@walletconnect/web3-provider'
2+
import { IRPCMap } from '@walletconnect/types'
3+
import { WalletModule, Helpers } from 'bnc-onboard/dist/src/interfaces'
4+
5+
import { getRpcServiceUrl } from 'src/config'
6+
import { getChains } from 'src/config/cache/chains'
7+
import { INFURA_TOKEN, WC_BRIDGE } from 'src/utils/constants'
8+
import { ChainId } from 'src/config/chain'
9+
10+
// Modified version of the built in WC module in Onboard v1.35.5, including:
11+
// https://github.com/blocknative/onboard/blob/release/1.35.5/src/modules/select/wallets/wallet-connect.ts
12+
13+
// - No `balance` subscription as `eth_getBalance` is otherwise constantly requested
14+
// but we do not request the balance from anywhere and is therefore not needed
15+
// - A high polling interval to prevent unnecessary `eth_getBlockByNumber` polling
16+
// https://github.com/WalletConnect/walletconnect-monorepo/issues/357#issuecomment-789663540
17+
18+
const walletConnectIcon = `
19+
<svg
20+
height="25"
21+
viewBox="0 0 40 25"
22+
width="40"
23+
xmlns="http://www.w3.org/2000/svg"
24+
>
25+
<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"
26+
fill="#3b99fc"/>
27+
</svg>
28+
`
29+
30+
export const getRpcMap = (): IRPCMap => {
31+
return getChains().reduce((map, { chainId, rpcUri }) => {
32+
return {
33+
...map,
34+
[parseInt(chainId, 10)]: getRpcServiceUrl(rpcUri),
35+
}
36+
}, {})
37+
}
38+
39+
export const WALLET_CONNECT_MODULE_NAME = 'WalletConnect'
40+
const patchedWalletConnect = (chainId: ChainId): WalletModule => {
41+
return {
42+
name: WALLET_CONNECT_MODULE_NAME,
43+
svg: walletConnectIcon,
44+
wallet: async ({ resetWalletState }: Helpers) => {
45+
const provider = new WalletConnectProvider({
46+
infuraId: INFURA_TOKEN,
47+
rpc: getRpcMap(),
48+
chainId: parseInt(chainId, 10),
49+
bridge: WC_BRIDGE,
50+
// Prevent `eth_getBlockByNumber` polling every 4 seconds
51+
pollingInterval: 60_000 * 60, // 1 hour
52+
})
53+
54+
provider.autoRefreshOnNetworkChange = false
55+
56+
provider.wc.on('disconnect', () => {
57+
resetWalletState({ disconnected: true, walletName: WALLET_CONNECT_MODULE_NAME })
58+
})
59+
60+
return {
61+
provider,
62+
interface: {
63+
name: WALLET_CONNECT_MODULE_NAME,
64+
connect: () =>
65+
new Promise((resolve, reject) => {
66+
provider
67+
.enable()
68+
.then(resolve)
69+
.catch(() =>
70+
reject({
71+
message: 'This dapp needs access to your account information.',
72+
}),
73+
)
74+
}),
75+
address: {
76+
onChange: (func) => {
77+
provider.send('eth_accounts').then((accounts: string[]) => accounts[0] && func(accounts[0]))
78+
provider.on('accountsChanged', (accounts: string[]) => func(accounts[0]))
79+
},
80+
},
81+
network: {
82+
onChange: (func) => {
83+
provider.send('eth_chainId').then(func)
84+
provider.on('chainChanged', func)
85+
},
86+
},
87+
// Prevent continuous `eth_getBalance` requests
88+
balance: {},
89+
disconnect: () => {
90+
provider.wc.killSession()
91+
provider.stop()
92+
},
93+
},
94+
}
95+
},
96+
type: 'sdk',
97+
desktop: true,
98+
mobile: true,
99+
preferred: true,
100+
}
101+
}
102+
103+
export default patchedWalletConnect

src/logic/wallets/store/middleware/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ import { WALLET_EVENTS } from 'src/utils/events/wallet'
1313
import { instantiateSafeContracts } from 'src/logic/contracts/safeContracts'
1414
import { resetWeb3, setWeb3 } from 'src/logic/wallets/getWeb3'
1515
import onboard, { removeLastUsedProvider, saveLastUsedProvider } from 'src/logic/wallets/onboard'
16+
import { WALLET_CONNECT_MODULE_NAME } from 'src/logic/wallets/patchedWalletConnect'
1617
import { checksumAddress } from 'src/utils/checksumAddress'
1718
import { shouldSwitchNetwork } from 'src/logic/wallets/utils/network'
1819

1920
const UNKNOWN_PEER = 'Unknown'
2021

21-
// https://github.com/blocknative/web3-onboard/blob/1cfcd6fb6f7b599f01b51ef56a2ec38250854938/src/modules/select/wallets/wallet-connect.ts#L30
22-
const WALLET_CONNECT_MODULE_NAME = 'WalletConnect'
23-
2422
const providerMiddleware =
2523
(store: ReturnType<typeof reduxStore>) =>
2624
(next: Dispatch) =>

src/logic/wallets/utils/walletList.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { WalletInitOptions, WalletModule, WalletSelectModuleOptions } from 'bnc-
22

33
import { getRpcServiceUrl, getDisabledWallets, getChainById } from 'src/config'
44
import { ChainId, WALLETS } from 'src/config/chain.d'
5-
import { FORTMATIC_KEY, PORTIS_ID, WC_BRIDGE } from 'src/utils/constants'
5+
import { FORTMATIC_KEY, PORTIS_ID } from 'src/utils/constants'
66
import getPairingModule from 'src/logic/wallets/pairing/module'
77
import { isPairingSupported } from 'src/logic/wallets/pairing/utils'
8-
import { getChains } from 'src/config/cache/chains'
8+
import getPatchedWCModule from 'src/logic/wallets/walletConnect/module'
99

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

2020
return [
2121
{ walletName: WALLETS.METAMASK, preferred: true, desktop: false },
22-
{
23-
walletName: WALLETS.WALLET_CONNECT,
24-
rpc: getChains().reduce((map, { chainId, rpcUri }) => {
25-
return {
26-
...map,
27-
[chainId]: getRpcServiceUrl(rpcUri),
28-
}
29-
}, {}),
30-
bridge: WC_BRIDGE,
31-
preferred: true,
32-
desktop: true,
33-
},
22+
// A patched version of WalletConnect is spliced in at this index
23+
// { preferred: true, desktop: true }
3424
{
3525
walletName: WALLETS.TREZOR,
3626
appUrl: 'gnosis-safe.io',
@@ -96,6 +86,12 @@ export const getSupportedWallets = (chainId: ChainId): WalletSelectModuleOptions
9686
})
9787
.map(({ desktop: _, ...rest }) => rest)
9888

89+
if (isSupportedWallet(WALLETS.WALLET_CONNECT)) {
90+
const wc = getPatchedWCModule(chainId)
91+
// Inset patched WC module at index 1
92+
supportedWallets?.splice(1, 0, wc)
93+
}
94+
9995
// Pairing must be 1st in list (to hide via CSS)
10096
return isPairingSupported() ? [getPairingModule(), ...supportedWallets] : supportedWallets
10197
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { WalletModule, Helpers } from 'bnc-onboard/dist/src/interfaces'
2+
3+
import { ChainId } from 'src/config/chain'
4+
import { getWCWalletInterface, getWalletConnectProvider } from 'src/logic/wallets/walletConnect/utils'
5+
6+
const walletConnectIcon = `
7+
<svg
8+
height="25"
9+
viewBox="0 0 40 25"
10+
width="40"
11+
xmlns="http://www.w3.org/2000/svg"
12+
>
13+
<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"
14+
fill="#3b99fc"/>
15+
</svg>
16+
`
17+
18+
// Modified version of the built in WC module in Onboard v1.35.5, including:
19+
// https://github.com/blocknative/onboard/blob/release/1.35.5/src/modules/select/wallets/wallet-connect.ts
20+
21+
const getPatchedWCModule = (chainId: ChainId): WalletModule => {
22+
const MODULE_NAME = 'WalletConnect'
23+
24+
return {
25+
name: MODULE_NAME,
26+
svg: walletConnectIcon,
27+
wallet: async ({ resetWalletState }: Helpers) => {
28+
const provider = getWalletConnectProvider(chainId)
29+
30+
provider.wc.on('disconnect', () => {
31+
resetWalletState({ disconnected: true, walletName: MODULE_NAME })
32+
})
33+
34+
return {
35+
provider,
36+
interface: {
37+
...getWCWalletInterface(provider),
38+
name: MODULE_NAME,
39+
connect: () =>
40+
new Promise((resolve, reject) => {
41+
provider
42+
.enable()
43+
.then(resolve)
44+
.catch(() =>
45+
reject({
46+
message: 'This dapp needs access to your account information.',
47+
}),
48+
)
49+
}),
50+
},
51+
}
52+
},
53+
type: 'sdk',
54+
desktop: true,
55+
mobile: true,
56+
preferred: true,
57+
}
58+
}
59+
60+
export default getPatchedWCModule

0 commit comments

Comments
 (0)