Skip to content

Commit

Permalink
core: parallel Wallet Polling in useAccount hook (#492)
Browse files Browse the repository at this point in the history
Resolves #482
  • Loading branch information
fracek authored Sep 1, 2024
2 parents 8981a72 + a56d92c commit 59048d7
Showing 1 changed file with 43 additions and 37 deletions.
80 changes: 43 additions & 37 deletions packages/core/src/hooks/use-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,56 +62,62 @@ export function useAccount(): UseAccountResult {
});
}

for (const connector of connectors) {
if (!connector.available()) continue;

const connectorPromises = connectors
// If the account is connected, we get the address
let connAccount: string[] | undefined;
try {
.filter((connector) => connector.available())
.map(async (connector) => {
// we get permissions from the wallet
const permissions: Permission[] = await connector.request({
type: "wallet_getPermissions",
});

// if the wallet doesn't have the permission to get accounts,
// that means the wallet is not connected and we skip it
if (!permissions.includes(Permission.ACCOUNTS)) continue;
// if the wallet doesn't have the permission to get accounts, reject
if (!permissions.includes(Permission.ACCOUNTS))
throw new Error("No permission to get accounts");

// if the wallet is connected and has permissions, so we request the accounts
connAccount = await connector.request({
const connAccount = await connector.request({
type: "wallet_requestAccounts",
params: { silent_mode: true },
});
} catch {}

if (connAccount?.[0] === connectedAccount.address) {
return setState({
connector,
chainId: await connector.chainId(),
account: connectedAccount,
address: getAddress(connectedAccount.address),
status: "connected",
isConnected: true,
isConnecting: false,
isDisconnected: false,
isReconnecting: false,
});
}
}
// Check if the account matches the connected account
if (connAccount?.[0] === connectedAccount.address) {
return {
connector,
chainId: await connector.chainId(),
account: connectedAccount,
address: getAddress(connectedAccount.address),
status: "connected" as const,
isConnected: true,
isConnecting: false,
isDisconnected: false,
isReconnecting: false,
};
}

// If the account does not match, reject
throw new Error("Account does not match");
});

// If we get here, we're not connected to any connector.
// This can happen if it's an arcade account.
setState({
connector: undefined,
chainId: undefined,
account: connectedAccount,
address: getAddress(connectedAccount.address),
status: "connected",
isConnected: true,
isConnecting: false,
isDisconnected: false,
isReconnecting: false,
});
try {
const state = await Promise.any(connectorPromises);
if (state) return setState(state);
} catch {
// If we get here, we're not connected to any connector.
// This can happen if it's an arcade account.
setState({
connector: undefined,
chainId: undefined,
account: connectedAccount,
address: getAddress(connectedAccount.address),
status: "connected",
isConnected: true,
isConnecting: false,
isDisconnected: false,
isReconnecting: false,
});
}
}, [connectedAccount, connectors]);

useEffect(() => {
Expand Down

0 comments on commit 59048d7

Please sign in to comment.