mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
* redesign "add chain" button * fix wallet detection * [skip ci] update default value for ENV * fix tests * return button to the footer and add source to mixpanel event * regcred from helmfile for review * [skip ci] remove wallets env from values for demo stand * use react-query to cache detection result * add cookie to hide button in the topbar * add prefix to svg ids to fix rabby icon * update screenshot * remove unnecessary comment --------- Co-authored-by: Nick Zenchik <[email protected]>
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import React from 'react';
|
|
import type { AddEthereumChainParameter } from 'viem';
|
|
|
|
import config from 'configs/app';
|
|
import { useMultichainContext } from 'lib/contexts/multichain';
|
|
import { SECOND } from 'toolkit/utils/consts';
|
|
|
|
import useRewardsActivity from '../hooks/useRewardsActivity';
|
|
import useProvider from './useProvider';
|
|
import { getHexadecimalChainId } from './utils';
|
|
|
|
function getParams(chainConfig: typeof config): AddEthereumChainParameter {
|
|
if (!chainConfig.chain.id) {
|
|
throw new Error('Missing required chain config');
|
|
}
|
|
|
|
return {
|
|
chainId: getHexadecimalChainId(Number(chainConfig.chain.id)),
|
|
chainName: chainConfig.chain.name ?? '',
|
|
nativeCurrency: {
|
|
name: chainConfig.chain.currency.name ?? '',
|
|
symbol: chainConfig.chain.currency.symbol ?? '',
|
|
decimals: chainConfig.chain.currency.decimals ?? 18,
|
|
},
|
|
rpcUrls: chainConfig.chain.rpcUrls,
|
|
blockExplorerUrls: [ chainConfig.app.baseUrl ],
|
|
};
|
|
}
|
|
|
|
export default function useAddChain() {
|
|
const { data: { wallet, provider } = {} } = useProvider();
|
|
const { trackUsage } = useRewardsActivity();
|
|
const multichainContext = useMultichainContext();
|
|
|
|
const chainConfig = multichainContext?.chain.config ?? config;
|
|
|
|
return React.useCallback(async() => {
|
|
if (!wallet || !provider) {
|
|
throw new Error('Wallet or provider not found');
|
|
}
|
|
|
|
const start = Date.now();
|
|
|
|
await provider.request({
|
|
method: 'wallet_addEthereumChain',
|
|
params: [ getParams(chainConfig) ],
|
|
});
|
|
|
|
// if network is already added, the promise resolves immediately
|
|
if (Date.now() - start > SECOND) {
|
|
await trackUsage('add_network');
|
|
}
|
|
}, [ wallet, provider, chainConfig, trackUsage ]);
|
|
}
|