Files
explore/lib/web3/useAddChain.tsx
T
tom goriunovandGitHub 429bb4657b MetaMask: update chain data and add NFT tokens (#2598)
* Tweak `Add {Network}` button to enable network details update in MetaMask

Fixes #2591

* add NFTs tokens to MM wallet
2025-03-05 15:03:40 +01:00

41 lines
1.1 KiB
TypeScript

import React from 'react';
import type { AddEthereumChainParameter } from 'viem';
import config from 'configs/app';
import useProvider from './useProvider';
import { getHexadecimalChainId } from './utils';
function getParams(): AddEthereumChainParameter {
if (!config.chain.id) {
throw new Error('Missing required chain config');
}
return {
chainId: getHexadecimalChainId(Number(config.chain.id)),
chainName: config.chain.name ?? '',
nativeCurrency: {
name: config.chain.currency.name ?? '',
symbol: config.chain.currency.symbol ?? '',
decimals: config.chain.currency.decimals ?? 18,
},
rpcUrls: config.chain.rpcUrls,
blockExplorerUrls: [ config.app.baseUrl ],
};
}
export default function useAddChain() {
const { wallet, provider } = useProvider();
return React.useCallback(() => {
if (!wallet || !provider) {
throw new Error('Wallet or provider not found');
}
return provider.request({
method: 'wallet_addEthereumChain',
params: [ getParams() ],
});
}, [ wallet, provider ]);
}