Skip to content

Commit

Permalink
fix: preventing wallet to reset because of fetch account returning un…
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigobranas authored Nov 28, 2024
1 parent 63aa695 commit 45de3e1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
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

0 comments on commit 45de3e1

Please sign in to comment.