mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
* first version of the pack * single entry point * add custom components to the package * move form components to toolkit * add more utils and hooks to the package * minor fixes * add CI workflow * update readme and package.json * fix ts * fix build of env-validator * fix docker build * fix icons size * test fixes * more test fixes
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import React from 'react';
|
|
import type { AddEthereumChainParameter } from 'viem';
|
|
|
|
import config from 'configs/app';
|
|
import { SECOND } from 'toolkit/utils/consts';
|
|
|
|
import useRewardsActivity from '../hooks/useRewardsActivity';
|
|
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();
|
|
const { trackUsage } = useRewardsActivity();
|
|
|
|
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() ],
|
|
});
|
|
|
|
// if network is already added, the promise resolves immediately
|
|
if (Date.now() - start > SECOND) {
|
|
await trackUsage('add_network');
|
|
}
|
|
}, [ wallet, provider, trackUsage ]);
|
|
}
|