1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

restore add token / add network

This commit is contained in:
Matthias Kretschmann 2023-01-19 02:07:02 +00:00
parent b9b72f13c2
commit 1d65e5d727
Signed by: m
GPG Key ID: 606EEEF3C479A91F
4 changed files with 9 additions and 17 deletions

View File

@ -41,7 +41,6 @@ export function accountTruncate(account: string): string {
} }
export async function addCustomNetwork( export async function addCustomNetwork(
web3Provider: any,
network: EthereumListsChain network: EthereumListsChain
): Promise<void> { ): Promise<void> {
// Always add explorer URL from ocean.js first, as it's null sometimes // Always add explorer URL from ocean.js first, as it's null sometimes
@ -59,13 +58,13 @@ export async function addCustomNetwork(
blockExplorerUrls blockExplorerUrls
} }
try { try {
await web3Provider.request({ await window?.ethereum.request({
method: 'wallet_switchEthereumChain', method: 'wallet_switchEthereumChain',
params: [{ chainId: newNetworkData.chainId }] params: [{ chainId: newNetworkData.chainId }]
}) })
} catch (switchError) { } catch (switchError) {
if (switchError.code === 4902) { if (switchError.code === 4902) {
await web3Provider.request( await (window?.ethereum.request as any)(
{ {
method: 'wallet_addEthereumChain', method: 'wallet_addEthereumChain',
params: [newNetworkData] params: [newNetworkData]
@ -96,7 +95,6 @@ export async function addCustomNetwork(
} }
export async function addTokenToWallet( export async function addTokenToWallet(
web3Provider: any,
address: string, address: string,
symbol: string, symbol: string,
logo?: string logo?: string
@ -110,7 +108,7 @@ export async function addTokenToWallet(
options: { address, symbol, image, decimals: 18 } options: { address, symbol, image, decimals: 18 }
} }
web3Provider.sendAsync( ;(window?.ethereum.request as any)(
{ {
method: 'wallet_watchAsset', method: 'wallet_watchAsset',
params: tokenMetadata, params: tokenMetadata,
@ -136,7 +134,7 @@ export async function getTokenBalance(
accountId: string, accountId: string,
decimals: number, decimals: number,
tokenAddress: string, tokenAddress: string,
web3Provider: any web3Provider: ethers.providers.Provider
): Promise<string> { ): Promise<string> {
try { try {
const token = new ethers.Contract(tokenAddress, erc20ABI, web3Provider) const token = new ethers.Contract(tokenAddress, erc20ABI, web3Provider)

View File

@ -4,7 +4,6 @@ import { addTokenToWallet } from '@utils/web3'
import Button from '@shared/atoms/Button' import Button from '@shared/atoms/Button'
import OceanLogo from '@images/logo.svg' import OceanLogo from '@images/logo.svg'
import styles from './index.module.css' import styles from './index.module.css'
import { useProvider } from 'wagmi'
const cx = classNames.bind(styles) const cx = classNames.bind(styles)
@ -23,8 +22,6 @@ export default function AddToken({
className, className,
minimal minimal
}: AddTokenProps): ReactElement { }: AddTokenProps): ReactElement {
const web3Provider = useProvider()
const styleClasses = cx({ const styleClasses = cx({
button: true, button: true,
minimal, minimal,
@ -32,9 +29,9 @@ export default function AddToken({
}) })
async function handleAddToken() { async function handleAddToken() {
if (!web3Provider) return if (!window?.ethereum) return
await addTokenToWallet(web3Provider, address, symbol) await addTokenToWallet(address, symbol)
} }
return ( return (

View File

@ -7,11 +7,10 @@ import useNetworkMetadata, {
getNetworkDisplayName getNetworkDisplayName
} from '@hooks/useNetworkMetadata' } from '@hooks/useNetworkMetadata'
import { useAsset } from '@context/Asset' import { useAsset } from '@context/Asset'
import { useNetwork, useProvider } from 'wagmi' import { useNetwork } from 'wagmi'
export default function WalletNetworkSwitcher(): ReactElement { export default function WalletNetworkSwitcher(): ReactElement {
const { chain } = useNetwork() const { chain } = useNetwork()
const web3Provider = useProvider()
const { asset } = useAsset() const { asset } = useAsset()
const { networksList } = useNetworkMetadata() const { networksList } = useNetworkMetadata()
@ -29,7 +28,7 @@ export default function WalletNetworkSwitcher(): ReactElement {
const networkNode = await networksList.find( const networkNode = await networksList.find(
(data) => data.chainId === asset.chainId (data) => data.chainId === asset.chainId
) )
addCustomNetwork(web3Provider, networkNode) addCustomNetwork(networkNode)
} }
return ( return (

View File

@ -4,7 +4,6 @@ import styles from './Network.module.css'
import Button from '@shared/atoms/Button' import Button from '@shared/atoms/Button'
import useNetworkMetadata from '@hooks/useNetworkMetadata' import useNetworkMetadata from '@hooks/useNetworkMetadata'
import { addCustomNetwork } from '@utils/web3' import { addCustomNetwork } from '@utils/web3'
import { useProvider } from 'wagmi'
export default function Network({ export default function Network({
chainId chainId
@ -12,11 +11,10 @@ export default function Network({
chainId: number chainId: number
}): ReactElement { }): ReactElement {
const { networksList } = useNetworkMetadata() const { networksList } = useNetworkMetadata()
const web3Provider = useProvider()
function changeNetwork(chainId: number) { function changeNetwork(chainId: number) {
const networkNode = networksList.find((data) => data.chainId === chainId) const networkNode = networksList.find((data) => data.chainId === chainId)
addCustomNetwork(web3Provider, networkNode) addCustomNetwork(networkNode)
} }
return ( return (