Skip to content
Closed
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: 4 additions & 4 deletions packages/tron-wallet-snap/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { TokenApiClient } from './clients/token-api/TokenApiClient';
import { TronHttpClient } from './clients/tron-http/TronHttpClient';
import { TrongridApiClient } from './clients/trongrid/TrongridApiClient';
import { TronWebFactory } from './clients/tronweb/TronWebFactory';
import { AssetsHandler } from './handlers/assets';
import { AssetsHandler } from './handlers/assets/assets';
import { ClientRequestHandler } from './handlers/clientRequest/clientRequest';
import { CronHandler } from './handlers/cronjob';
import { KeyringHandler } from './handlers/keyring';
import { CronHandler } from './handlers/cronjob/cronjob';
import { KeyringHandler } from './handlers/keyring/keyring';
import { RpcHandler } from './handlers/rpc/rpc';
import { UserInputHandler } from './handlers/userInput';
import { UserInputHandler } from './handlers/user-input/userInput';
import { AccountsRepository } from './services/accounts/AccountsRepository';
import { AccountsService } from './services/accounts/AccountsService';
import { AssetsRepository } from './services/assets/AssetsRepository';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import type {
OnAssetsMarketDataResponse,
} from '@metamask/snaps-sdk';

import type { AssetsService } from '../services/assets/AssetsService';
import type { ILogger } from '../utils/logger';
import { createPrefixedLogger } from '../utils/logger';
import type { AssetsService } from '../../services/assets/AssetsService';
import type { ILogger } from '../../utils/logger';
import { createPrefixedLogger } from '../../utils/logger';

export class AssetsHandler {
readonly #logger: ILogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import type { TransactionRawData } from '../../services/transaction-expiration-r
import type { TransactionsService } from '../../services/transactions/TransactionsService';
import { trxToSun } from '../../utils/conversion';
import { mockLogger } from '../../utils/mockLogger';
import { BackgroundEventMethod } from '../cronjob';
import { BackgroundEventMethod } from '../cronjob/cronjob';
import { ClientRequestHandler } from './clientRequest';
import { ClientRequestMethod, SendErrorCodes } from './types';
import type { OnAmountInputRequestStruct } from './validation';
Expand Down Expand Up @@ -115,7 +115,7 @@ type WithClientRequestHandlerCallback<ReturnValue> = (payload: {
Pick<AccountsService, 'findById' | 'findByIdOrThrow' | 'deriveTronKeypair'>
>;
mockAssetsService: jest.Mocked<
Pick<AssetsService, 'getAssetsByAccountId' | 'getAssetByAccountId'>
Pick<AssetsService, 'getAccountAssetsByIDs' | 'getAccountAssetByID'>
>;
mockSendService: jest.Mocked<
Pick<
Expand Down Expand Up @@ -165,10 +165,10 @@ async function withClientRequestHandler<ReturnValue>(
};

const mockAssetsService: jest.Mocked<
Pick<AssetsService, 'getAssetsByAccountId' | 'getAssetByAccountId'>
Pick<AssetsService, 'getAccountAssetsByIDs' | 'getAccountAssetByID'>
> = {
getAssetsByAccountId: jest.fn(),
getAssetByAccountId: jest.fn(),
getAccountAssetsByIDs: jest.fn(),
getAccountAssetByID: jest.fn(),
};

const mockSendService: jest.Mocked<
Expand Down Expand Up @@ -304,7 +304,7 @@ describe('ClientRequestHandler', () => {
} as unknown as jest.Mocked<AccountsService>;

mockAssetsService = {
getAssetsByAccountId: jest.fn(),
getAccountAssetsByIDs: jest.fn(),
} as unknown as jest.Mocked<AssetsService>;

mockSendService = {} as unknown as jest.Mocked<SendService>;
Expand Down Expand Up @@ -736,7 +736,7 @@ describe('ClientRequestHandler', () => {
mockTronWeb.trx.sign.mockResolvedValue(signedTransaction);

// Mock available resources
mockAssetsService.getAssetsByAccountId.mockResolvedValue([
mockAssetsService.getAccountAssetsByIDs.mockResolvedValue([
{ rawAmount: '5000' }, // Bandwidth
{ rawAmount: '100000' }, // Energy
] as any);
Expand Down Expand Up @@ -781,7 +781,7 @@ describe('ClientRequestHandler', () => {
).toHaveBeenCalledWith('TriggerSmartContract', expect.any(String));
// trx.sign is NOT called - fee computation uses unsigned transactions
expect(mockTronWeb.trx.sign).not.toHaveBeenCalled();
expect(mockAssetsService.getAssetsByAccountId).toHaveBeenCalledWith(
expect(mockAssetsService.getAccountAssetsByIDs).toHaveBeenCalledWith(
TEST_ACCOUNT_ID,
[Networks[scope].bandwidth.id, Networks[scope].energy.id],
);
Expand Down Expand Up @@ -873,7 +873,7 @@ describe('ClientRequestHandler', () => {
};
mockTronWeb.trx.sign.mockResolvedValue(signedTransaction);

mockAssetsService.getAssetsByAccountId.mockResolvedValue([
mockAssetsService.getAccountAssetsByIDs.mockResolvedValue([
{ rawAmount: '1000' }, // Bandwidth
{ rawAmount: '0' }, // Energy (not needed for native transfer)
] as any);
Expand Down Expand Up @@ -966,7 +966,7 @@ describe('ClientRequestHandler', () => {
});

// No resources available
mockAssetsService.getAssetsByAccountId.mockResolvedValue([
mockAssetsService.getAccountAssetsByIDs.mockResolvedValue([
undefined, // No bandwidth asset
undefined, // No energy asset
] as any);
Expand Down Expand Up @@ -1490,7 +1490,7 @@ describe('ClientRequestHandler - signAndSendTransaction', () => {
} as unknown as jest.Mocked<AccountsService>;

mockAssetsService = {
getAssetsByAccountId: jest.fn(),
getAccountAssetsByIDs: jest.fn(),
} as unknown as jest.Mocked<AssetsService>;

mockSendService = {} as unknown as jest.Mocked<SendService>;
Expand Down Expand Up @@ -1762,7 +1762,7 @@ describe('ClientRequestHandler - onAmountInput', () => {
];

mockAccountsService.findById.mockResolvedValue(mockAccount);
mockAssetsService.getAssetsByAccountId.mockResolvedValue(mockAssets);
mockAssetsService.getAccountAssetsByIDs.mockResolvedValue(mockAssets);

const result = await handler.handle(request);

Expand Down Expand Up @@ -1820,7 +1820,7 @@ describe('ClientRequestHandler - onAmountInput', () => {
];

mockAccountsService.findById.mockResolvedValue(mockAccount);
mockAssetsService.getAssetsByAccountId.mockResolvedValue(mockAssets);
mockAssetsService.getAccountAssetsByIDs.mockResolvedValue(mockAssets);
mockSendService.buildTransaction.mockResolvedValue(builtTransaction);
mockFeeCalculatorService.computeFee.mockResolvedValue(mockFees);

Expand Down Expand Up @@ -1892,7 +1892,7 @@ describe('ClientRequestHandler - onAmountInput', () => {
];

mockAccountsService.findById.mockResolvedValue(mockAccount);
mockAssetsService.getAssetsByAccountId.mockResolvedValue(mockAssets);
mockAssetsService.getAccountAssetsByIDs.mockResolvedValue(mockAssets);
mockSendService.buildTransaction.mockResolvedValue(builtTransaction);
mockFeeCalculatorService.computeFee.mockResolvedValue(mockFees);

Expand Down Expand Up @@ -1943,7 +1943,7 @@ describe('ClientRequestHandler - onAmountInput', () => {
];

mockAccountsService.findById.mockResolvedValue(mockAccount);
mockAssetsService.getAssetsByAccountId.mockResolvedValue(mockAssets);
mockAssetsService.getAccountAssetsByIDs.mockResolvedValue(mockAssets);

const result = await handler.handle(request);

Expand Down Expand Up @@ -2004,7 +2004,7 @@ describe('ClientRequestHandler - onAmountInput', () => {
];

mockAccountsService.findById.mockResolvedValue(mockAccount);
mockAssetsService.getAssetsByAccountId.mockResolvedValue(mockAssets);
mockAssetsService.getAccountAssetsByIDs.mockResolvedValue(mockAssets);
mockSendService.buildTransaction.mockResolvedValue(builtTransaction);
mockFeeCalculatorService.computeFee.mockResolvedValue(mockFees);

Expand Down Expand Up @@ -2094,10 +2094,10 @@ describe('ClientRequestHandler - computeStakeFee', () => {
const nativeAssetId = Networks[scope].nativeToken.id;

// Mock native balance and resources
mockAssetsService.getAssetByAccountId.mockResolvedValue({
mockAssetsService.getAccountAssetByID.mockResolvedValue({
uiAmount: '100',
} as AssetEntity);
mockAssetsService.getAssetsByAccountId.mockResolvedValue([
mockAssetsService.getAccountAssetsByIDs.mockResolvedValue([
{ rawAmount: '5000' }, // Bandwidth
{ rawAmount: '100000' }, // Energy
] as AssetEntity[]);
Expand Down Expand Up @@ -2130,11 +2130,11 @@ describe('ClientRequestHandler - computeStakeFee', () => {
'ENERGY',
'TGJn1wnUYHJbvN88cynZbsAz2EMeZq73yx',
);
expect(mockAssetsService.getAssetByAccountId).toHaveBeenCalledWith(
expect(mockAssetsService.getAccountAssetByID).toHaveBeenCalledWith(
TEST_ACCOUNT_ID,
nativeAssetId,
);
expect(mockAssetsService.getAssetsByAccountId).toHaveBeenCalledWith(
expect(mockAssetsService.getAccountAssetsByIDs).toHaveBeenCalledWith(
TEST_ACCOUNT_ID,
[Networks[scope].bandwidth.id, Networks[scope].energy.id],
);
Expand Down Expand Up @@ -2179,7 +2179,7 @@ describe('ClientRequestHandler - computeStakeFee', () => {
} as any);

// Account has only 5 TRX
mockAssetsService.getAssetByAccountId.mockResolvedValue({
mockAssetsService.getAccountAssetByID.mockResolvedValue({
uiAmount: '5',
} as AssetEntity);

Expand Down Expand Up @@ -2237,7 +2237,7 @@ describe('ClientRequestHandler - confirmSend validation', () => {
uiAmount: '100',
rawAmount: '100000000',
} as NativeAsset;
mockAssetsService.getAssetByAccountId.mockResolvedValue(mockAsset);
mockAssetsService.getAccountAssetByID.mockResolvedValue(mockAsset);

// validateSend returns insufficient balance
mockSendService.validateSend.mockResolvedValue({
Expand Down Expand Up @@ -2306,7 +2306,7 @@ describe('ClientRequestHandler - confirmSend validation', () => {
uiAmount: '100',
rawAmount: '100000000',
} as NativeAsset;
mockAssetsService.getAssetByAccountId.mockResolvedValue(mockAsset);
mockAssetsService.getAccountAssetByID.mockResolvedValue(mockAsset);

// validateSend returns insufficient balance to cover fee
mockSendService.validateSend.mockResolvedValue({
Expand Down Expand Up @@ -2368,13 +2368,13 @@ describe('ClientRequestHandler - confirmSend validation', () => {
uiAmount: '100',
rawAmount: '100000000',
} as NativeAsset;
mockAssetsService.getAssetByAccountId.mockResolvedValue(mockAsset);
mockAssetsService.getAccountAssetByID.mockResolvedValue(mockAsset);

// validateSend returns valid.
mockSendService.validateSend.mockResolvedValue({ valid: true });

// Mock the rest of the flow.
mockAssetsService.getAssetsByAccountId.mockResolvedValue([
mockAssetsService.getAccountAssetsByIDs.mockResolvedValue([
{ rawAmount: '1000' }, // Bandwidth
{ rawAmount: '50000' }, // Energy
] as any);
Expand Down Expand Up @@ -2530,7 +2530,7 @@ describe('ClientRequestHandler - confirmSend validation', () => {
} as any);

// Asset not found
(mockAssetsService.getAssetByAccountId as jest.Mock).mockResolvedValue(
(mockAssetsService.getAccountAssetByID as jest.Mock).mockResolvedValue(
null,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
assertTransactionSignerConsistency,
assertTransactionStructure,
} from '../../validation/transaction';
import { BackgroundEventMethod } from '../cronjob';
import { BackgroundEventMethod } from '../cronjob/cronjob';
import { ClientRequestMethod, SendErrorCodes } from './types';
import {
ClaimTrxStakingRewardsRequestStruct,
Expand Down Expand Up @@ -370,7 +370,7 @@ export class ClientRequestHandler {
const scope = chainId as Network;

const [asset, nativeTokenAsset, bandwidthAsset, energyAsset] =
await this.#assetsService.getAssetsByAccountId(accountId, [
await this.#assetsService.getAccountAssetsByIDs(accountId, [
assetId,
Networks[scope].nativeToken.id,
Networks[scope].bandwidth.id,
Expand Down Expand Up @@ -483,7 +483,7 @@ export class ClientRequestHandler {
};
}

const asset = await this.#assetsService.getAssetByAccountId(
const asset = await this.#assetsService.getAccountAssetByID(
fromAccountId,
assetId,
);
Expand Down Expand Up @@ -527,7 +527,7 @@ export class ClientRequestHandler {
/**
* Get available Energy and Bandwidth from account assets.
*/
this.#assetsService.getAssetsByAccountId(fromAccountId, [
this.#assetsService.getAccountAssetsByIDs(fromAccountId, [
Networks[scope].bandwidth.id,
Networks[scope].energy.id,
]),
Expand Down Expand Up @@ -673,7 +673,7 @@ export class ClientRequestHandler {
* Get available Energy and Bandwidth from account assets.
*/
const [bandwidthAsset, energyAsset] =
await this.#assetsService.getAssetsByAccountId(accountId, [
await this.#assetsService.getAccountAssetsByIDs(accountId, [
Networks[scope].bandwidth.id,
Networks[scope].energy.id,
]);
Expand Down Expand Up @@ -728,7 +728,7 @@ export class ClientRequestHandler {

const scope = Network.Mainnet;

const asset = await this.#assetsService.getAssetByAccountId(
const asset = await this.#assetsService.getAccountAssetByID(
fromAccountId,
Networks[scope].nativeToken.id,
);
Expand Down Expand Up @@ -759,7 +759,7 @@ export class ClientRequestHandler {
* Get available Energy and Bandwidth from account assets.
*/
const [bandwidthAsset, energyAsset] =
await this.#assetsService.getAssetsByAccountId(fromAccountId, [
await this.#assetsService.getAccountAssetsByIDs(fromAccountId, [
Networks[scope].bandwidth.id,
Networks[scope].energy.id,
]);
Expand Down Expand Up @@ -804,7 +804,7 @@ export class ClientRequestHandler {
const { accountId, assetId, value } = request.params;

await this.#accountsService.findByIdOrThrow(accountId);
const asset = await this.#assetsService.getAssetByAccountId(
const asset = await this.#assetsService.getAccountAssetByID(
accountId,
assetId,
);
Expand Down Expand Up @@ -851,7 +851,7 @@ export class ClientRequestHandler {

const account = await this.#accountsService.findByIdOrThrow(fromAccountId);

const asset = await this.#assetsService.getAssetByAccountId(
const asset = await this.#assetsService.getAccountAssetByID(
fromAccountId,
assetId,
);
Expand Down Expand Up @@ -913,7 +913,7 @@ export class ClientRequestHandler {
const stakedAssetId = `${assetId}-staked-for-${purpose.toLowerCase()}`;

await this.#accountsService.findByIdOrThrow(accountId);
const asset = await this.#assetsService.getAssetByAccountId(
const asset = await this.#assetsService.getAccountAssetByID(
accountId,
stakedAssetId,
);
Expand Down Expand Up @@ -967,7 +967,7 @@ export class ClientRequestHandler {

const account = await this.#accountsService.findByIdOrThrow(accountId);

const asset = await this.#assetsService.getAssetByAccountId(
const asset = await this.#assetsService.getAccountAssetByID(
accountId,
stakedAssetId,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import type { PriceApiClient } from '../clients/price-api/PriceApiClient';
import type { SnapClient } from '../clients/snap/SnapClient';
import type { TronHttpClient } from '../clients/tron-http/TronHttpClient';
import type { TronWebFactory } from '../clients/tronweb/TronWebFactory';
import type { PriceApiClient } from '../../clients/price-api/PriceApiClient';
import type { SnapClient } from '../../clients/snap/SnapClient';
import type { TronHttpClient } from '../../clients/tron-http/TronHttpClient';
import type { TronWebFactory } from '../../clients/tronweb/TronWebFactory';
import {
Network,
TRACK_TX_INTERVAL,
TRACK_TX_MAX_ATTEMPTS,
} from '../constants';
import type { AccountsService } from '../services/accounts/AccountsService';
import type { State, UnencryptedStateValue } from '../services/state/State';
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';
import { SimulationStatus } from '../services/transaction-scan/types';
import type { TransactionScanResult } from '../services/transaction-scan/types';
import { FetchStatus } from '../types/snap';
import { CONFIRM_SIGN_TRANSACTION_INTERFACE_NAME } from '../ui/confirmation/views/ConfirmSignTransaction/types';
import type { ConfirmSignTransactionContext } from '../ui/confirmation/views/ConfirmSignTransaction/types';
import type { ConfirmTransactionRequestContext } from '../ui/confirmation/views/ConfirmTransactionRequest/types';
import type { ILogger } from '../utils/logger';
} from '../../constants';
import type { AccountsService } from '../../services/accounts/AccountsService';
import type { State, UnencryptedStateValue } from '../../services/state/State';
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';
import { SimulationStatus } from '../../services/transaction-scan/types';
import type { TransactionScanResult } from '../../services/transaction-scan/types';
import { FetchStatus } from '../../types/snap';
import { CONFIRM_SIGN_TRANSACTION_INTERFACE_NAME } from '../../ui/confirmation/views/ConfirmSignTransaction/types';
import type { ConfirmSignTransactionContext } from '../../ui/confirmation/views/ConfirmSignTransaction/types';
import type { ConfirmTransactionRequestContext } from '../../ui/confirmation/views/ConfirmTransactionRequest/types';
import type { ILogger } from '../../utils/logger';
import { BackgroundEventMethod, CronHandler } from './cronjob';

/**
Expand Down Expand Up @@ -66,7 +66,7 @@

const MOCK_BLOCK_TIMESTAMP = 1_700_000_000_000;

const getRefBlockBytes = (number: number) =>

Check failure on line 69 in packages/tron-wallet-snap/src/handlers/cronjob/cronjob.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

Missing return type on function

Check failure on line 69 in packages/tron-wallet-snap/src/handlers/cronjob/cronjob.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

Missing return type on function
number.toString(16).slice(-4).padStart(4, '0');

const createBlock = ({
Expand All @@ -77,7 +77,7 @@
number: number;
timestamp: number;
hashSegment?: string;
}) => ({

Check failure on line 80 in packages/tron-wallet-snap/src/handlers/cronjob/cronjob.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

Missing return type on function

Check failure on line 80 in packages/tron-wallet-snap/src/handlers/cronjob/cronjob.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

Missing return type on function
blockID: `${'0'.repeat(16)}${hashSegment}${'f'.repeat(32)}`,

block_header: {
Expand Down Expand Up @@ -181,7 +181,7 @@
ref_block_hash: '',
expiration: 0,
timestamp: 0,
} as any,

Check failure on line 184 in packages/tron-wallet-snap/src/handlers/cronjob/cronjob.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

Unexpected any. Specify a different type

Check failure on line 184 in packages/tron-wallet-snap/src/handlers/cronjob/cronjob.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

Unexpected any. Specify a different type
accountType: 'tron:eoa',
...overrides,
};
Expand All @@ -205,7 +205,7 @@
methods: ['signMessage', 'signTransaction'],
type: 'tron:eoa',
scopes: [Network.Mainnet],
entropySource: 'entropy-source-1' as any,

Check failure on line 208 in packages/tron-wallet-snap/src/handlers/cronjob/cronjob.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

Unexpected any. Specify a different type

Check failure on line 208 in packages/tron-wallet-snap/src/handlers/cronjob/cronjob.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

Unexpected any. Specify a different type
derivationPath: "m/44'/195'/0'/0/0",
index: 0,
},
Expand Down Expand Up @@ -639,7 +639,7 @@

// The final update should have scanFetchStatus: FetchStatus.Error
const lastUpdateCall = mockSnapClient.updateInterface.mock.calls[1];
const contextArg = lastUpdateCall?.[2] as any;

Check failure on line 642 in packages/tron-wallet-snap/src/handlers/cronjob/cronjob.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

Unexpected any. Specify a different type

Check failure on line 642 in packages/tron-wallet-snap/src/handlers/cronjob/cronjob.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

Unexpected any. Specify a different type
expect(contextArg?.scanFetchStatus).toBe(FetchStatus.Error);
},
);
Expand Down Expand Up @@ -1017,7 +1017,7 @@
const mockAccount = { id: ACCOUNT_ID, type: 'tron:eoa' };
mockTronHttpClient.getTransactionInfoById.mockResolvedValue({
blockNumber: 100,
} as any);

Check failure on line 1020 in packages/tron-wallet-snap/src/handlers/cronjob/cronjob.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

Unexpected any. Specify a different type

Check failure on line 1020 in packages/tron-wallet-snap/src/handlers/cronjob/cronjob.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (lint:eslint)

Unexpected any. Specify a different type
mockAccountsService.findByIds.mockResolvedValue([mockAccount]);

await cronHandler.trackTransaction({
Expand Down
Loading
Loading