Auto-populate eth chain data from chain id (#96)

* Auto-populate eth data from json file

* Modify variable names

* Clear fields if chainId is not found

* Format code

* Fix indentation

* Use reset to clear values

* Exclude chain id while resetting

* Make review changes

* Fix format
This commit is contained in:
IshaVenikar 2024-04-16 18:53:00 +05:30 committed by Nabarun Gogoi
parent 33147bee0d
commit 888302a0dd
3 changed files with 22 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@ -18,8 +18,9 @@ import { NetworksDataState, NetworksFormData, StackParamsList } from '../types';
import { SelectNetworkType } from '../components/SelectNetworkType';
import { storeNetworkData } from '../utils/accounts';
import { useNetworks } from '../context/NetworksContext';
import { COSMOS, EIP155 } from '../utils/constants';
import { COSMOS, EIP155, CHAINID_DEBOUNCE_DELAY } from '../utils/constants';
import { getCosmosAccounts } from '../utils/accounts';
import ETH_CHAINS from '../assets/ethereum-chains.json';
// TODO: Add validation to form inputs
const AddNetwork = () => {
@ -31,6 +32,7 @@ const AddNetwork = () => {
formState: { errors, isValid },
handleSubmit,
setValue,
reset,
} = useForm<NetworksDataState>({
mode: 'onChange',
});
@ -49,11 +51,25 @@ const AddNetwork = () => {
};
const fetchChainDetails = useDebouncedCallback((chainId: string) => {
if (namespace === EIP155) {
const ethChainDetails = ETH_CHAINS.find(
chain => chain.chainId === Number(chainId),
);
if (!ethChainDetails) {
reset({ chainId: chainId });
return;
}
setValue('networkName', ethChainDetails.name);
setValue('rpcUrl', ethChainDetails.rpc[0]);
setValue('blockExplorerUrl', ethChainDetails.explorers![0].url);
setValue('coinType', String(ethChainDetails.slip44));
setValue('currencySymbol', ethChainDetails.nativeCurrency.symbol);
}
const cosmosChainDetails = chains.find(
({ chain_id }) => chain_id === chainId,
);
if (!cosmosChainDetails) {
reset({ chainId: chainId });
return;
}
setValue('networkName', cosmosChainDetails.pretty_name);
@ -62,7 +78,7 @@ const AddNetwork = () => {
setValue('addressPrefix', cosmosChainDetails.bech32_prefix);
setValue('coinType', String(cosmosChainDetails.slip44));
setValue('nativeDenom', cosmosChainDetails.fees?.fee_tokens[0].denom || '');
}, 2000);
}, CHAINID_DEBOUNCE_DELAY);
const submit = useCallback(
async (data: NetworksFormData) => {

View File

@ -24,3 +24,5 @@ export const DEFAULT_NETWORKS = [
isDefault: true,
},
];
export const CHAINID_DEBOUNCE_DELAY = 250;