mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
* Tweak `Add {Network}` button to enable network details update in MetaMask
Fixes #2591
* add NFTs tokens to MM wallet
36 lines
983 B
TypeScript
36 lines
983 B
TypeScript
import { get } from 'es-toolkit/compat';
|
|
import React from 'react';
|
|
|
|
import getErrorObj from 'lib/errors/getErrorObj';
|
|
|
|
import useAddChain from './useAddChain';
|
|
import useProvider from './useProvider';
|
|
import useSwitchChain from './useSwitchChain';
|
|
|
|
export default function useSwitchOrAddChain() {
|
|
const { wallet, provider } = useProvider();
|
|
const addChain = useAddChain();
|
|
const switchChain = useSwitchChain();
|
|
|
|
return React.useCallback(async() => {
|
|
if (!wallet || !provider) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
return switchChain();
|
|
} catch (error) {
|
|
const errorObj = getErrorObj(error);
|
|
const code = get(errorObj, 'code');
|
|
const originalErrorCode = get(errorObj, 'data.originalError.code');
|
|
|
|
// This error code indicates that the chain has not been added to Wallet.
|
|
if (code === 4902 || originalErrorCode === 4902) {
|
|
return addChain();
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
}, [ addChain, provider, wallet, switchChain ]);
|
|
}
|