mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
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
This commit is contained in:
parent
0eb6c15cda
commit
56993904cf
@ -2,20 +2,17 @@ import React, { ReactElement } from 'react'
|
|||||||
import { useWeb3 } from '../../providers/Web3'
|
import { useWeb3 } from '../../providers/Web3'
|
||||||
import {
|
import {
|
||||||
addCustomNetwork,
|
addCustomNetwork,
|
||||||
getNetworkConfigObject,
|
|
||||||
getNetworkDisplayName,
|
getNetworkDisplayName,
|
||||||
getNetworkDataById
|
getNetworkDataById
|
||||||
} from '../../utils/web3'
|
} from '../../utils/web3'
|
||||||
import Button from '../atoms/Button'
|
import Button from '../atoms/Button'
|
||||||
import styles from './WalletNetworkSwitcher.module.css'
|
import styles from './WalletNetworkSwitcher.module.css'
|
||||||
import useNetworkMetadata from '../../hooks/useNetworkMetadata'
|
import useNetworkMetadata from '../../hooks/useNetworkMetadata'
|
||||||
import { getOceanConfig } from '../../utils/ocean'
|
|
||||||
import { useAsset } from '../../providers/Asset'
|
import { useAsset } from '../../providers/Asset'
|
||||||
|
|
||||||
export default function WalletNetworkSwitcher(): ReactElement {
|
export default function WalletNetworkSwitcher(): ReactElement {
|
||||||
const { networkId, web3Provider } = useWeb3()
|
const { networkId, web3Provider } = useWeb3()
|
||||||
const { ddo } = useAsset()
|
const { ddo } = useAsset()
|
||||||
const oceanConfig = getOceanConfig(ddo.chainId)
|
|
||||||
const { networksList } = useNetworkMetadata()
|
const { networksList } = useNetworkMetadata()
|
||||||
const ddoNetworkData = getNetworkDataById(networksList, ddo.chainId)
|
const ddoNetworkData = getNetworkDataById(networksList, ddo.chainId)
|
||||||
const walletNetworkData = getNetworkDataById(networksList, networkId)
|
const walletNetworkData = getNetworkDataById(networksList, networkId)
|
||||||
@ -28,12 +25,10 @@ export default function WalletNetworkSwitcher(): ReactElement {
|
|||||||
)
|
)
|
||||||
|
|
||||||
async function switchWalletNetwork() {
|
async function switchWalletNetwork() {
|
||||||
const networkNode = networksList.find(
|
const networkNode = await networksList.find(
|
||||||
(data) => data.node.chainId === ddo.chainId
|
(data) => data.node.chainId === ddo.chainId
|
||||||
).node
|
).node
|
||||||
const network = { ...networkNode, providerUri: oceanConfig.providerUri }
|
addCustomNetwork(web3Provider, networkNode)
|
||||||
const networkConfig = getNetworkConfigObject(network)
|
|
||||||
addCustomNetwork(web3Provider, networkConfig)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -14,6 +14,10 @@ const networksQuery = graphql`
|
|||||||
network
|
network
|
||||||
networkId
|
networkId
|
||||||
chainId
|
chainId
|
||||||
|
rpc
|
||||||
|
explorers {
|
||||||
|
url
|
||||||
|
}
|
||||||
nativeCurrency {
|
nativeCurrency {
|
||||||
name
|
name
|
||||||
symbol
|
symbol
|
||||||
|
@ -10,19 +10,22 @@ export interface EthereumListsChain {
|
|||||||
networkId: number
|
networkId: number
|
||||||
nativeCurrency: { name: string; symbol: string; decimals: number }
|
nativeCurrency: { name: string; symbol: string; decimals: number }
|
||||||
rpc: string[]
|
rpc: string[]
|
||||||
faucets: string[]
|
|
||||||
infoURL: string
|
infoURL: string
|
||||||
|
faucets: string[]
|
||||||
|
explorers: [{ url: string }]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface NetworkObject {
|
export interface NetworkObject {
|
||||||
chainId: number
|
chainId: number
|
||||||
name: string
|
name: string
|
||||||
|
nativeCurrency: string
|
||||||
|
explorers: [{ url: string }]
|
||||||
urlList: string[]
|
urlList: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const configGaiaX = getOceanConfig(2021000)
|
const configGaiaX = getOceanConfig(2021000)
|
||||||
|
|
||||||
export const networkDataGaiaX = {
|
export const networkDataGaiaX: EthereumListsChain = {
|
||||||
name: 'GAIA-X Testnet',
|
name: 'GAIA-X Testnet',
|
||||||
chainId: 2021000,
|
chainId: 2021000,
|
||||||
shortName: 'GAIA-X',
|
shortName: 'GAIA-X',
|
||||||
@ -35,17 +38,8 @@ export const networkDataGaiaX = {
|
|||||||
'https://faucet.gaiaxtestnet.oceanprotocol.com/',
|
'https://faucet.gaiaxtestnet.oceanprotocol.com/',
|
||||||
'https://faucet.gx.gaiaxtestnet.oceanprotocol.com/'
|
'https://faucet.gx.gaiaxtestnet.oceanprotocol.com/'
|
||||||
],
|
],
|
||||||
infoURL: 'https://www.gaia-x.eu'
|
infoURL: 'https://www.gaia-x.eu',
|
||||||
}
|
explorers: [{ url: '' }]
|
||||||
|
|
||||||
export function getNetworkConfigObject(node: any): NetworkObject {
|
|
||||||
const networkConfig = {
|
|
||||||
name: node.chain,
|
|
||||||
symbol: node.nativeCurrency.symbol,
|
|
||||||
chainId: node.chainId,
|
|
||||||
urlList: [node.providerUri]
|
|
||||||
}
|
|
||||||
return networkConfig
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function accountTruncate(account: string): string {
|
export function accountTruncate(account: string): string {
|
||||||
@ -98,12 +92,14 @@ export function getNetworkDataById(
|
|||||||
|
|
||||||
export async function addCustomNetwork(
|
export async function addCustomNetwork(
|
||||||
web3Provider: any,
|
web3Provider: any,
|
||||||
network: NetworkObject
|
network: EthereumListsChain
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const newNewtworkData = {
|
const newNewtworkData = {
|
||||||
chainId: `0x${network.chainId.toString(16)}`,
|
chainId: `0x${network.chainId.toString(16)}`,
|
||||||
chainName: network.name,
|
chainName: getNetworkDisplayName(network, network.chainId),
|
||||||
rpcUrls: network.urlList
|
nativeCurrency: network.nativeCurrency,
|
||||||
|
rpcUrls: network.rpc,
|
||||||
|
blockExplorerUrls: [network.explorers[0].url]
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await web3Provider.request({
|
await web3Provider.request({
|
||||||
@ -112,7 +108,7 @@ export async function addCustomNetwork(
|
|||||||
})
|
})
|
||||||
} catch (switchError) {
|
} catch (switchError) {
|
||||||
if (switchError.code === 4902) {
|
if (switchError.code === 4902) {
|
||||||
web3Provider.request(
|
await web3Provider.request(
|
||||||
{
|
{
|
||||||
method: 'wallet_addEthereumChain',
|
method: 'wallet_addEthereumChain',
|
||||||
params: [newNewtworkData]
|
params: [newNewtworkData]
|
||||||
|
Loading…
Reference in New Issue
Block a user