Skip to content

Commit

Permalink
fix: use staticNetwork option in JsonRpcProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
schmanu committed Feb 6, 2024
1 parent 57f23f5 commit 74a003a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/hooks/wallets/useInitWeb3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export const useInitWeb3 = () => {
}, [wallet, chainId])

useEffect(() => {
if (!rpcUri || !chainId) {
if (!rpcUri) {
setWeb3ReadOnly(undefined)
return
}
const web3ReadOnly = createWeb3ReadOnly(chainId, rpcUri, customRpcUrl)
const web3ReadOnly = createWeb3ReadOnly(chain, rpcUri, customRpcUrl)
setWeb3ReadOnly(web3ReadOnly)
}, [chainId, rpcUri, customRpcUrl])
}, [chain, rpcUri, customRpcUrl])
}
17 changes: 10 additions & 7 deletions src/hooks/wallets/web3.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { RPC_AUTHENTICATION, type RpcUri } from '@safe-global/safe-gateway-typescript-sdk'
import { type ChainInfo, RPC_AUTHENTICATION, type RpcUri } from '@safe-global/safe-gateway-typescript-sdk'
import { INFURA_TOKEN, SAFE_APPS_INFURA_TOKEN } from '@/config/constants'
import { type JsonRpcProvider, BrowserProvider, type Eip1193Provider, type Provider } from 'ethers'
import { JsonRpcProvider, BrowserProvider, type Eip1193Provider, type Provider, Network } from 'ethers'
import ExternalStore from '@/services/ExternalStore'
import { EMPTY_DATA } from '@safe-global/protocol-kit/dist/src/utils/constants'
import ReadonlyRpcProvider from '@/utils/providers/ReadonlyRpcProvider'

// RPC helpers
const formatRpcServiceUrl = ({ authentication, value }: RpcUri, token: string): string => {
Expand All @@ -22,27 +21,31 @@ export const getRpcServiceUrl = (rpcUri: RpcUri): string => {
}

export const createWeb3ReadOnly = (
chainId: string,
chain: ChainInfo,
rpcUri: RpcUri,
customRpc?: string,
): JsonRpcProvider | undefined => {
const url = customRpc || getRpcServiceUrl(rpcUri)
if (!url) return
return new ReadonlyRpcProvider(chainId, url)
return new JsonRpcProvider(url, new Network(chain.chainName, chain.chainId), {
staticNetwork: true,
})
}

export const createWeb3 = (walletProvider: Eip1193Provider): BrowserProvider => {
return new BrowserProvider(walletProvider)
}

export const createSafeAppsWeb3Provider = (
chainId: string,
chain: ChainInfo,
safeAppsRpcUri: RpcUri,
customRpc?: string,
): JsonRpcProvider | undefined => {
const url = customRpc || formatRpcServiceUrl(safeAppsRpcUri, SAFE_APPS_INFURA_TOKEN)
if (!url) return
return new ReadonlyRpcProvider(chainId, url)
return new JsonRpcProvider(url, new Network(chain.chainName, chain.chainId), {
staticNetwork: true,
})
}

export const { setStore: setWeb3, useStore: useWeb3 } = new ExternalStore<BrowserProvider>()
Expand Down
23 changes: 0 additions & 23 deletions src/utils/providers/ReadonlyRpcProvider.ts

This file was deleted.

0 comments on commit 74a003a

Please sign in to comment.