From 56993904cff366ffa56e4cc5dc9f3cba50c0cd7b Mon Sep 17 00:00:00 2001 From: Norbi <37236152+KatunaNorbert@users.noreply.github.com> Date: Thu, 16 Sep 2021 20:44:34 +0300 Subject: [PATCH] Error when switching to a network that is not added in Metamask (#846) * use nodeUri instead of providerUri * get rpc node from query * remove ocean config * fix and complete data for new network config --- .../molecules/WalletNetworkSwitcher.tsx | 9 ++---- src/hooks/useNetworkMetadata.ts | 4 +++ src/utils/web3.ts | 30 ++++++++----------- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/components/molecules/WalletNetworkSwitcher.tsx b/src/components/molecules/WalletNetworkSwitcher.tsx index d3563873d..309062776 100644 --- a/src/components/molecules/WalletNetworkSwitcher.tsx +++ b/src/components/molecules/WalletNetworkSwitcher.tsx @@ -2,20 +2,17 @@ import React, { ReactElement } from 'react' import { useWeb3 } from '../../providers/Web3' import { addCustomNetwork, - getNetworkConfigObject, getNetworkDisplayName, getNetworkDataById } from '../../utils/web3' import Button from '../atoms/Button' import styles from './WalletNetworkSwitcher.module.css' import useNetworkMetadata from '../../hooks/useNetworkMetadata' -import { getOceanConfig } from '../../utils/ocean' import { useAsset } from '../../providers/Asset' export default function WalletNetworkSwitcher(): ReactElement { const { networkId, web3Provider } = useWeb3() const { ddo } = useAsset() - const oceanConfig = getOceanConfig(ddo.chainId) const { networksList } = useNetworkMetadata() const ddoNetworkData = getNetworkDataById(networksList, ddo.chainId) const walletNetworkData = getNetworkDataById(networksList, networkId) @@ -28,12 +25,10 @@ export default function WalletNetworkSwitcher(): ReactElement { ) async function switchWalletNetwork() { - const networkNode = networksList.find( + const networkNode = await networksList.find( (data) => data.node.chainId === ddo.chainId ).node - const network = { ...networkNode, providerUri: oceanConfig.providerUri } - const networkConfig = getNetworkConfigObject(network) - addCustomNetwork(web3Provider, networkConfig) + addCustomNetwork(web3Provider, networkNode) } return ( diff --git a/src/hooks/useNetworkMetadata.ts b/src/hooks/useNetworkMetadata.ts index 78bd9b3bd..6e049382a 100644 --- a/src/hooks/useNetworkMetadata.ts +++ b/src/hooks/useNetworkMetadata.ts @@ -14,6 +14,10 @@ const networksQuery = graphql` network networkId chainId + rpc + explorers { + url + } nativeCurrency { name symbol diff --git a/src/utils/web3.ts b/src/utils/web3.ts index 6e8986eed..6a40ca140 100644 --- a/src/utils/web3.ts +++ b/src/utils/web3.ts @@ -10,19 +10,22 @@ export interface EthereumListsChain { networkId: number nativeCurrency: { name: string; symbol: string; decimals: number } rpc: string[] - faucets: string[] infoURL: string + faucets: string[] + explorers: [{ url: string }] } export interface NetworkObject { chainId: number name: string + nativeCurrency: string + explorers: [{ url: string }] urlList: string[] } const configGaiaX = getOceanConfig(2021000) -export const networkDataGaiaX = { +export const networkDataGaiaX: EthereumListsChain = { name: 'GAIA-X Testnet', chainId: 2021000, shortName: 'GAIA-X', @@ -35,17 +38,8 @@ export const networkDataGaiaX = { 'https://faucet.gaiaxtestnet.oceanprotocol.com/', 'https://faucet.gx.gaiaxtestnet.oceanprotocol.com/' ], - infoURL: 'https://www.gaia-x.eu' -} - -export function getNetworkConfigObject(node: any): NetworkObject { - const networkConfig = { - name: node.chain, - symbol: node.nativeCurrency.symbol, - chainId: node.chainId, - urlList: [node.providerUri] - } - return networkConfig + infoURL: 'https://www.gaia-x.eu', + explorers: [{ url: '' }] } export function accountTruncate(account: string): string { @@ -98,12 +92,14 @@ export function getNetworkDataById( export async function addCustomNetwork( web3Provider: any, - network: NetworkObject + network: EthereumListsChain ): Promise { const newNewtworkData = { chainId: `0x${network.chainId.toString(16)}`, - chainName: network.name, - rpcUrls: network.urlList + chainName: getNetworkDisplayName(network, network.chainId), + nativeCurrency: network.nativeCurrency, + rpcUrls: network.rpc, + blockExplorerUrls: [network.explorers[0].url] } try { await web3Provider.request({ @@ -112,7 +108,7 @@ export async function addCustomNetwork( }) } catch (switchError) { if (switchError.code === 4902) { - web3Provider.request( + await web3Provider.request( { method: 'wallet_addEthereumChain', params: [newNewtworkData]