Use networks data from local storage
This commit is contained in:
parent
1c8f0fbcfb
commit
36bb645e65
@ -1,6 +1,6 @@
|
||||
import { useEffect, useCallback } from "react";
|
||||
|
||||
import { addNewNetwork, createWallet, isChainIdPresent, isWalletCreated } from "../utils/accounts";
|
||||
import { addNewNetwork, createWallet, isChainIdPresent, isWalletCreated, retrieveAccounts } from "../utils/accounts";
|
||||
import { sendMessage } from "../utils/misc";
|
||||
import useAccountsData from "./useAccountsData";
|
||||
import { useNetworks } from "../context/NetworksContext";
|
||||
@ -11,9 +11,9 @@ import { NetworksFormData } from "../types";
|
||||
const REACT_APP_ALLOWED_URLS = import.meta.env.REACT_APP_ALLOWED_URLS;
|
||||
|
||||
const useGetOrCreateNetwork = () => {
|
||||
const { networksData } = useNetworks();
|
||||
const { getAccountsData } = useAccountsData();
|
||||
const { networksData, setNetworksData } = useNetworks();
|
||||
const { setAccounts } = useAccounts();
|
||||
const { getAccountsData } = useAccountsData();
|
||||
|
||||
const getOrCreateNetwork = useCallback(async (chainId: string, networkData?: NetworksFormData) => {
|
||||
const isCreated = await isWalletCreated();
|
||||
@ -24,23 +24,53 @@ const useGetOrCreateNetwork = () => {
|
||||
}
|
||||
|
||||
const isNetworkPresent = await isChainIdPresent(chainId);
|
||||
|
||||
let resolvedNetworkData: NetworksFormData | undefined = undefined;
|
||||
|
||||
if (!isNetworkPresent) {
|
||||
if (!networkData || chainId !== networkData.chainId) {
|
||||
console.log('Network data not available.');
|
||||
console.log("Network data not available.");
|
||||
return [];
|
||||
}
|
||||
|
||||
console.log('ChainId not found. Adding network')
|
||||
await addNewNetwork(networkData);
|
||||
console.log("ChainId not found. Adding network");
|
||||
|
||||
resolvedNetworkData = {
|
||||
chainId: networkData.chainId,
|
||||
namespace: networkData.namespace,
|
||||
networkName: networkData.networkName,
|
||||
rpcUrl: networkData.rpcUrl,
|
||||
blockExplorerUrl: networkData.blockExplorerUrl || "",
|
||||
addressPrefix: networkData.addressPrefix || "",
|
||||
coinType: networkData.coinType || "",
|
||||
nativeDenom: networkData.nativeDenom || "",
|
||||
gasPrice: networkData.gasPrice || String(import.meta.env.REACT_APP_DEFAULT_GAS_PRICE || ""),
|
||||
currencySymbol: networkData.currencySymbol || "",
|
||||
isDefault: false,
|
||||
};
|
||||
|
||||
const retrievedNetworksData = await addNewNetwork(resolvedNetworkData);
|
||||
setNetworksData(retrievedNetworksData);
|
||||
} else {
|
||||
// fallback if the network already exists, try to find it from networksData
|
||||
resolvedNetworkData = networksData.find(net => net.chainId === chainId);
|
||||
}
|
||||
|
||||
const accountsData = await getAccountsData(chainId);
|
||||
if (!resolvedNetworkData) {
|
||||
console.warn("Resolved network data is not available");
|
||||
return [];
|
||||
}
|
||||
|
||||
// Update the AccountsContext with the new accounts
|
||||
setAccounts(accountsData);
|
||||
const accounts = await retrieveAccounts({ ...resolvedNetworkData, networkId: '' });
|
||||
|
||||
return accountsData;
|
||||
}, [networksData, getAccountsData, setAccounts]);
|
||||
if (!accounts) {
|
||||
return [];
|
||||
}
|
||||
|
||||
setAccounts(accounts);
|
||||
|
||||
return accounts;
|
||||
}, [networksData, setAccounts, setNetworksData]);
|
||||
|
||||
useEffect(() => {
|
||||
const handleCreateAccounts = async (event: MessageEvent) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user