Skip to content

Commit 0dcba25

Browse files
committed
autoconnect in bulk to stored account chains
1 parent 6c8d1c0 commit 0dcba25

File tree

1 file changed

+79
-13
lines changed

1 file changed

+79
-13
lines changed

packages/core/src/manager.ts

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -381,21 +381,87 @@ export class WalletManager extends StateBase {
381381
);
382382
if (accountsStr && accountsStr !== '[]') {
383383
const accounts: SimpleAccount[] = JSON.parse(accountsStr);
384-
accounts.forEach((data) => {
385-
const chainWallet = mainWallet
386-
.getChainWalletList(false)
387-
.find(
388-
(w) =>
389-
w.chainRecord.chain?.chain_id === data.chainId &&
390-
w.namespace === data.namespace
391-
);
392-
chainWallet?.activate();
393-
if (mainWallet.walletInfo.mode === 'wallet-connect') {
394-
chainWallet?.setData(data);
395-
chainWallet?.setState(State.Done);
384+
385+
const accountChainWallets = accounts.flatMap(
386+
(data): ChainWalletBase | [] => {
387+
const chainWallet = mainWallet
388+
.getChainWalletList(false)
389+
.find(
390+
(w) =>
391+
w.chainRecord.chain?.chain_id === data.chainId &&
392+
w.namespace === data.namespace
393+
);
394+
chainWallet?.activate();
395+
if (mainWallet.walletInfo.mode === 'wallet-connect') {
396+
chainWallet?.setData(data);
397+
chainWallet?.setState(State.Done);
398+
}
399+
return chainWallet || [];
396400
}
397-
});
401+
);
402+
398403
mainWallet.setState(State.Done);
404+
405+
// Activate all repos for the account chains and get their chain
406+
// records.
407+
const connectedChainRecords = accountChainWallets.flatMap(
408+
(chainWallet): ChainRecord | [] => {
409+
try {
410+
const repo = this.getWalletRepo(chainWallet.chainName);
411+
repo.activate();
412+
return repo.chainRecord;
413+
} catch {
414+
return [];
415+
}
416+
}
417+
);
418+
419+
const attemptConnectAll = async () => {
420+
try {
421+
await mainWallet.client?.enable?.(
422+
connectedChainRecords.map(
423+
(chainRecord) => chainRecord.chain.chain_id
424+
)
425+
);
426+
return true;
427+
} catch (error) {
428+
// If the error is a wallet rejection, log and return false to
429+
// indicate user rejection. Do not throw to continue.
430+
if (error instanceof Error && mainWallet.rejectMatched(error)) {
431+
this.logger?.error(
432+
`Failed to connect to stored chains: ${error.message}`
433+
);
434+
return false;
435+
}
436+
437+
throw error;
438+
}
439+
};
440+
441+
// Connect to all chains at once. On error (other than user
442+
// rejection), attempt to add each chain and try again.
443+
try {
444+
const rejected = !(await attemptConnectAll());
445+
// If user rejected, do not call `_reconnect` at the bottom or
446+
// else every chain wallet will attempt to connect to all chains
447+
// again.
448+
if (rejected) {
449+
return;
450+
}
451+
} catch (error) {
452+
// If failed to enable, add each chain and try again.
453+
for (const chainRecord of connectedChainRecords) {
454+
await mainWallet.client?.addChain?.(chainRecord);
455+
}
456+
457+
const rejected = !(await attemptConnectAll());
458+
// If user rejected, do not call `_reconnect` at the bottom or
459+
// else every chain wallet will attempt to connect to all chains
460+
// again.
461+
if (rejected) {
462+
return;
463+
}
464+
}
399465
}
400466
}
401467

0 commit comments

Comments
 (0)