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: wallet reset issue #1713

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/brown-walls-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

refactor: accountMachine should not handle isLogged operations and should not have parallel states running
5 changes: 5 additions & 0 deletions .changeset/cool-goats-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

set isLogged when opening the db
5 changes: 5 additions & 0 deletions .changeset/sharp-beans-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

feat: cleaning now waits for localStorage
3 changes: 1 addition & 2 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"recommendations": [
"FuelLabs.sway-vscode-plugin",
"statelyai.stately-vscode",
"firsttris.vscode-jest-runner"
"statelyai.stately-vscode"
]
}
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
},
"[typescript]": {
"editor.defaultFormatter": "biomejs.biome"
}
},
"typescript.suggest.autoImports": true,
"javascript.suggest.autoImports": true,
"typescript.suggest.paths": true,
"typescript.suggest.enabled": true,
"typescript.suggest.completeFunctionCalls": true,
}
8 changes: 0 additions & 8 deletions packages/app/src/systems/Account/hooks/useAccounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ export function useAccounts() {
return accountStatus === status;
}

store.useUpdateMachineConfig(Services.accounts, {
actions: {
refreshApplication() {
window.location.reload();
},
},
});

useEffect(() => {
if (shouldListen.current) {
shouldListen.current = false;
Expand Down
177 changes: 113 additions & 64 deletions packages/app/src/systems/Account/machines/accountsMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type MachineContext = {
needsRecovery?: boolean;
account?: AccountWithBalance;
error?: unknown;
isFetchingAccounts?: boolean;
isLoggingOut?: boolean;
skipLoading?: boolean;
};

type MachineServices = {
Expand Down Expand Up @@ -41,31 +44,31 @@ export type AccountsMachineEvents =
input: AccountInputs['updateAccount'];
};

const fetchingAccountsState =
{
const fetchingAccountsState = {
initial: 'fetchingAccounts',
entry: 'assignIsFetching',
exit: 'assignIsntFetching',
states: {
fetchingAccounts: {
invoke: {
src: 'fetchAccounts',
onDone: [
{
target: 'recoveringWallet',
actions: ['assignAccounts', 'setIsLogged'],
cond: 'hasAccountsOrNeedsRecovery',
},
{
target: 'fetchingAccount',
actions: ['assignAccounts'],
},
],
onError: [
{
actions: 'assignError',
target: '#(machine).failed',
},
],
},
{
actions: 'assignError',
target: '#(machine).failed',
cond: FetchMachine.hasError,
},
{
target: 'recoveringWallet',
actions: ['assignAccounts'],
cond: 'hasAccountsOrNeedsRecovery',
},
{
target: 'fetchingAccount',
actions: ['assignAccounts'],
},
],
},
},
recoveringWallet: {
invoke: {
Expand Down Expand Up @@ -93,13 +96,7 @@ const fetchingAccountsState =
},
{
target: '#(machine).idle',
actions: ['assignAccount'],
},
],
onError: [
{
actions: 'assignError',
target: '#(machine).failed',
actions: ['assignAccount', 'assignSkipLoadingTrue'],
},
],
},
Expand All @@ -117,27 +114,29 @@ export const accountsMachine = createMachine(
},
predictableActionArguments: true,
id: '(machine)',
initial: 'fetchingAccounts',
initial: 'checkStartFetching',
states: {
idle: {
on: {
SET_CURRENT_ACCOUNT: {
target: 'settingCurrentAccount',
after: {
TIMEOUT: {
target: 'checkStartFetching',
},
TOGGLE_HIDE_ACCOUNT: {
actions: ['toggleHideAccount', 'notifyUpdateAccounts'],
},
},
checkStartFetching: {
always: [
{
cond: 'isLoggingOut',
target: 'idle',
},
},
after: {
/**
* Update accounts every 5 seconds
*/
TIMEOUT: {
{
cond: 'shouldSkipLoading',
target: 'refreshingAccounts',
cond: 'isLoggedIn',
},
},
{
target: 'fetchingAccounts',
},
],
},
fetchingAccounts: {
tags: ['loading'],
Expand All @@ -159,12 +158,33 @@ export const accountsMachine = createMachine(
cond: FetchMachine.hasError,
},
{
actions: ['notifyUpdateAccounts', 'redirectToHome'],
target: 'fetchingAccounts',
actions: [
'notifyUpdateAccounts',
'redirectToHome',
'assignSkipLoadingFalse',
],
target: 'checkStartFetching',
},
],
},
},
checkLogout: {
// this state enters on a "lock" state where fetching will not be performed
// also it will wait til fetching is not happening anymore
tags: ['loading'],
entry: 'assignIsLoggingOut',
always: [
{
cond: 'isntFetchingAccounts',
target: 'loggingout',
},
],
after: {
LOOP_TRY_LOGOUT: {
target: 'checkLogout',
},
},
},
loggingout: {
tags: ['loading'],
invoke: {
Expand All @@ -176,40 +196,51 @@ export const accountsMachine = createMachine(
target: 'failed',
},
{
actions: ['clearContext', 'refreshApplication'],
target: 'idle',
actions: ['clearContext'],
target: 'stopAll',
},
],
},
},
stopAll: {
type: 'final',
},
failed: {
after: {
INTERVAL: {
target: 'fetchingAccounts', // retry
cond: 'isLoggedIn',
},
INTERVAL: [
{
target: 'checkStartFetching', // retry
},
{
target: 'idle',
},
],
},
},
},
on: {
LOGOUT: {
target: 'loggingout',
target: 'checkLogout',
},
SET_CURRENT_ACCOUNT: {
target: 'settingCurrentAccount',
},
TOGGLE_HIDE_ACCOUNT: {
actions: ['toggleHideAccount', 'notifyUpdateAccounts'],
},
REFRESH_ACCOUNTS: [
{
cond: 'shouldSkipLoading',
target: 'refreshingAccounts',
actions: ['assignSkipLoading'],
target: 'checkStartFetching',
},
{
target: 'fetchingAccounts',
}
],
},
},
{
delays: {
INTERVAL: 2000,
TIMEOUT: 5000,
TIMEOUT: 2000,
LOOP_TRY_LOGOUT: 100,
},
actions: {
assignAccounts: assign({
Expand All @@ -226,15 +257,30 @@ export const accountsMachine = createMachine(
toggleHideAccount: (_, ev) => {
AccountService.updateAccount(ev.input);
},
setIsLogged: () => {
Storage.setItem(IS_LOGGED_KEY, true);
},
notifyUpdateAccounts: () => {
store.refreshAccounts();
},
redirectToHome: () => {
store.closeOverlay();
}
},
assignIsFetching: assign({
isFetchingAccounts: () => true,
}),
assignIsntFetching: assign({
isFetchingAccounts: () => false,
}),
assignIsLoggingOut: assign({
isLoggingOut: () => true,
}),
assignSkipLoading: assign({
skipLoading: (_, ev) => !!ev.input?.skipLoading,
}),
assignSkipLoadingTrue: assign({
skipLoading: () => true,
}),
assignSkipLoadingFalse: assign({
skipLoading: () => false,
}),
},
services: {
fetchAccounts: FetchMachine.create<
Expand Down Expand Up @@ -305,9 +351,6 @@ export const accountsMachine = createMachine(
}),
},
guards: {
isLoggedIn: () => {
return !!Storage.getItem(IS_LOGGED_KEY);
},
hasAccountsOrNeedsRecovery: (ctx, ev) => {
const hasAccounts = Boolean(
(ev.data.accounts || ctx?.accounts || []).length
Expand All @@ -317,8 +360,14 @@ export const accountsMachine = createMachine(
);
return hasAccounts || needsRecovery;
},
shouldSkipLoading: (_, ev) => {
return !!ev.input?.skipLoading;
shouldSkipLoading: (ctx) => {
return !!ctx.skipLoading;
},
isLoggingOut: (ctx) => {
return !!ctx.isLoggingOut;
},
isntFetchingAccounts: (ctx) => {
return !ctx.isFetchingAccounts;
},
},
}
Expand Down
4 changes: 3 additions & 1 deletion packages/app/src/systems/Account/services/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ export class AccountService {
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
const dataToLog: any = {};
try {
dataToLog.backupAccounts = JSON.stringify(backupAccounts?.map((account) => account?.data?.address) || []);
dataToLog.backupAccounts = JSON.stringify(
backupAccounts?.map((account) => account?.data?.address) || []
);
dataToLog.backupNetworks = JSON.stringify(backupNetworks || []);
// try getting data from indexedDB (outside of dexie) to check if it's also corrupted
const testNoDexieDbData = await getTestNoDexieDbData();
Expand Down
2 changes: 0 additions & 2 deletions packages/app/src/systems/Account/utils/storage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export class IndexedDBStorage implements StorageAbstract {
}

async clear() {
await chromeStorage.vaults.clear();
await chromeStorage.accounts.clear();
await db.transaction('rw', db.vaults, db.accounts, async () => {
await db.vaults.clear();
await db.accounts.clear();
Expand Down
18 changes: 13 additions & 5 deletions packages/app/src/systems/Core/services/core.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { toast } from '@fuel-ui/react';
import { VaultService } from '~/systems/Vault';
import { delay } from '../utils';
import { db } from '../utils/database';
import { Storage } from '../utils/storage';
import { chromeStorage } from './chromeStorage';
import { clearParallelDb } from '~/systems/Core/utils/databaseNoDexie';
import { IS_LOGGED_KEY } from '~/config';

// biome-ignore lint/complexity/noStaticOnlyClass: <explanation>
export class CoreService {
static async clear() {
toast.success('Your wallet will be reset');
await delay(1500);
await chromeStorage.clear();
await VaultService.clear();
await db.clear();
await Storage.clear();
Storage.clear();
await clearParallelDb();
const reloadAfterCleanCompleted = () => {
const isLogged = Storage.getItem(IS_LOGGED_KEY);
if (!isLogged) {
window.location.reload();
return;
}
setTimeout(() => reloadAfterCleanCompleted(), 50);
};
reloadAfterCleanCompleted();
}
}
Loading
Loading