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

Commit

Permalink
fix: throttle balance syncer + remove WC patch (#3848)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook authored May 4, 2022
1 parent 82f97ff commit 23eeb7a
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 266 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"abi-decoder": "^2.4.0",
"axios": "0.21.4",
"bignumber.js": "9.0.1",
"bnc-onboard": "^1.37.3",
"bnc-onboard": "^1.38.2",
"classnames": "^2.2.6",
"currency-flags": "3.2.1",
"date-fns": "^2.20.2",
Expand Down
26 changes: 26 additions & 0 deletions patches/bnc-onboard+1.38.2.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git a/node_modules/bnc-onboard/dist/cjs/onboard-d16018d5.js b/node_modules/bnc-onboard/dist/cjs/onboard-d16018d5.js
index df8c3c3..de97415 100644
--- a/node_modules/bnc-onboard/dist/cjs/onboard-d16018d5.js
+++ b/node_modules/bnc-onboard/dist/cjs/onboard-d16018d5.js
@@ -2536,7 +2536,7 @@ function initializeStores() {
balance = get_store_value(app).dappId ? createBalanceStore(null) : createWalletStateSliceStore({
parameter: 'balance',
initialState: null,
- intervalSetting: 1000
+ intervalSetting: 1000 * 60 * 60
});
wallet = writable({
name: null,
diff --git a/node_modules/bnc-onboard/dist/esm/onboard-59ef9a1c.js b/node_modules/bnc-onboard/dist/esm/onboard-59ef9a1c.js
index 92a3dc6..3c877f2 100644
--- a/node_modules/bnc-onboard/dist/esm/onboard-59ef9a1c.js
+++ b/node_modules/bnc-onboard/dist/esm/onboard-59ef9a1c.js
@@ -2502,7 +2502,7 @@ function initializeStores() {
balance = get_store_value(app).dappId ? createBalanceStore(null) : createWalletStateSliceStore({
parameter: 'balance',
initialState: null,
- intervalSetting: 1000
+ intervalSetting: 1000 * 60 * 60
});
wallet = writable({
name: null,
3 changes: 3 additions & 0 deletions src/logic/wallets/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ 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
50 changes: 42 additions & 8 deletions src/logic/wallets/pairing/module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { IClientMeta } from '@walletconnect/types'
import WalletConnectProvider from '@walletconnect/web3-provider'
import { IClientMeta, IRPCMap } from '@walletconnect/types'
import { WalletModule } from 'bnc-onboard/dist/src/interfaces'
import UAParser from 'ua-parser-js'

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

// 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 @@ -33,22 +36,34 @@ const getClientMeta = (): IClientMeta => {
}
}

// Note: this shares a lot of similarities with the patchedWalletConnect module
const getPairingModule = (chainId: ChainId): WalletModule => {
const STORAGE_ID = 'SAFE__pairingProvider'
const clientMeta = getClientMeta()

return {
name: PAIRING_MODULE_NAME,
wallet: async ({ resetWalletState }) => {
const provider = getWalletConnectProvider(chainId, {
const RPC_MAP: IRPCMap = getChains().reduce((map, { chainId, rpcUri }) => {
return {
...map,
[parseInt(chainId, 10)]: getRpcServiceUrl(rpcUri),
}
}, {})

const provider = new WalletConnectProvider({
bridge: WC_BRIDGE,
pollingInterval: BLOCK_POLLING_INTERVAL,
infuraId: INFURA_TOKEN,
rpc: RPC_MAP,
chainId: parseInt(chainId, 10),
storageId: STORAGE_ID,
qrcode: false, // Don't show QR modal
clientMeta,
})

// WalletConnect overrides the clientMeta, so we need to set it back
;(provider.wc as any).clientMeta = clientMeta
provider.autoRefreshOnNetworkChange = false

// WalletConnect overrides the clientMeta, so we need to set it back(provider.wc as any).clientMeta = clientMeta
;(provider.wc as any)._clientMeta = clientMeta

const onDisconnect = () => {
Expand All @@ -65,7 +80,26 @@ const getPairingModule = (chainId: ChainId): WalletModule => {
return {
provider,
interface: {
...getWCWalletInterface(provider),
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 request balance from onboard
balance: {},
disconnect: () => {
// Only disconnect if connected
if (provider.wc.peerId) {
provider.disconnect()
}
},
name: PAIRING_MODULE_NAME,
},
}
Expand Down
106 changes: 0 additions & 106 deletions src/logic/wallets/patchedWalletConnect.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/logic/wallets/store/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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'

Expand Down Expand Up @@ -78,7 +77,7 @@ const providerMiddleware =
if (account === checksumAddress(address) && !shouldSwitchNetwork(wallet)) {
trackEvent({ ...WALLET_EVENTS.CONNECT, label: name })
// Track WalletConnect peer wallet
if (name === WALLET_CONNECT_MODULE_NAME) {
if (name.toUpperCase() === 'WALLETCONNECT') {
trackEvent({
...WALLET_EVENTS.WALLET_CONNECT,
label: (wallet.provider as InstanceType<typeof WalletConnectProvider>)?.wc?.peerMeta?.name || UNKNOWN_PEER,
Expand Down
24 changes: 14 additions & 10 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 } from 'src/utils/constants'
import { FORTMATIC_KEY, PORTIS_ID, WC_BRIDGE } from 'src/utils/constants'
import getPairingModule from 'src/logic/wallets/pairing/module'
import { isPairingSupported } from 'src/logic/wallets/pairing/utils'
import getPatchedWCModule from 'src/logic/wallets/walletConnect/module'
import { getChains } from 'src/config/cache/chains'

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

return [
{ walletName: WALLETS.METAMASK, preferred: true, desktop: false },
// A patched version of WalletConnect is spliced in at this index
// { preferred: true, desktop: true }
{
walletName: WALLETS.WALLET_CONNECT,
rpc: getChains().reduce((map, { chainId, rpcUri }) => {
return {
...map,
[chainId]: getRpcServiceUrl(rpcUri),
}
}, {}),
bridge: WC_BRIDGE,
preferred: true,
desktop: true,
},
{
walletName: WALLETS.TREZOR,
appUrl: 'gnosis-safe.io',
Expand Down Expand Up @@ -86,12 +96,6 @@ 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(chainId), ...supportedWallets] : supportedWallets
}
60 changes: 0 additions & 60 deletions src/logic/wallets/walletConnect/module.ts

This file was deleted.

Loading

0 comments on commit 23eeb7a

Please sign in to comment.