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

[Refactor] Improve state management #222

Open
wants to merge 151 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
151 commits
Select commit Hold shift + click to select a range
125bfa0
feat: remove unused state propeties subscription:
fpezzola Nov 17, 2022
1fdfb54
feat: Split state and subscriptions:
fpezzola Nov 21, 2022
c0a3896
feat: prune transactions receipt
fpezzola Nov 23, 2022
3842657
feat: virtualized list for transactions
fpezzola Nov 23, 2022
bea2a74
Merge branch 'master' of github.com:block-wallet/extension into refac…
fpezzola Nov 29, 2022
a0cb8ea
fix: grab loading flag from exchange rate state
fpezzola Nov 30, 2022
e101826
feat: Improve loading styles and scroll
fpezzola Dec 1, 2022
4e66ee9
Merge branch 'master' of github.com:block-wallet/extension into refac…
fpezzola Dec 15, 2022
28d700d
Merge branch 'master' of github.com:block-wallet/extension into refac…
fpezzola Dec 20, 2022
1df0f55
fix: styles
fpezzola Dec 21, 2022
8efd124
feat: use UIStore in the AppStateController
fpezzola Dec 21, 2022
fc87f45
refactor: move state subscriptions outside router
fpezzola Dec 21, 2022
5acc879
tests: Add mock context
fpezzola Dec 21, 2022
7b5af9d
tests: fix test
fpezzola Dec 21, 2022
c218a74
bump version
fpezzola Dec 21, 2022
619dbef
Merge branch 'master' into refactor/state-subscriptions-improvement
fpezzola Dec 21, 2022
823465c
Merge branch 'master' into refactor/state-subscriptions-improvement
Dec 22, 2022
214aa1a
Merge branch 'master' of github.com:block-wallet/extension into refac…
fpezzola Jan 6, 2023
4c12a4a
Merge branch 'master' into refactor/state-subscriptions-improvement
Jan 12, 2023
c4873df
Merge branch 'master' into refactor/state-subscriptions-improvement
Jan 24, 2023
84b0014
lint: fixed lint.
julianariel Jan 24, 2023
e5027ee
fix: fixed tests, added missing migration import.
julianariel Jan 25, 2023
c11e750
Merge branch 'master' into refactor/state-subscriptions-improvement
Jan 30, 2023
799338b
Merge branch 'master' into refactor/state-subscriptions-improvement
Feb 2, 2023
994563d
Merge branch 'master' of github.com:block-wallet/extension into refac…
fpezzola Feb 27, 2023
c95805c
feat: add migration and improve code
fpezzola Feb 27, 2023
c167ad4
feat: new brand stylesheet
Feb 27, 2023
5770cf9
feat: onboarding redesign
Feb 28, 2023
eef0e15
fix: improve state and code colocation
fpezzola Feb 28, 2023
a237b60
feat: Net Worth in PopupPage
Feb 28, 2023
c6502bf
feat: net worth tooltip + bridge icon color
Feb 28, 2023
fe16b07
fix: imports
fpezzola Mar 1, 2023
5cd389a
fix: balances undefined
Mar 1, 2023
44d5434
fix: onboarding logo
Mar 1, 2023
4f59238
fix: reset done page to align with new onboarding
Mar 1, 2023
14a5fc4
fix: net worth tooltip pos
Mar 1, 2023
302525b
fix: useTokenList in useNetWorth hook
Mar 1, 2023
373cfcc
fix: color changes
Mar 2, 2023
b586f80
fix: redesign comments
Mar 6, 2023
7c0cb2b
feat: backup in settings in top
Mar 6, 2023
e5993c8
fix: remove net worth symbol
Mar 6, 2023
d412c49
merge master into extension redesign
Mar 7, 2023
dc6bd6e
fix: net worth tooltip
Mar 7, 2023
3297929
feat: network indicator redesign UI
Mar 8, 2023
13bdc0d
feat: network indicator redesign Background
Mar 8, 2023
a70b35d
fix: handle wrong network image URL
Mar 9, 2023
191bb37
fix: NetworkDisplayBadge Network icon url error
Mar 9, 2023
00f6bdd
fix: UI inconsistencies and improvements
Mar 10, 2023
af4bcc0
fix: CTA color + header height
Mar 13, 2023
ebccf6f
Merge branch 'master' into feat/extension-redesign
Mar 13, 2023
ead7e20
fix: edit background version
Mar 13, 2023
81fcfb4
fix: unlock tooltip
Mar 14, 2023
49068bb
fix: origin app icon size bug
Mar 14, 2023
585e4f3
fix: new network logos
Mar 14, 2023
a276884
fix: new preferences icon
Mar 14, 2023
dad8e17
fix: network indicator height in dap requests
Mar 15, 2023
72acec9
fix: eth logo url in network const
Mar 15, 2023
ae12583
fix: insufficient
Mar 15, 2023
82a913c
fix: allowance decimals rounding up
Mar 15, 2023
7bc2257
Merge branch 'master' into feat/extension-redesign
Mar 15, 2023
b0ed098
fix: weird recent accounts in address book
Mar 20, 2023
fbf515a
fix: native token icon for activity list, gas & bridge
Mar 20, 2023
aa67a8e
fix: font colors with brand
Mar 20, 2023
23102ec
fix: add network indicator on some pages
Mar 20, 2023
f9f53f9
fix: DApp logos size
Mar 20, 2023
72bdfb0
fix: icons blue color
Mar 20, 2023
7ff618a
fix: net worth on Gnosis
Mar 20, 2023
3a44a34
fix: fiat currency consistent format
Mar 21, 2023
810ce24
fix: design inconsistencies v1.0
Mar 23, 2023
b167e22
fix: UI design inconsistencies v2.0
Mar 24, 2023
0700197
fix: design inconsistencies v3.0
Mar 24, 2023
4b3a189
Merge branch 'master' of github.com:block-wallet/extension into refac…
fpezzola Mar 27, 2023
53edea9
fix: tooltip ui consistency
Mar 27, 2023
8937cac
fix: ui design inconsistencies v3.0
Mar 27, 2023
546e3ca
Merge branch 'master' into feat/extension-redesign
Mar 28, 2023
1108bcc
fix: tooltips border
Mar 28, 2023
e7fe3ca
fix: hover color on drop network display
Mar 28, 2023
e770143
Merge branch 'master' into feat/extension-redesign
Apr 3, 2023
dcda723
fix: design comments
Apr 3, 2023
2d64fc0
feat: multiple rpcUrls in network (background)
Apr 4, 2023
f00b1d6
fix: network selector width + net worth link
Apr 5, 2023
62c2ffa
Merge branch 'feat/extension-redesign' into feat/improve-provider-down
Apr 5, 2023
2164346
feat: provider status UI & logic
Apr 5, 2023
92325b2
fix: check default interval when using backup provider
Apr 5, 2023
806a9ee
fix: svg warnings in network switch icon
Apr 5, 2023
9672fd9
refractor: provider status comp
Apr 5, 2023
8484d78
fix: box design inputs + input labels fix
Apr 6, 2023
2c80ce2
fix: token input heights
Apr 6, 2023
12cd666
Merge branch 'feat/extension-redesign' into feat/improve-provider-down
Apr 6, 2023
e69236b
refractor: change rpcUrls array to currentRpcUrl in Network obj
Apr 7, 2023
941645d
fix: mock app state and var name for consistency
Apr 7, 2023
e4e79fd
fix: input spacings
Apr 10, 2023
0579ea3
fix: input placeholder font weight
Apr 10, 2023
775d913
fix: UI border radius and text input
Apr 10, 2023
fb7466b
fix: input label hover effect
Apr 10, 2023
416925f
Merge branch 'feat/extension-redesign' into feat/improve-provider-down
Apr 10, 2023
ed4c550
refractor: provider status in networkController in background
Apr 12, 2023
cf024e5
Merge branch 'master' of github.com:block-wallet/extension into refac…
fpezzola Apr 14, 2023
30b0f15
fix: provider down on provider error
Apr 14, 2023
06d98a4
fix: update default provider before current if error
Apr 14, 2023
2bfcef1
Merge branch 'master' into feat/extension-redesign
Apr 14, 2023
0574fd4
Merge branch 'feat/extension-redesign' into feat/improve-provider-down
Apr 14, 2023
220a203
fix: migration imports
Apr 14, 2023
6bab0ce
Merge branch 'feat/extension-redesign' into feat/improve-provider-down
Apr 14, 2023
6aa8217
fix: migrations
fpezzola Apr 14, 2023
9d4125c
fix: seed phrase backup text on 3 lines
Apr 17, 2023
2b9c9ac
fix: net worth in account select + more space for name
Apr 17, 2023
216c7d7
feat: provider status in needed pages
Apr 17, 2023
67c5f5b
Merge branch 'feat/extension-redesign' into feat/improve-provider-down
Apr 17, 2023
019a348
fix: release notes info title wrap
Apr 17, 2023
49da77d
fix: three dot icon vertical + network display name size
Apr 17, 2023
324aef3
fix: backup provider status updates
Apr 18, 2023
fd69409
Merge branch 'feat/extension-redesign' into feat/improve-provider-down
Apr 18, 2023
0710e5a
fix: bug on ens search in send
Apr 19, 2023
3ba8896
Merge branch 'feat/extension-redesign' into feat/improve-provider-down
Apr 19, 2023
b61af9d
fix: disabled tab select hover + long balance text length
Apr 19, 2023
a505a31
Merge branch 'feat/extension-redesign' into feat/improve-provider-down
Apr 19, 2023
b876703
fix: rpcs in networks constant
Apr 19, 2023
ee863f2
fix: fix speed-up txs
fpezzola Apr 24, 2023
537fd18
fix: check sort by nonce before time
fpezzola Apr 24, 2023
c6a08f8
fix: sort
fpezzola Apr 24, 2023
1d312e4
fix: asset sticky tabs
fpezzola Apr 24, 2023
b5799e5
fix: transactions item dynamic size
fpezzola Apr 24, 2023
75d3971
fix: add new rpc urls to networks constant
Apr 24, 2023
2573b7b
feat: update chain-assets lib version
Apr 25, 2023
ef65feb
feat: include yarn.lock
Apr 25, 2023
dc9fdb8
fix: assets lists button and gas not updating problem
fpezzola Apr 25, 2023
26f999f
Merge branch 'master' into refactor/state-subscriptions-improvement
fpezzola Apr 25, 2023
6abb479
Feat/keyboard navigation (#420)
MartnLecam Apr 26, 2023
3d9ce18
Merge branch 'master' of github.com:block-wallet/extension into refac…
fpezzola Apr 27, 2023
c87617f
refractor: isRpcValid in background
Apr 27, 2023
ccd2455
Merge branch 'feat/extension-redesign' into feat/improve-provider-down
Apr 27, 2023
488e9bc
Merge pull request #424 from block-wallet/feat/improve-provider-down
Apr 27, 2023
85b7efc
Merge branch 'master' into refactor/state-subscriptions-improvement
Apr 27, 2023
9ea9c23
Merge branch 'master' into feat/extension-redesign
Apr 28, 2023
069130d
fix: build error after merging master
Apr 28, 2023
298d3cf
fix: ens search & address book error with useNetWorth
Apr 28, 2023
2f18b4b
fix: list not rerendering
fpezzola Apr 28, 2023
e3141de
[Feat] Send to smart contract warning (#439)
Apr 28, 2023
bd80568
fix: use react-virtualized instead of react-window
fpezzola Apr 28, 2023
fabfaf6
Merge branch 'feat/extension-redesign' of github.com:block-wallet/ext…
fpezzola May 1, 2023
bfbbdaf
Merge with master and fix merge issues
MartnLecam Oct 5, 2023
06a30d1
Merge Master in refactor/state
MartnLecam Oct 17, 2023
6c665bd
Merge master with refactor
MartnLecam Oct 17, 2023
af50551
Merge master
MartnLecam Oct 17, 2023
69cefcf
Remove unused vars
MartnLecam Oct 18, 2023
a5707fb
Fix missing migration
MartnLecam Oct 18, 2023
8db767d
Merge to master
MartnLecam Jan 19, 2024
ed3fc64
Merge branch 'master' into refactor/state-subscriptions-improvement
MartnLecam Jan 19, 2024
4630e5f
Add same logic than tokenactivity to scroll without loosing the header.
MartnLecam Jan 22, 2024
0af20f3
Merge branch 'refactor/state-subscriptions-improvement' of github.com…
MartnLecam Jan 22, 2024
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
2 changes: 1 addition & 1 deletion packages/background/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@block-wallet/background",
"version": "1.0.0",
"version": "1.1.0",
"private": true,
"dependencies": {
"@block-wallet/chains-assets": "https://github.com/block-wallet/chains-assets#v0.0.24",
Expand Down
36 changes: 21 additions & 15 deletions packages/background/src/controllers/AccountTrackerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
getAddressBalances as getAddressBalancesFromSingleCallBalancesContract,
isSingleCallBalancesContractAvailable,
} from '../utils/balance-checker/balanceChecker';
import { cloneDeep } from 'lodash';
import { cloneDeep, isEqual } from 'lodash';
import {
ACTIONS_TIME_INTERVALS_DEFAULT_VALUES,
Network,
Expand Down Expand Up @@ -852,7 +852,7 @@ export class AccountTrackerController extends BaseController<AccountTrackerState
assetAddressToGetBalance: string[],
deletedUserTokens: string[]
): void {
const stateAccounts = this.store.getState().accounts;
const stateAccounts = cloneDeep(this.store.getState().accounts);

const finalNativeTokenBalance = assetAddressToGetBalance.includes(
NATIVE_TOKEN_ADDRESS
Expand Down Expand Up @@ -886,22 +886,28 @@ export class AccountTrackerController extends BaseController<AccountTrackerState
}
}

this.store.updateState({
accounts: {
...this.store.getState().accounts,
[accountAddress]: {
...this.store.getState().accounts[accountAddress],
balances: {
...this.store.getState().accounts[accountAddress]
.balances,
[chainId]: {
nativeTokenBalance: finalNativeTokenBalance,
tokens: finalTokens,
},
const newState: Accounts = {
...this.store.getState().accounts,
[accountAddress]: {
...this.store.getState().accounts[accountAddress],
balances: {
...this.store.getState().accounts[accountAddress].balances,
[chainId]: {
nativeTokenBalance: finalNativeTokenBalance,
tokens: finalTokens,
},
},
},
});
};

const shouldUpdate = !isEqual(newState, this.store.getState().accounts);

//only update state if it has changed.
if (shouldUpdate) {
this.store.updateState({
chk0912 marked this conversation as resolved.
Show resolved Hide resolved
accounts: newState,
});
}
}

/**
Expand Down
57 changes: 50 additions & 7 deletions packages/background/src/controllers/ActivityListController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { compareAddresses } from './transactions/utils/utils';
import {
TransactionTypeEnum,
TransactionWatcherController,
TransactionWatcherControllerState,
} from './TransactionWatcherController';

export interface IActivityListState {
Expand Down Expand Up @@ -53,9 +54,52 @@ export class ActivityListController extends BaseController<IActivityListState> {
// If any of the following stores were updated trigger the ActivityList update
this._transactionsController.UIStore.subscribe(this.onStoreUpdate);
this._privacyController.UIStore.subscribe(this.onStoreUpdate);
this._preferencesController.store.subscribe(this.onStoreUpdate);
this._networkController.store.subscribe(this.onStoreUpdate);
this._transactionWatcherController.store.subscribe(this.onStoreUpdate);
this._preferencesController.store.subscribe((newState, oldState) => {
if (
!compareAddresses(
newState.selectedAddress,
oldState?.selectedAddress
)
) {
return this.onStoreUpdate();
}
});
this._networkController.store.subscribe((newState, oldState) => {
if (newState.selectedNetwork !== oldState?.selectedNetwork) {
this.onStoreUpdate();
}
});
this._transactionWatcherController.store.subscribe(
(newState, oldState) => {
const { selectedAddress } =
this._preferencesController.store.getState();

const safeGetTxsHashes = (
txWatcherState:
| TransactionWatcherControllerState
| undefined
) => {
if (!txWatcherState) {
return [];
}
return this.parseWatchedTransactions(
this._networkController.network.chainId,
selectedAddress,
txWatcherState.transactions
)
.map((tx) => tx.transactionParams.hash!)
fpezzola marked this conversation as resolved.
Show resolved Hide resolved
.filter(Boolean);
};

const oldTxs = safeGetTxsHashes(oldState);
const newTxs = safeGetTxsHashes(newState);

//If hashes length has changed, then update the activity list.
if (oldTxs.length !== newTxs.length) {
this.onStoreUpdate();
}
}
);
this._bridgeController.store.subscribe(this.onStoreUpdate);
this.onStoreUpdate();
}
Expand Down Expand Up @@ -219,11 +263,10 @@ export class ActivityListController extends BaseController<IActivityListState> {
*/
private parseWatchedTransactions(
chainId: number,
selectedAddress: string
selectedAddress: string,
transactions: TransactionWatcherControllerState['transactions'] = this._transactionWatcherController.store.getState()
.transactions
): TransactionMeta[] {
const { transactions } =
this._transactionWatcherController.store.getState();

const watchedTransactions: TransactionMeta[] = [];

if (chainId in transactions) {
Expand Down
24 changes: 16 additions & 8 deletions packages/background/src/controllers/AddressBookController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TransactionMeta,
} from './transactions/utils/types';
import { PreferencesController } from './PreferencesController';
import { isEqual } from 'lodash';
import { isNativeTokenAddress } from '../utils/token';

/**
Expand Down Expand Up @@ -339,15 +340,22 @@ export class AddressBookController extends BaseController<AddressBookControllerM
}
}

this.store.setState({
...this.store.getState(),
recentAddresses: {
...this.store.getState().recentAddresses,
[network]: {
...recentAddresses,
if (
!isEqual(
recentAddresses,
this.store.getState().recentAddresses[network] || {}
)
) {
this.store.setState({
...this.store.getState(),
recentAddresses: {
...this.store.getState().recentAddresses,
[network]: {
...recentAddresses,
},
},
},
});
});
}

return recentAddresses;
}
Expand Down
63 changes: 57 additions & 6 deletions packages/background/src/controllers/AppStateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import { isManifestV3 } from '../utils/manifest';
import KeyringControllerDerivated from './KeyringControllerDerivated';
import TransactionController from './transactions/TransactionController';

const STICKY_STORAGE_DATA_TTL = 60000 * 10;

export interface AppStateControllerMemState {
expiredStickyStorage: boolean;
isAppUnlocked: boolean;
lockedByTimeout: boolean;
idleTimeout: number;
}

export interface AppStateControllerState {
idleTimeout: number; // Minutes until auto-lock - Zero if disabled
isAppUnlocked: boolean;
Expand All @@ -17,15 +26,24 @@ export enum AppStateEvents {
APP_UNLOCKED = 'APP_UNLOCKED',
}

export default class AppStateController extends BaseController<AppStateControllerState> {
export default class AppStateController extends BaseController<
AppStateControllerState,
AppStateControllerMemState
> {
private _timer: ReturnType<typeof setTimeout> | null;
private _stickyStorageTimer: ReturnType<typeof setTimeout> | undefined;

constructor(
initState: AppStateControllerState,
private readonly _keyringController: KeyringControllerDerivated,
private readonly _transactionController: TransactionController
) {
super(initState);
super(initState, {
idleTimeout: initState.idleTimeout,
isAppUnlocked: initState.isAppUnlocked,
lockedByTimeout: initState.lockedByTimeout,
expiredStickyStorage: false,
});

this._timer = null;

Expand All @@ -44,6 +62,28 @@ export default class AppStateController extends BaseController<AppStateControlle
}

this._resetTimer();
this.store.subscribe(
(
newState: AppStateControllerState,
oldState?: AppStateControllerState
) => {
//To avoid sending the lastActiveTime to the UI, update the UIStore based on the store data.

if (newState.isAppUnlocked !== oldState?.isAppUnlocked) {
this.UIStore.updateState({
isAppUnlocked: newState.isAppUnlocked,
lockedByTimeout: newState.lockedByTimeout,
});
return;
}

if (newState.idleTimeout !== oldState?.idleTimeout) {
this.UIStore.updateState({
idleTimeout: newState.idleTimeout,
});
}
}
);
}

/**
Expand All @@ -53,12 +93,9 @@ export default class AppStateController extends BaseController<AppStateControlle
public setLastActiveTime = (): void => {
this.store.updateState({ lastActiveTime: new Date().getTime() });
this._resetTimer();
this._resetStickyStorageTimer();
};

public get lastActiveTime(): number {
return this.lastActiveTime;
}

/**
* Set a custom time in minutes for the extension auto block
* The idle timeout should be greater than zero
Expand Down Expand Up @@ -138,6 +175,19 @@ export default class AppStateController extends BaseController<AppStateControlle
}
};

private _resetStickyStorageTimer = () => {
if (this._stickyStorageTimer) {
clearTimeout(this._stickyStorageTimer);
}
//only update UIStore when it is necessary.
if (this.UIStore.getState().expiredStickyStorage) {
this.UIStore.updateState({ expiredStickyStorage: false });
}

this._stickyStorageTimer = setTimeout(() => {
this.UIStore.updateState({ expiredStickyStorage: true });
}, STICKY_STORAGE_DATA_TTL);
};
public autoUnlock = async (): Promise<void> => {
if (isManifestV3()) {
const { isAppUnlocked } = this.store.getState();
Expand Down Expand Up @@ -170,6 +220,7 @@ export default class AppStateController extends BaseController<AppStateControlle
// Update controller state
this.store.updateState({
isAppUnlocked: true,
lockedByTimeout: false,
});

this._resetTimer();
Expand Down
Loading