Skip to content

Commit 57a3ada

Browse files
authored
feat: add should keep polling method (#685)
1 parent bfb533f commit 57a3ada

File tree

5 files changed

+142
-12
lines changed

5 files changed

+142
-12
lines changed

source/scripts/Background/controllers/MigrationController.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable camelcase */
22
import paliData from '../../../../package.json';
33
import v3_0_1 from '../migration/v3_0_1';
4+
import v3_3_2 from '../migration/v3_3_2';
45
import { getIsMigratedVersion, loadState } from 'state/paliStorage';
56

67
const MigrationController = async () => {
@@ -21,6 +22,14 @@ const MigrationController = async () => {
2122
if (currentPaliVersion === '3.0.1' && !isMigratedVersion) {
2223
await v3_0_1(state);
2324
}
25+
26+
/**
27+
* version < 3.3.2
28+
* Description: add faucet feature
29+
*/
30+
if (currentPaliVersion === '3.3.2' && !isMigratedVersion) {
31+
await v3_3_2(state);
32+
}
2433
};
2534

2635
export default MigrationController;

source/scripts/Background/handlers/handlePaliUpdates.ts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,64 @@
1+
import { INetworkType } from '@pollum-io/sysweb3-network';
2+
13
import { controllerEmitter } from 'scripts/Background/controllers/controllerEmitter';
24
import { isPollingRunNotValid } from 'scripts/Background/utils/isPollingRunNotValid';
35
import store from 'state/store';
6+
import { setPrevBalances } from 'state/vault';
47

5-
export async function checkForUpdates() {
6-
const { activeAccount, isBitcoinBased, activeNetwork } =
7-
store.getState().vault;
8+
function shouldUpdate() {
9+
const {
10+
accounts,
11+
activeAccount,
12+
isBitcoinBased,
13+
activeNetwork,
14+
prevBalances,
15+
} = store.getState().vault;
16+
17+
const chain = isBitcoinBased ? INetworkType.Syscoin : INetworkType.Ethereum;
18+
const chainId = activeNetwork.chainId;
19+
20+
const currentBalance = isBitcoinBased
21+
? accounts[activeAccount.type][activeAccount.id].balances.syscoin
22+
: accounts[activeAccount.type][activeAccount.id].balances.syscoin;
823

24+
const previousBalance = prevBalances[activeAccount.id]?.[chain]?.[chainId];
25+
const currentAccount = accounts[activeAccount.type][activeAccount.id];
26+
const currentAccountTransactions = currentAccount.transactions[chain][
27+
chainId
28+
] as any[];
29+
30+
const hasPendingTx = (currentAccountTransactions ?? []).every(
31+
(tx) => tx.confirmations > 0
32+
);
33+
34+
if (currentBalance === previousBalance && hasPendingTx) {
35+
return false;
36+
}
37+
38+
store.dispatch(
39+
setPrevBalances({
40+
activeAccountId: activeAccount.id,
41+
balance: currentBalance,
42+
chain: isBitcoinBased ? INetworkType.Syscoin : INetworkType.Ethereum,
43+
chainId: activeNetwork.chainId,
44+
})
45+
);
46+
47+
return true;
48+
}
49+
50+
export async function checkForUpdates() {
951
if (isPollingRunNotValid()) {
1052
return;
1153
}
54+
55+
if (!shouldUpdate()) {
56+
return;
57+
}
58+
59+
const { activeAccount, isBitcoinBased, activeNetwork } =
60+
store.getState().vault;
61+
1262
controllerEmitter(
1363
['wallet', 'updateUserNativeBalance'],
1464
[
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-disable camelcase */
2+
3+
import { saveState, setMigratedVersions } from 'state/paliStorage';
4+
import { initialState as initialVaultState } from 'state/vault';
5+
import { IVaultState } from 'state/vault/types';
6+
7+
type V3_3_2 = {
8+
vault: IVaultState;
9+
};
10+
11+
const MigrateRunner = async (oldState: any) => {
12+
try {
13+
const newState: V3_3_2 = {
14+
...oldState,
15+
vault: {
16+
...oldState.vault,
17+
prevBalances: initialVaultState.prevBalances,
18+
},
19+
};
20+
21+
await Promise.all([saveState(newState), setMigratedVersions('3.3.2')]);
22+
23+
console.log('Migrate to <v3.3.2> successfully!');
24+
} catch (error) {
25+
console.log('<v3.3.2> Migration Error');
26+
console.log(error);
27+
}
28+
};
29+
30+
export default MigrateRunner;

source/state/vault/index.ts

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import cloneDeep from 'lodash/cloneDeep';
44
import take from 'lodash/take';
55

66
import {
7-
initialNetworksState,
7+
IKeyringBalances,
88
initialActiveHdAccountState,
99
initialActiveImportedAccountState,
10-
KeyringAccountType,
11-
IWalletState,
12-
IKeyringBalances,
1310
initialActiveTrezorAccountState,
11+
initialNetworksState,
12+
IWalletState,
13+
KeyringAccountType,
1414
} from '@pollum-io/sysweb3-keyring';
1515
import { INetwork, INetworkType } from '@pollum-io/sysweb3-network';
1616
import { INftsStructure } from '@pollum-io/sysweb3-utils';
@@ -89,6 +89,12 @@ export const initialState: IVaultState = {
8989
5700: true,
9090
57000: true,
9191
},
92+
prevBalances: {
93+
[0]: {
94+
[INetworkType.Ethereum]: {},
95+
[INetworkType.Syscoin]: {},
96+
},
97+
},
9298
};
9399

94100
export const getHasEncryptedVault = createAsyncThunk(
@@ -795,13 +801,35 @@ const VaultState = createSlice({
795801
TransactionsType.Ethereum
796802
][chainID] as IEvmTransaction[];
797803

798-
const removedTx = userTransactions.filter(
804+
state.accounts[type][id].transactions[TransactionsType.Ethereum][
805+
chainID
806+
] = userTransactions.filter(
799807
(tx) => tx.hash !== oldTxHash
800808
) as IEvmTransaction[];
809+
},
810+
setPrevBalances(
811+
state: IVaultState,
812+
action: PayloadAction<{
813+
activeAccountId: number;
814+
balance: number;
815+
chain: INetworkType;
816+
chainId: number;
817+
}>
818+
) {
819+
const { activeAccountId, chain, chainId, balance } = action.payload;
801820

802-
state.accounts[type][id].transactions[TransactionsType.Ethereum][
803-
chainID
804-
] = removedTx;
821+
if (!state.prevBalances[activeAccountId]) {
822+
state.prevBalances[activeAccountId] = {
823+
[INetworkType.Ethereum]: {},
824+
[INetworkType.Syscoin]: {},
825+
};
826+
}
827+
828+
if (!state.prevBalances[activeAccountId][chain]) {
829+
state.prevBalances[activeAccountId][chain] = {};
830+
}
831+
832+
state.prevBalances[activeAccountId][chain][chainId] = balance;
805833
},
806834
},
807835

@@ -858,6 +886,7 @@ export const {
858886
setTransactionStatusToAccelerated,
859887
setCoinsList,
860888
setIsLastTxConfirmed,
889+
setPrevBalances,
861890
} = VaultState.actions;
862891

863892
export default VaultState.reducer;

source/state/vault/types.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import { ITokenEthProps, ITokenSysProps } from 'types/tokens';
1515

1616
export interface IVaultState {
17-
accounts: { [key in KeyringAccountType]: PaliAccount }; //todo adjust and guarantee type is correct
17+
accounts: { [key in KeyringAccountType]: PaliAccount };
1818
activeAccount: {
1919
id: number;
2020
type: KeyringAccountType;
@@ -43,6 +43,7 @@ export interface IVaultState {
4343
isTimerEnabled: boolean;
4444
lastLogin: number;
4545
networks: INetworksVault;
46+
prevBalances: IPrevBalances;
4647
shouldShowFaucetModal: { [k: number]: boolean };
4748
timer: number;
4849
}
@@ -56,6 +57,17 @@ export interface INetworksVault {
5657
};
5758
}
5859

60+
export interface IPrevBalances {
61+
[accountId: number]: {
62+
[INetworkType.Ethereum]: {
63+
[chainId: number]: number;
64+
};
65+
[INetworkType.Syscoin]: {
66+
[chainId: number]: number;
67+
};
68+
};
69+
}
70+
5971
export interface IChangingConnectedAccount {
6072
connectedAccountType: KeyringAccountType | undefined;
6173
host: string | undefined;

0 commit comments

Comments
 (0)