Update method to get accounts

This commit is contained in:
Adw8 2024-11-11 12:46:12 +05:30
parent 65c9014a5d
commit a970e11014

View File

@ -46,27 +46,22 @@ export const WalletEmbed = () => {
const { networksData } = useNetworks();
const getAccountsData = useCallback(async (chainId: string) => {
const getAccountsData = useCallback(async (chainId: string): Promise<string[]> => {
const targetNetwork = networksData.find(network => network.chainId === chainId);
if (!targetNetwork) {
return '';
return [];
}
const accounts = await retrieveAccounts(targetNetwork);
if (!accounts || accounts.length === 0) {
return '';
return [];
}
const accountsData = accounts.map(account => account.address).join(',');
return accountsData;
return accounts.map(account => account.address);
}, [networksData]);
const getAddressesFromData = (accountsData: string): string[] =>
accountsData?.split(',') || [];
const sendMessage = (
source: Window | null,
type: string,
@ -88,13 +83,13 @@ export const WalletEmbed = () => {
if (event.data.type !== 'REQUEST_WALLET_ACCOUNTS') return;
const accountsData = await getAccountsData(event.data.chainId);
if (!accountsData) {
if (accountsData.length === 0) {
sendMessage(event.source as Window, 'ERROR', 'Wallet accounts not found', event.origin);
return;
}
const addresses = getAddressesFromData(accountsData);
sendMessage(event.source as Window, 'WALLET_ACCOUNTS_DATA', addresses, event.origin);
sendMessage(event.source as Window, 'WALLET_ACCOUNTS_DATA', accountsData, event.origin);
};
window.addEventListener('message', handleGetAccounts);
@ -110,7 +105,7 @@ export const WalletEmbed = () => {
let accountsData = await getAccountsData(event.data.chainId);
if (!accountsData) {
if (accountsData.length === 0) {
console.log("Accounts not found, creating wallet...");
await createWallet(networksData);
@ -118,8 +113,7 @@ export const WalletEmbed = () => {
accountsData = await getAccountsData(event.data.chainId);
}
const addresses = getAddressesFromData(accountsData);
sendMessage(event.source as Window, 'WALLET_ACCOUNTS_DATA', addresses, event.origin);
sendMessage(event.source as Window, 'WALLET_ACCOUNTS_DATA', accountsData, event.origin);
};
window.addEventListener('message', handleCreateAccounts);
@ -348,7 +342,7 @@ export const WalletEmbed = () => {
</>
) : (
<View style={styles.spinnerContainer}>
<Text style={styles.LoadingText}>Loading...</Text>
<View style={{marginTop: 50}}></View>
<ActivityIndicator size="large" color="#0000ff" />
</View>
)}