forked from cerc-io/laconic-wallet
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:
parent
33147bee0d
commit
888302a0dd
1
src/assets/ethereum-chains.json
Normal file
1
src/assets/ethereum-chains.json
Normal file
File diff suppressed because one or more lines are too long
@ -18,8 +18,9 @@ import { NetworksDataState, NetworksFormData, StackParamsList } from '../types';
|
|||||||
import { SelectNetworkType } from '../components/SelectNetworkType';
|
import { SelectNetworkType } from '../components/SelectNetworkType';
|
||||||
import { storeNetworkData } from '../utils/accounts';
|
import { storeNetworkData } from '../utils/accounts';
|
||||||
import { useNetworks } from '../context/NetworksContext';
|
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 { getCosmosAccounts } from '../utils/accounts';
|
||||||
|
import ETH_CHAINS from '../assets/ethereum-chains.json';
|
||||||
|
|
||||||
// TODO: Add validation to form inputs
|
// TODO: Add validation to form inputs
|
||||||
const AddNetwork = () => {
|
const AddNetwork = () => {
|
||||||
@ -31,6 +32,7 @@ const AddNetwork = () => {
|
|||||||
formState: { errors, isValid },
|
formState: { errors, isValid },
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
setValue,
|
setValue,
|
||||||
|
reset,
|
||||||
} = useForm<NetworksDataState>({
|
} = useForm<NetworksDataState>({
|
||||||
mode: 'onChange',
|
mode: 'onChange',
|
||||||
});
|
});
|
||||||
@ -49,11 +51,25 @@ const AddNetwork = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const fetchChainDetails = useDebouncedCallback((chainId: string) => {
|
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(
|
const cosmosChainDetails = chains.find(
|
||||||
({ chain_id }) => chain_id === chainId,
|
({ chain_id }) => chain_id === chainId,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!cosmosChainDetails) {
|
if (!cosmosChainDetails) {
|
||||||
|
reset({ chainId: chainId });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setValue('networkName', cosmosChainDetails.pretty_name);
|
setValue('networkName', cosmosChainDetails.pretty_name);
|
||||||
@ -62,7 +78,7 @@ const AddNetwork = () => {
|
|||||||
setValue('addressPrefix', cosmosChainDetails.bech32_prefix);
|
setValue('addressPrefix', cosmosChainDetails.bech32_prefix);
|
||||||
setValue('coinType', String(cosmosChainDetails.slip44));
|
setValue('coinType', String(cosmosChainDetails.slip44));
|
||||||
setValue('nativeDenom', cosmosChainDetails.fees?.fee_tokens[0].denom || '');
|
setValue('nativeDenom', cosmosChainDetails.fees?.fee_tokens[0].denom || '');
|
||||||
}, 2000);
|
}, CHAINID_DEBOUNCE_DELAY);
|
||||||
|
|
||||||
const submit = useCallback(
|
const submit = useCallback(
|
||||||
async (data: NetworksFormData) => {
|
async (data: NetworksFormData) => {
|
||||||
|
@ -24,3 +24,5 @@ export const DEFAULT_NETWORKS = [
|
|||||||
isDefault: true,
|
isDefault: true,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
export const CHAINID_DEBOUNCE_DELAY = 250;
|
||||||
|
Loading…
Reference in New Issue
Block a user