Skip to content
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
8 changes: 0 additions & 8 deletions eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -1502,14 +1502,6 @@
"count": 7
}
},
"packages/tron-wallet-snap/src/services/state/State.test.ts": {
"@typescript-eslint/explicit-function-return-type": {
"count": 1
},
"@typescript-eslint/no-explicit-any": {
"count": 1
}
},
"packages/tron-wallet-snap/src/services/transaction-scan/TransactionScanService.ts": {
"no-restricted-syntax": {
"count": 5
Expand Down
44 changes: 2 additions & 42 deletions packages/snap-networks-utils/src/utils/cache/StateCache.test.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,10 @@
/* eslint-disable jest/prefer-strict-equal */

import { get, set, unset } from 'lodash';

import { Logger, LogLevel } from '../logger/Logger';
import type { Serializable } from '../serialization/types';
import type { CacheStateManager } from './StateCache';
import type { StateValue } from './StateCache';
import { InMemoryState } from '../state/InMemoryState';
import type { CacheStateManager, StateValue } from './StateCache';
import { StateCache } from './StateCache';

/**
* A simple implementation of a state manager that relies on an in-memory state,
* used for testing purposes.
*/
class InMemoryState implements CacheStateManager<StateValue> {
#state: StateValue;

constructor(initialState: StateValue) {
this.#state = initialState;
}

async get(): Promise<StateValue> {
return this.#state;
}

async getKey<TKey extends Serializable>(
key: string,
): Promise<TKey | undefined> {
return get(this.#state, key) as TKey | undefined;
}

async setKey(key: string, value: Serializable): Promise<void> {
set(this.#state, key, value); // Use lodash to set the value using a json path
}

async update(
callback: (state: StateValue) => StateValue,
): Promise<StateValue> {
return (this.#state = callback(this.#state));
}

async deleteKey(key: string): Promise<void> {
// Using lodash's unset to leverage the json path capabilities
unset(this.#state, key);
}
}

describe('StateCache', () => {
let logger: Logger;

Expand Down
1 change: 0 additions & 1 deletion packages/tron-wallet-snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"@metamask/superstruct": "^3.4.1",
"@metamask/utils": "^11.11.0",
"@types/lodash": "^4.17.15",
"async-mutex": "^0.5.0",
"bignumber.js": "^9.3.1",
"concurrently": "^10.0.3",
"dotenv": "^17.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/tron-wallet-snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/internal-snaps.git"
},
"source": {
"shasum": "nbBLLJNTo42N6FSp8TuKDy1SlsTrBCYHXEvSk8dHIQA=",
"shasum": "Ur39uZHrtG9fCP8q7mzi14zsrtqB3F/4BKHq0dwZxiU=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
15 changes: 6 additions & 9 deletions packages/tron-wallet-snap/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import {
AssetsProvider,
InMemoryCache,
RemoteFeatureFlagsProvider,
State,
StateCache,
} from '@metamask/snap-networks-utils';
import type {
AssetsProviderMessenger,
IStateManager,
RemoteFeatureFlagsProviderMessenger,
} from '@metamask/snap-networks-utils';
import { getMessenger } from '@metamask/snaps-sdk';
Expand Down Expand Up @@ -33,8 +35,8 @@ import { ConfirmationHandler } from './services/confirmation/ConfirmationHandler
import { FeeCalculatorService } from './services/send/FeeCalculatorService';
import { SendService } from './services/send/SendService';
import { StakingService } from './services/staking/StakingService';
import type { UnencryptedStateValue } from './services/state/State';
import { State } from './services/state/State';
import { DEFAULT_UNENCRYPTED_STATE } from './services/state/stateTypes';
import type { UnencryptedStateValue } from './services/state/stateTypes';
import { TransactionExpirationRefresherService } from './services/transaction-expiration-refresher/TransactionExpirationRefresherService';
import { TransactionScanService } from './services/transaction-scan/TransactionScanService';
import { TransactionsRepository } from './services/transactions/TransactionsRepository';
Expand All @@ -59,12 +61,7 @@ export const configProvider = new ConfigProvider();

const state = new State({
encrypted: false,
defaultState: {
keyringAccounts: {},
assets: {},
transactions: {},
mapInterfaceNameToId: {},
},
defaultState: DEFAULT_UNENCRYPTED_STATE,
});

const snapClient = new SnapClient();
Expand Down Expand Up @@ -261,7 +258,7 @@ export type SnapExecutionContext = {
/**
* Services
*/
state: State<UnencryptedStateValue>;
state: IStateManager<UnencryptedStateValue>;
priceApiClient: PriceApiClient;
feeCalculatorService: FeeCalculatorService;
assetsService: AssetsService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { IStateManager } from '@metamask/snap-networks-utils';

import type { PriceApiClient } from '../../clients/price-api/PriceApiClient';
import type { SnapClient } from '../../clients/snap/SnapClient';
import type { TronHttpClient } from '../../clients/tron-http/TronHttpClient';
Expand All @@ -8,7 +10,7 @@ import {
TRACK_TX_MAX_ATTEMPTS,
} from '../../constants';
import type { AccountsService } from '../../services/accounts/AccountsService';
import type { State, UnencryptedStateValue } from '../../services/state/State';
import type { UnencryptedStateValue } from '../../services/state/stateTypes';
import { TransactionExpirationRefresherService } from '../../services/transaction-expiration-refresher/TransactionExpirationRefresherService';
import type { JsonTransactionRawData } from '../../services/transaction-expiration-refresher/types';
import type { TransactionScanService } from '../../services/transaction-scan/TransactionScanService';
Expand Down Expand Up @@ -387,7 +389,7 @@ function buildCronHandler({
logger: mockLogger,
accountsService: {} as AccountsService,
snapClient: mockSnapClient as unknown as SnapClient,
state: mockState as unknown as State<UnencryptedStateValue>,
state: mockState as unknown as IStateManager<UnencryptedStateValue>,
priceApiClient: {} as PriceApiClient,
tronHttpClient: {} as TronHttpClient,
transactionScanService:
Expand Down Expand Up @@ -949,7 +951,7 @@ describe('CronHandler', () => {
logger: mockLogger,
accountsService: mockAccountsService as unknown as AccountsService,
snapClient: mockSnapClient as unknown as SnapClient,
state: {} as unknown as State<UnencryptedStateValue>,
state: {} as unknown as IStateManager<UnencryptedStateValue>,
priceApiClient: {} as PriceApiClient,
tronHttpClient: mockTronHttpClient as unknown as TronHttpClient,
transactionScanService: {} as unknown as TransactionScanService,
Expand Down
8 changes: 4 additions & 4 deletions packages/tron-wallet-snap/src/handlers/cronjob/cronjob.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Logger } from '@metamask/snap-networks-utils';
import type { IStateManager, Logger } from '@metamask/snap-networks-utils';
import type { JsonRpcRequest } from '@metamask/snaps-sdk';

import type { PriceApiClient } from '../../clients/price-api/PriceApiClient';
Expand All @@ -8,7 +8,7 @@ import type { Network } from '../../constants';
import { TRACK_TX_INTERVAL, TRACK_TX_MAX_ATTEMPTS } from '../../constants';
import type { TronKeyringAccount } from '../../entities/keyring-account';
import type { AccountsService } from '../../services/accounts/AccountsService';
import type { State, UnencryptedStateValue } from '../../services/state/State';
import type { UnencryptedStateValue } from '../../services/state/stateTypes';
import type { TransactionExpirationRefresherService } from '../../services/transaction-expiration-refresher/TransactionExpirationRefresherService';
import type {
JsonTransactionRawData,
Expand Down Expand Up @@ -52,7 +52,7 @@ export class CronHandler {

readonly #snapClient: SnapClient;

readonly #state: State<UnencryptedStateValue>;
readonly #state: IStateManager<UnencryptedStateValue>;

readonly #priceApiClient: PriceApiClient;

Expand All @@ -75,7 +75,7 @@ export class CronHandler {
logger: Logger;
accountsService: AccountsService;
snapClient: SnapClient;
state: State<UnencryptedStateValue>;
state: IStateManager<UnencryptedStateValue>;
priceApiClient: PriceApiClient;
tronHttpClient: TronHttpClient;
transactionScanService: TransactionScanService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TrxAccountType, TrxScope } from '@metamask/keyring-api';
import { InMemoryState } from '@metamask/snap-networks-utils';

import type { TronKeyringAccount } from '../../entities/keyring-account';
import { InMemoryState } from '../state/InMemoryState';
import type { UnencryptedStateValue } from '../state/State';
import type { UnencryptedStateValue } from '../state/stateTypes';
import { AccountsRepository } from './AccountsRepository';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { EntropySourceId } from '@metamask/keyring-api';
import type { IStateManager } from '@metamask/snap-networks-utils';

import type { TronKeyringAccount } from '../../entities/keyring-account';
import type { IStateManager } from '../state/IStateManager';
import type { UnencryptedStateValue } from '../state/State';
import type { UnencryptedStateValue } from '../state/stateTypes';

/**
* Range of inclusive account indices to create.
Expand Down Expand Up @@ -198,10 +198,10 @@ export class AccountsRepository {
}

async delete(id: string): Promise<void> {
await Promise.all([
this.#state.deleteKey(`${this.#storageKey}.${id}`),
this.#state.deleteKey(`assets.${id}`),
this.#state.deleteKey(`transactions.${id}`),
await this.#state.deleteKeys([
`${this.#storageKey}.${id}`,
`assets.${id}`,
`transactions.${id}`,
]);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { IStateManager } from '@metamask/snap-networks-utils';

import { KnownCaip19Id, Network } from '../../constants';
import type {
AssetEntity,
NativeAsset,
TokenAsset,
} from '../../entities/assets';
import type { IStateManager } from '../state/IStateManager';
import type { UnencryptedStateValue } from '../state/State';
import type { UnencryptedStateValue } from '../state/stateTypes';
import { AssetsRepository } from './AssetsRepository';
import type { NativeCaipAssetType, TokenCaipAssetType } from './types';

Expand Down Expand Up @@ -109,6 +110,7 @@ describe('AssetsRepository', () => {
return stateValue;
},
deleteKey: async () => undefined,
deleteKeys: async () => undefined,
};

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { IStateManager } from '@metamask/snap-networks-utils';
import { cloneDeep } from 'lodash';

import type { AssetEntity } from '../../entities/assets';
import type { IStateManager } from '../state/IStateManager';
import type { UnencryptedStateValue } from '../state/State';
import type { UnencryptedStateValue } from '../state/stateTypes';

export class AssetsRepository {
readonly #state: IStateManager<UnencryptedStateValue>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
KeyringAccount,
} from '@metamask/keyring-api';
import { emitSnapKeyringEvent } from '@metamask/keyring-snap-sdk';
import type { Logger } from '@metamask/snap-networks-utils';
import type { IStateManager, Logger } from '@metamask/snap-networks-utils';
import type { AssetMetadata, FungibleAssetMetadata } from '@metamask/snaps-sdk';
import type { CaipAssetType } from '@metamask/utils';
import { parseCaipAssetType } from '@metamask/utils';
Expand Down Expand Up @@ -39,7 +39,7 @@ import {
} from '../../../constants';
import type { AssetEntity } from '../../../entities/assets';
import { toUiAmount } from '../../../utils/conversion';
import type { State, UnencryptedStateValue } from '../../state/State';
import type { UnencryptedStateValue } from '../../state/stateTypes';
import type { AssetsRepository } from '../AssetsRepository';
import type {
InLockPeriodCaipAssetType,
Expand Down Expand Up @@ -86,7 +86,7 @@ export class SnapAssetsAdapter {

readonly #assetsRepository: AssetsRepository;

readonly #state: State<UnencryptedStateValue>;
readonly #state: IStateManager<UnencryptedStateValue>;

readonly #trongridApiClient: TrongridApiClient;

Expand All @@ -110,7 +110,7 @@ export class SnapAssetsAdapter {
}: {
logger: Logger;
assetsRepository: AssetsRepository;
state: State<UnencryptedStateValue>;
state: IStateManager<UnencryptedStateValue>;
trongridApiClient: TrongridApiClient;
tronHttpClient: TronHttpClient;
priceApiClient: PriceApiClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Logger } from '@metamask/snap-networks-utils';
import type { IStateManager, Logger } from '@metamask/snap-networks-utils';
import { InternalError } from '@metamask/snaps-sdk';
import { assert } from '@metamask/superstruct';
import { BigNumber } from 'bignumber.js';
Expand Down Expand Up @@ -28,14 +28,14 @@ import { assertTransactionStructure } from '../../validation/transaction';
import type { AssetsService } from '../assets/AssetsService';
import type { FeeCalculatorService } from '../send/FeeCalculatorService';
import type { ComputeFeeResult } from '../send/types';
import type { State, UnencryptedStateValue } from '../state/State';
import type { UnencryptedStateValue } from '../state/stateTypes';

export class ConfirmationHandler {
readonly #logger: Logger;

readonly #snapClient: SnapClient;

readonly #state: State<UnencryptedStateValue>;
readonly #state: IStateManager<UnencryptedStateValue>;

readonly #tronWebFactory: TronWebFactory;

Expand All @@ -52,7 +52,7 @@ export class ConfirmationHandler {
logger,
}: {
snapClient: SnapClient;
state: State<UnencryptedStateValue>;
state: IStateManager<UnencryptedStateValue>;
tronWebFactory: TronWebFactory;
assetsService: AssetsService;
feeCalculatorService: FeeCalculatorService;
Expand Down
Loading
Loading