Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: preventing wallet to reset because of fetch account returning undefined #1667

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lucky-trainers-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

fix wallet reset when fetch account returns undefined
13 changes: 7 additions & 6 deletions packages/app/src/systems/Account/machines/accountsMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const fetchAccount = {
},
{
target: 'idle',
actions: ['assignAccount', 'setIsUnlogged'],
actions: ['assignAccount'],
},
],
onError: [
Expand Down Expand Up @@ -106,7 +106,7 @@ export const accountsMachine = createMachine(
},
{
target: 'idle',
actions: ['assignAccounts', 'setIsUnlogged'],
actions: ['assignAccounts'],
},
],
onError: [
Expand Down Expand Up @@ -211,9 +211,6 @@ export const accountsMachine = createMachine(
setIsLogged: () => {
Storage.setItem(IS_LOGGED_KEY, true);
},
setIsUnlogged: () => {
Storage.removeItem(IS_LOGGED_KEY);
},
notifyUpdateAccounts: () => {
store.updateAccounts();
},
Expand All @@ -232,7 +229,11 @@ export const accountsMachine = createMachine(
showError: true,
maxAttempts: 1,
async fetch() {
const accountToFetch = await AccountService.getCurrentAccount();
let accountToFetch = await AccountService.getCurrentAccount();
if (!accountToFetch) {
await AccountService.setCurrentAccountToDefault();
accountToFetch = await AccountService.getCurrentAccount();
}
if (!accountToFetch) return undefined;
const selectedNetwork = await NetworkService.getSelectedNetwork();
if (!selectedNetwork) {
Expand Down
20 changes: 20 additions & 0 deletions packages/app/src/systems/Account/services/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,26 @@ export class AccountService {
});
}

static setCurrentAccountToFalse() {
return db.transaction('rw', db.accounts, async () => {
await db.accounts
.filter((account) => !!account.isCurrent)
.modify({ isCurrent: false });
});
}

static setCurrentAccountToDefault() {
console.log('recovering default');
return db.transaction('rw', db.accounts, async () => {
const [firstAccount] = await db.accounts.toArray();
if (firstAccount) {
await db.accounts
.filter((account) => account.address === firstAccount.address)
.modify({ isCurrent: true });
}
});
}

static setCurrentAccount(input: AccountInputs['setCurrentAccount']) {
return db.transaction('rw', db.accounts, async () => {
await db.accounts
Expand Down
Loading