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