Prefill accounts for new network

This commit is contained in:
Shreerang Kale 2025-06-05 12:28:59 +05:30
parent 2a07bc8b35
commit 5c335d39db
2 changed files with 31 additions and 2 deletions

View File

@ -13,7 +13,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { StackParamsList } from "../types";
import { SelectNetworkType } from "../components/SelectNetworkType";
import { storeNetworkData } from "../utils/accounts";
import { addAccountsForANetwork, getNextAccountId, storeNetworkData } from "../utils/accounts";
import { useNetworks } from "../context/NetworksContext";
import {
COSMOS,
@ -135,7 +135,6 @@ const AddNetwork = () => {
);
}, CHAINID_DEBOUNCE_DELAY);
// TODO: Handle replicating existing accounts after adding new network
const submit = useCallback(
async (data: z.infer<typeof networksFormDataSchema>) => {
const newNetworkData = {
@ -182,6 +181,15 @@ const AddNetwork = () => {
const retrievedNetworksData = await storeNetworkData(newNetworkData);
setNetworksData(retrievedNetworksData);
// Get number of accounts in first network
const currentNumberOfAccounts = await getNextAccountId(
`${retrievedNetworksData[0].namespace}:${retrievedNetworksData[0].chainId}`,
);
const selectedNetwork = retrievedNetworksData.find(
(network) => network.chainId === newNetworkData.chainId,
);
await Promise.all([
setInternetCredentials(
`accounts/${newNetworkData.namespace}:${newNetworkData.chainId}/0`,
@ -200,6 +208,8 @@ const AddNetwork = () => {
),
]);
await addAccountsForANetwork(selectedNetwork!, currentNumberOfAccounts - 1);
navigation.navigate("Home");
},
[navigation, namespace, setNetworksData],

View File

@ -112,6 +112,24 @@ const addAccount = async (
}
};
const addAccountsForANetwork = async (
network: NetworksDataState,
numberOfAccounts: number,
): Promise<void> => {
try {
const namespaceChainId = `${network.namespace}:${network.chainId}`;
for (let i = 0; i < numberOfAccounts; i++) {
const id = await getNextAccountId(namespaceChainId);
const hdPath = getHDPath(namespaceChainId, `0'/0/${id}`);
await addAccountFromHDPath(hdPath, network);
await updateAccountCounter(namespaceChainId, id);
}
} catch (error) {
console.error('Error creating account:', error);
}
};
const addAccountFromHDPath = async (
hdPath: string,
networkData: NetworksDataState,
@ -354,6 +372,7 @@ export {
createWallet,
addAccount,
addAccountFromHDPath,
addAccountsForANetwork,
storeNetworkData,
retrieveNetworksData,
retrieveAccounts,