Skip to content

Commit

Permalink
Fix/add bg cycle deps checker (#2671)
Browse files Browse the repository at this point in the history
* fix: overview subscribe

* Revert "fix: lazy dispatch (#2668)"

This reverts commit 2b304d5.

* fix: add cycle deps check

* Revert "Revert "fix: lazy dispatch (#2668)""

This reverts commit 1313d28.

* fix: add eslint rule

* fix: lint

* fix: cycle deps
  • Loading branch information
sidmorizon committed Mar 8, 2023
1 parent aed9d48 commit 3b0a597
Show file tree
Hide file tree
Showing 23 changed files with 172 additions and 135 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -19,6 +19,7 @@ const jsRules = {
'react/jsx-no-useless-fragment': ['error', { allowExpressions: true }],
'no-promise-executor-return': 'off',
'default-param-last': 'off',
'import/no-cycle': 'warn',
// 'no-console': [isDev ? 'warn' : 'off'],
};
const tsRules = {
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/@tests/prepareMockVault.ts
Expand Up @@ -4,7 +4,7 @@ import bufferUitls from '@onekeyhq/shared/src/utils/bufferUtils';
import { getCurveByImpl } from '../src/managers/impl';
import { fromDBNetworkToNetwork } from '../src/managers/network';
import { Verifier, fromDBNetworkToChainInfo } from '../src/proxy';
import { createVaultSettings } from '../src/vaults/factory';
import { createVaultSettings } from '../src/vaults/factory.createVaultSettings';

import type { Engine } from '../src';
import type { ExportedCredential } from '../src/dbs/base';
Expand Down
1 change: 0 additions & 1 deletion packages/engine/src/dbs/realms/schemas/index.ts
@@ -1,4 +1,3 @@
/* eslint-disable import/no-cycle */
export { TokenSchema } from './TokenSchema';
export { AccountSchema } from './AccountSchema';
export { WalletSchema } from './WalletSchema';
Expand Down
6 changes: 2 additions & 4 deletions packages/engine/src/index.ts
Expand Up @@ -102,10 +102,8 @@ import {
WALLET_TYPE_WATCHING,
} from './types/wallet';
import { Validators } from './validators';
import {
createVaultHelperInstance,
createVaultSettings,
} from './vaults/factory';
import { createVaultHelperInstance } from './vaults/factory';
import { createVaultSettings } from './vaults/factory.createVaultSettings';
import { getMergedTxs } from './vaults/impl/evm/decoder/history';
import { IDecodedTxActionType } from './vaults/types';
import { VaultFactory } from './vaults/VaultFactory';
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/src/managers/impl.ts
Expand Up @@ -45,7 +45,7 @@ import {

import { NotImplemented } from '../errors';
import { AccountType } from '../types/account';
import { createVaultSettings } from '../vaults/factory';
import { createVaultSettings } from '../vaults/factory.createVaultSettings';

import type { DBAccount } from '../types/account';
import type { AccountNameInfo } from '../types/network';
Expand Down
8 changes: 1 addition & 7 deletions packages/engine/src/managers/network.ts
Expand Up @@ -12,6 +12,7 @@ import { getFiatEndpoint } from '../endpoint';
import { getPresetNetworks, networkIsPreset } from '../presets';

import { getAccountNameInfoByImpl, implToCoinTypes } from './impl';
import { getNetworkImpl } from './network.utils';

import type {
AddEVMNetworkParams,
Expand Down Expand Up @@ -194,13 +195,6 @@ function parseNetworkId(networkId: string): {
};
}

// getNetworkImplFromNetworkId
// getImplFromNetworkId
function getNetworkImpl(networkId: string) {
const [impl] = networkId.split(SEPERATOR);
return impl;
}

function getChainIdFromNetworkId(networkId: string) {
const [, chainId] = networkId.split(SEPERATOR);
return chainId ?? '';
Expand Down
8 changes: 8 additions & 0 deletions packages/engine/src/managers/network.utils.ts
@@ -0,0 +1,8 @@
// getNetworkImplFromNetworkId
// getImplFromNetworkId
import { SEPERATOR } from '@onekeyhq/shared/src/engine/engineConsts';

export function getNetworkImpl(networkId: string) {
const [impl] = networkId.split(SEPERATOR);
return impl;
}
1 change: 1 addition & 0 deletions packages/engine/src/managers/token.ts
Expand Up @@ -117,6 +117,7 @@ export const checkTokenUpdate = async (timestamp: number): Promise<boolean> =>
false,
);

// fetch top 2000 tokens of network
export const fetchOnlineTokens = async (
params: TokenQuery,
): Promise<ServerToken[]> => {
Expand Down
1 change: 0 additions & 1 deletion packages/engine/src/vaults/VaultContext.ts
Expand Up @@ -7,7 +7,6 @@ import {
getWalletIdFromAccountId,
isAccountCompatibleWithNetwork,
} from '../managers/account';
import { parseNetworkId } from '../managers/network';
import { AccountType } from '../types/account';

import type { Engine } from '../index';
Expand Down
103 changes: 103 additions & 0 deletions packages/engine/src/vaults/factory.createVaultSettings.ts
@@ -0,0 +1,103 @@
/* eslint-disable new-cap, @typescript-eslint/require-await, @typescript-eslint/no-unsafe-member-access */
import {
IMPL_ADA,
IMPL_ALGO,
IMPL_APTOS,
IMPL_BCH,
IMPL_BTC,
IMPL_CFX,
IMPL_COSMOS,
IMPL_DOGE,
IMPL_DOT,
IMPL_EVM,
IMPL_FIL,
IMPL_LTC,
IMPL_NEAR,
IMPL_SOL,
IMPL_STC,
IMPL_SUI,
IMPL_TBTC,
IMPL_TRON,
IMPL_XRP,
} from '@onekeyhq/shared/src/engine/engineConsts';

import { OneKeyInternalError } from '../errors';
import { getNetworkImpl } from '../managers/network.utils';

import type { IVaultSettings } from './types';

export function createVaultSettings(options: {
networkId?: string;
impl?: string;
}): IVaultSettings {
if (!options.impl && !options.networkId) {
throw new Error('networkId and impl require at least one parameter');
}
let { impl } = options;
if (options.networkId) {
impl = getNetworkImpl(options.networkId);
}

if (impl === IMPL_EVM) {
return require('./impl/evm/settings').default as IVaultSettings;
}
if (impl === IMPL_NEAR) {
return require('./impl/near/settings').default as IVaultSettings;
}
if (impl === IMPL_CFX) {
return require('./impl/cfx/settings').default as IVaultSettings;
}
if (impl === IMPL_BTC) {
return require('./impl/btc/settings').default as IVaultSettings;
}
if (impl === IMPL_TBTC) {
return require('./impl/tbtc/settings').default as IVaultSettings;
}
if (impl === IMPL_STC) {
return require('./impl/stc/settings').default as IVaultSettings;
}
if (impl === IMPL_SOL) {
return require('./impl/sol/settings').default as IVaultSettings;
}
if (impl === IMPL_TRON) {
return require('./impl/tron/settings').default as IVaultSettings;
}
if (impl === IMPL_APTOS) {
return require('./impl/apt/settings').default as IVaultSettings;
}
if (impl === IMPL_DOGE) {
return require('./impl/doge/settings').default as IVaultSettings;
}
if (impl === IMPL_LTC) {
return require('./impl/ltc/settings').default as IVaultSettings;
}
if (impl === IMPL_ALGO) {
return require('./impl/algo/settings').default as IVaultSettings;
}
if (impl === IMPL_BCH) {
return require('./impl/bch/settings').default as IVaultSettings;
}
if (impl === IMPL_XRP) {
return require('./impl/xrp/settings').default as IVaultSettings;
}
if (impl === IMPL_COSMOS) {
return require('./impl/cosmos/settings').default as IVaultSettings;
}
if (impl === IMPL_ADA) {
return require('./impl/ada/settings').default as IVaultSettings;
}
if (impl === IMPL_SUI) {
return require('./impl/sui/settings').default as IVaultSettings;
}
if (impl === IMPL_FIL) {
return require('./impl/fil/settings').default as IVaultSettings;
}
if (impl === IMPL_DOT) {
return require('./impl/dot/settings').default as IVaultSettings;
}
throw new OneKeyInternalError(
`VaultSettings not found for: networkId=${options.networkId ?? ''}, impl=${
impl ?? ''
}`,
);
}
85 changes: 3 additions & 82 deletions packages/engine/src/vaults/factory.ts
Expand Up @@ -22,13 +22,14 @@ import {
} from '@onekeyhq/shared/src/engine/engineConsts';

import { OneKeyInternalError } from '../errors';
import { getNetworkImpl } from '../managers/network';
import { getNetworkImpl } from '../managers/network.utils';
import {
WALLET_TYPE_EXTERNAL,
WALLET_TYPE_IMPORTED,
WALLET_TYPE_WATCHING,
} from '../types/wallet';

import { createVaultSettings } from './factory.createVaultSettings';
import VaultHelperAda from './impl/ada/VaultHelper';
import VaultHelperAlgo from './impl/algo/VaultHelper';
import VaultHelperAptos from './impl/apt/VaultHelper';
Expand All @@ -50,90 +51,10 @@ import VaultHelperTron from './impl/tron/VaultHelper';
import VaultHelperXrp from './impl/xrp/VaultHelper';

import type { KeyringBase } from './keyring/KeyringBase';
import type {
IVaultFactoryOptions,
IVaultOptions,
IVaultSettings,
} from './types';
import type { IVaultFactoryOptions, IVaultOptions } from './types';
import type { VaultBase } from './VaultBase';
import type { VaultHelperBase } from './VaultHelperBase';

export function createVaultSettings(options: {
networkId?: string;
impl?: string;
}): IVaultSettings {
if (!options.impl && !options.networkId) {
throw new Error('networkId and impl require at least one parameter');
}
let { impl } = options;
if (options.networkId) {
impl = getNetworkImpl(options.networkId);
}

if (impl === IMPL_EVM) {
return require('./impl/evm/settings').default as IVaultSettings;
}
if (impl === IMPL_NEAR) {
return require('./impl/near/settings').default as IVaultSettings;
}
if (impl === IMPL_CFX) {
return require('./impl/cfx/settings').default as IVaultSettings;
}
if (impl === IMPL_BTC) {
return require('./impl/btc/settings').default as IVaultSettings;
}
if (impl === IMPL_TBTC) {
return require('./impl/tbtc/settings').default as IVaultSettings;
}
if (impl === IMPL_STC) {
return require('./impl/stc/settings').default as IVaultSettings;
}
if (impl === IMPL_SOL) {
return require('./impl/sol/settings').default as IVaultSettings;
}
if (impl === IMPL_TRON) {
return require('./impl/tron/settings').default as IVaultSettings;
}
if (impl === IMPL_APTOS) {
return require('./impl/apt/settings').default as IVaultSettings;
}
if (impl === IMPL_DOGE) {
return require('./impl/doge/settings').default as IVaultSettings;
}
if (impl === IMPL_LTC) {
return require('./impl/ltc/settings').default as IVaultSettings;
}
if (impl === IMPL_ALGO) {
return require('./impl/algo/settings').default as IVaultSettings;
}
if (impl === IMPL_BCH) {
return require('./impl/bch/settings').default as IVaultSettings;
}
if (impl === IMPL_XRP) {
return require('./impl/xrp/settings').default as IVaultSettings;
}
if (impl === IMPL_COSMOS) {
return require('./impl/cosmos/settings').default as IVaultSettings;
}
if (impl === IMPL_ADA) {
return require('./impl/ada/settings').default as IVaultSettings;
}
if (impl === IMPL_SUI) {
return require('./impl/sui/settings').default as IVaultSettings;
}
if (impl === IMPL_FIL) {
return require('./impl/fil/settings').default as IVaultSettings;
}
if (impl === IMPL_DOT) {
return require('./impl/dot/settings').default as IVaultSettings;
}
throw new OneKeyInternalError(
`VaultSettings not found for: networkId=${options.networkId ?? ''}, impl=${
impl ?? ''
}`,
);
}

export async function createVaultHelperInstance(
options: IVaultFactoryOptions,
): Promise<VaultHelperBase> {
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/src/vaults/impl/dot/KeyringHardware.ts
Expand Up @@ -21,7 +21,7 @@ import {
} from '@onekeyhq/shared/src/engine/engineConsts';
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';

import { TYPE_PREFIX } from './Vault';
import { TYPE_PREFIX } from './consts';

import type { DotImplOptions } from './types';
import type Vault from './Vault';
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/src/vaults/impl/dot/KeyringHd.ts
Expand Up @@ -23,8 +23,8 @@ import {
} from '@onekeyhq/shared/src/engine/engineConsts';
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';

import { TYPE_PREFIX } from './consts';
import { derivationHdLedger } from './utils';
import { TYPE_PREFIX } from './Vault';

import type { DotImplOptions } from './types';
import type Vault from './Vault';
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/src/vaults/impl/dot/KeyringImported.ts
Expand Up @@ -16,7 +16,7 @@ import { addHexPrefix } from '@onekeyhq/engine/src/vaults/utils/hexUtils';
import { COINTYPE_DOT as COIN_TYPE } from '@onekeyhq/shared/src/engine/engineConsts';
import debugLogger from '@onekeyhq/shared/src/logger/debugLogger';

import { TYPE_PREFIX } from './Vault';
import { TYPE_PREFIX } from './consts';

import type { DotImplOptions } from './types';
import type Vault from './Vault';
Expand Down
7 changes: 0 additions & 7 deletions packages/engine/src/vaults/impl/dot/Vault.ts
Expand Up @@ -83,13 +83,6 @@ import type { Metadata } from '@polkadot/types';
import type { BlockHash, RuntimeVersion } from '@polkadot/types/interfaces';
import type { BaseTxInfo, TypeRegistry } from '@substrate/txwrapper-polkadot';

export const TYPE_PREFIX = {
ecdsa: new Uint8Array([2]),
ed25519: new Uint8Array([0]),
ethereum: new Uint8Array([2]),
sr25519: new Uint8Array([1]),
};

// @ts-ignore
export default class Vault extends VaultBase {
keyringMap = {
Expand Down
6 changes: 6 additions & 0 deletions packages/engine/src/vaults/impl/dot/consts.ts
@@ -0,0 +1,6 @@
export const TYPE_PREFIX = {
ecdsa: new Uint8Array([2]),
ed25519: new Uint8Array([0]),
ethereum: new Uint8Array([2]),
sr25519: new Uint8Array([1]),
};
9 changes: 9 additions & 0 deletions packages/kit-bg/src/BackgroundApiBase.ts
Expand Up @@ -86,9 +86,18 @@ function isExtensionInternalCall(payload: IJsBridgeMessagePayload) {
@backgroundClass()
class BackgroundApiBase implements IBackgroundApiBridge {
constructor() {
this.cycleDepsCheck();
this._initBackgroundPersistor();
}

cycleDepsCheck() {
if (!this.persistor || !this.store || !this.appSelector) {
const msg = `background cycle deps ERROR: redux store failed, some reducer may reference backgroundApiProxy`;
alert(msg);
throw new Error(msg);
}
}

persistor = persistor;

store = store;
Expand Down
3 changes: 3 additions & 0 deletions packages/kit-bg/src/services/ServiceOverview.ts
Expand Up @@ -76,6 +76,9 @@ class ServiceOverview extends ServiceBase {
activeNetworkId: networkId,
activeWalletId,
} = appSelector((s) => s.general);
if (!activeWalletId || !activeAccountId) {
return;
}

const account = await serviceAccount.getAccount({
walletId: activeWalletId || '',
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/src/hooks/useOverview.ts
@@ -1,9 +1,8 @@
import { useMemo } from 'react';

import { useAccount } from '.';

import B from 'bignumber.js';

import { useAccount } from './useAccount';
import { useAppSelector } from './useAppSelector';
import {
useAccountTokenValues,
Expand Down
1 change: 0 additions & 1 deletion packages/kit/src/routes/Modal/types.ts
Expand Up @@ -31,7 +31,6 @@ export type { ManageConnectedSitesRoutesParams } from '../../views/ManageConnect
export type { PushNotificationRoutesParams } from '../../views/PushNotification/types';
export type { RevokeRoutesParams } from './Revoke';
export type { BulkSenderRoutesParams } from './BulkSender';
// eslint-disable-next-line import/no-cycle
export type { NFTMarketRoutesParams } from './NFTMarket';
export type { OverviewModalRoutesParams } from './Overview';
export type { AnnualReportModalParams } from './AnnualReport';
Expand Down

0 comments on commit 3b0a597

Please sign in to comment.