From 33147bee0d1e4da961264c20e829e46352426ddc Mon Sep 17 00:00:00 2001 From: IshaVenikar <145848618+IshaVenikar@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:35:42 +0530 Subject: [PATCH] Auto-populate cosmos chain details from chain id (#95) * Autplopulate cosmos chain details from chain id * Display rpc url * Remove assert * Make review changes * Use watch to trigger function * Use usewatch --- package.json | 4 ++- src/components/Accounts.tsx | 6 ++-- src/screens/AddNetwork.tsx | 67 +++++++++++++++++++++++++------------ yarn.lock | 17 ++++++++++ 4 files changed, 68 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index 17228df..468d95b 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "@react-navigation/native-stack": "^6.9.18", "@walletconnect/react-native-compat": "^2.11.2", "@walletconnect/web3wallet": "^1.10.2", + "chain-registry": "^1.41.2", "cosmjs-types": "^0.9.0", "ethers": "5.7.2", "fast-text-encoding": "^1.0.6", @@ -44,7 +45,8 @@ "react-native-svg": "^15.1.0", "react-native-vector-icons": "^10.0.3", "react-native-vision-camera": "^3.9.0", - "text-encoding-polyfill": "^0.6.7" + "text-encoding-polyfill": "^0.6.7", + "use-debounce": "^10.0.0" }, "devDependencies": { "@babel/core": "^7.20.0", diff --git a/src/components/Accounts.tsx b/src/components/Accounts.tsx index 411838b..051e0eb 100644 --- a/src/components/Accounts.tsx +++ b/src/components/Accounts.tsx @@ -161,10 +161,8 @@ const Accounts = ({ currentIndex, updateIndex }: AccountsProps) => { '_', JSON.stringify(updatedNetworks), ); - const currentNetwork = networksData.find( - networkData => networkData.networkId === '0', - ); - setSelectedNetwork(currentNetwork!); + + setSelectedNetwork(updatedNetworks[0]); setDeleteNetworkDialog(false); setNetworksData(updatedNetworks); }; diff --git a/src/screens/AddNetwork.tsx b/src/screens/AddNetwork.tsx index 339ed0d..e96eee9 100644 --- a/src/screens/AddNetwork.tsx +++ b/src/screens/AddNetwork.tsx @@ -1,12 +1,14 @@ import React, { useCallback, useEffect, useState } from 'react'; import { ScrollView } from 'react-native'; -import { useForm, Controller } from 'react-hook-form'; +import { useForm, Controller, useWatch } from 'react-hook-form'; import { TextInput, Button, HelperText } from 'react-native-paper'; import { getInternetCredentials, setInternetCredentials, } from 'react-native-keychain'; import { HDNode } from 'ethers/lib/utils'; +import { chains } from 'chain-registry'; +import { useDebouncedCallback } from 'use-debounce'; import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { useNavigation } from '@react-navigation/native'; @@ -33,6 +35,11 @@ const AddNetwork = () => { mode: 'onChange', }); + const watchChainId = useWatch({ + control, + name: 'chainId', + }); + const { setNetworksData } = useNetworks(); const [namespace, setNamespace] = useState(EIP155); @@ -41,6 +48,22 @@ const AddNetwork = () => { setNamespace(newNetworkType); }; + const fetchChainDetails = useDebouncedCallback((chainId: string) => { + const cosmosChainDetails = chains.find( + ({ chain_id }) => chain_id === chainId, + ); + + if (!cosmosChainDetails) { + return; + } + setValue('networkName', cosmosChainDetails.pretty_name); + setValue('rpcUrl', cosmosChainDetails.apis?.rpc?.[0]?.address || ''); + setValue('blockExplorerUrl', cosmosChainDetails.explorers?.[0].url || ''); + setValue('addressPrefix', cosmosChainDetails.bech32_prefix); + setValue('coinType', String(cosmosChainDetails.slip44)); + setValue('nativeDenom', cosmosChainDetails.fees?.fee_tokens[0].denom || ''); + }, 2000); + const submit = useCallback( async (data: NetworksFormData) => { const newNetworkData = { @@ -110,12 +133,31 @@ const AddNetwork = () => { ); useEffect(() => { - setValue('coinType', namespace === EIP155 ? '60' : '118'); - }, [setValue, namespace]); + fetchChainDetails(watchChainId); + }, [watchChainId, fetchChainDetails]); return ( // TODO: get form data from json file + + + ( + <> + onChange(value)} + /> + {errors.chainId?.message} + + )} + /> { )} /> - ( - <> - onChange(value)} - /> - {errors.chainId?.message} - - )} - /> + { )} /> -