import { getNetworkDisplayName } from '@hooks/useNetworkMetadata' import { LoggerInstance } from '@oceanprotocol/lib' import { getOceanConfig } from './ocean' export function accountTruncate(account: string): string { if (!account || account === '') return const middle = account.substring(6, 38) const truncated = account.replace(middle, '…') return truncated } export async function addCustomNetwork( web3Provider: any, network: EthereumListsChain ): Promise { // Always add explorer URL from ocean.js first, as it's null sometimes // in network data const blockExplorerUrls = [ getOceanConfig(network.networkId).explorerUri, network.explorers && network.explorers[0].url ] const newNetworkData = { chainId: `0x${network.chainId.toString(16)}`, chainName: getNetworkDisplayName(network, network.chainId), nativeCurrency: network.nativeCurrency, rpcUrls: network.rpc, blockExplorerUrls } try { await web3Provider.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: newNetworkData.chainId }] }) } catch (switchError) { if (switchError.code === 4902) { await web3Provider.request( { method: 'wallet_addEthereumChain', params: [newNetworkData] }, (err: string, added: any) => { if (err || 'error' in added) { LoggerInstance.error( `Couldn't add ${network.name} (0x${ network.chainId }) network to MetaMask, error: ${err || added.error}` ) } else { LoggerInstance.log( `Added ${network.name} (0x${network.chainId}) network to MetaMask` ) } } ) } else { LoggerInstance.error( `Couldn't add ${network.name} (0x${network.chainId}) network to MetaMask, error: ${switchError}` ) } } LoggerInstance.log( `Added ${network.name} (0x${network.chainId}) network to MetaMask` ) } export async function addTokenToWallet( web3Provider: any, address: string, symbol: string, logo?: string ): Promise { const image = logo || 'https://raw.githubusercontent.com/oceanprotocol/art/main/logo/token.png' const tokenMetadata = { type: 'ERC20', options: { address, symbol, image, decimals: 18 } } web3Provider.sendAsync( { method: 'wallet_watchAsset', params: tokenMetadata, id: Math.round(Math.random() * 100000) }, (err: { code: number; message: string }, added: any) => { if (err || 'error' in added) { LoggerInstance.error( `Couldn't add ${tokenMetadata.options.symbol} (${ tokenMetadata.options.address }) to MetaMask, error: ${err.message || added.error}` ) } else { LoggerInstance.log( `Added ${tokenMetadata.options.symbol} (${tokenMetadata.options.address}) to MetaMask` ) } } ) }