Skip to content
Draft
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
108 changes: 0 additions & 108 deletions oxlint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -791,114 +791,6 @@
"count": 1
}
},
"packages/solana-wallet-snap/src/core/services/subscriptions/KeyringAccountMonitor.test.ts": {
"typescript/no-unsafe-argument": {
"count": 2
},
"typescript/no-unsafe-call": {
"count": 3
}
},
"packages/solana-wallet-snap/src/core/services/subscriptions/KeyringAccountMonitor.ts": {
"typescript/explicit-function-return-type": {
"count": 1
}
},
"packages/solana-wallet-snap/src/core/services/subscriptions/SignatureMonitor.test.ts": {
"typescript/no-explicit-any": {
"count": 1
},
"typescript/no-unsafe-argument": {
"count": 3
}
},
"packages/solana-wallet-snap/src/core/services/subscriptions/SignatureMonitor.ts": {
"typescript/no-unnecessary-type-assertion": {
"count": 1
}
},
"packages/solana-wallet-snap/src/core/services/subscriptions/SubscriptionRepository.ts": {
"internal-snaps/no-in-operator": {
"count": 1
}
},
"packages/solana-wallet-snap/src/core/services/subscriptions/SubscriptionService.test.ts": {
"typescript/explicit-function-return-type": {
"count": 6
},
"typescript/no-explicit-any": {
"count": 3
},
"typescript/no-unsafe-argument": {
"count": 3
},
"typescript/no-unsafe-member-access": {
"count": 1
}
},
"packages/solana-wallet-snap/src/core/services/subscriptions/SubscriptionService.ts": {
"internal-snaps/no-in-operator": {
"count": 7
},
"typescript/explicit-function-return-type": {
"count": 1
},
"typescript/no-explicit-any": {
"count": 4
},
"typescript/no-unsafe-argument": {
"count": 2
},
"typescript/no-unsafe-member-access": {
"count": 4
},
"typescript/unbound-method": {
"count": 2
}
},
"packages/solana-wallet-snap/src/core/services/subscriptions/WebSocketConnectionRepository.test.ts": {
"typescript/no-explicit-any": {
"count": 1
},
"typescript/no-unsafe-member-access": {
"count": 1
}
},
"packages/solana-wallet-snap/src/core/services/subscriptions/WebSocketConnectionRepository.ts": {
"typescript/explicit-function-return-type": {
"count": 1
}
},
"packages/solana-wallet-snap/src/core/services/subscriptions/WebSocketConnectionService.test.ts": {
"typescript/no-explicit-any": {
"count": 1
},
"typescript/no-unsafe-member-access": {
"count": 1
},
"vitest/no-conditional-in-test": {
"count": 3
}
},
"packages/solana-wallet-snap/src/core/services/subscriptions/WebSocketConnectionService.ts": {
"typescript/restrict-template-expressions": {
"count": 1
}
},
"packages/solana-wallet-snap/src/core/services/subscriptions/parseWebSocketMessage.ts": {
"internal-snaps/no-in-operator": {
"count": 1
},
"typescript/no-explicit-any": {
"count": 1
},
"typescript/no-unsafe-assignment": {
"count": 3
},
"typescript/no-unsafe-return": {
"count": 1
}
},
"packages/solana-wallet-snap/src/core/services/transaction-scan/TransactionScanService.test.ts": {
"typescript/no-unnecessary-type-assertion": {
"count": 12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import type { Transaction } from '@metamask/keyring-api';
import { TOKEN_PROGRAM_ADDRESS } from '@solana-program/token';
import type { Address } from '@solana/kit';
import { signature } from '@solana/kit';
import type { Mock } from 'vitest';

import type {
AccountNotification,
AccountNotificationHandler,
ConfirmedSubscription,
ProgramNotification,
ProgramNotificationHandler,
SubscribeMethod,
Subscription,
} from '../../../entities';
import { KnownCaip19Id, Network } from '../../constants/solana';
Expand Down Expand Up @@ -107,13 +107,23 @@ describe('KeyringAccountMonitor', () => {
getAll: vi.fn().mockResolvedValue([]),
registerNotificationHandler: vi
.fn()
.mockImplementation(async (method, _network, handler) => {
if (method === 'accountSubscribe') {
accountNotificationHandlers.push(handler);
} else if (method === 'programSubscribe') {
programNotificationHandlers.push(handler);
}
}),
.mockImplementation(
async (
method: SubscribeMethod,
_network: Network,
handler: AccountNotificationHandler | ProgramNotificationHandler,
) => {
if (method === 'accountSubscribe') {
accountNotificationHandlers.push(
handler as AccountNotificationHandler,
);
} else if (method === 'programSubscribe') {
programNotificationHandlers.push(
handler as ProgramNotificationHandler,
);
}
},
),
registerConnectionRecoveryHandler: vi.fn(),
} as unknown as SubscriptionService;

Expand Down Expand Up @@ -719,9 +729,9 @@ describe('KeyringAccountMonitor', () => {
const syncSpy = vi.spyOn(mockAccountsSynchronizer, 'synchronize');

// Simulate connection recovery
const recoveryHandler = (
mockSubscriptionService.registerConnectionRecoveryHandler as Mock
).mock.calls[0]?.[1];
const recoveryHandler = vi.mocked(
mockSubscriptionService.registerConnectionRecoveryHandler,
).mock.calls[0]![1];
await recoveryHandler(Network.Mainnet);

expect(syncSpy).toHaveBeenCalledTimes(1);
Expand All @@ -740,9 +750,9 @@ describe('KeyringAccountMonitor', () => {
const syncSpy = vi.spyOn(mockAccountsSynchronizer, 'synchronize');

// Simulate connection recovery
const recoveryHandler = (
mockSubscriptionService.registerConnectionRecoveryHandler as Mock
).mock.calls[0]?.[1];
const recoveryHandler = vi.mocked(
mockSubscriptionService.registerConnectionRecoveryHandler,
).mock.calls[0]![1];
await recoveryHandler(Network.Mainnet);

expect(syncSpy).toHaveBeenCalledWith(accounts);
Expand All @@ -754,9 +764,9 @@ describe('KeyringAccountMonitor', () => {
const syncSpy = vi.spyOn(mockAccountsSynchronizer, 'synchronize');

// Simulate connection recovery
const recoveryHandler = (
mockSubscriptionService.registerConnectionRecoveryHandler as Mock
).mock.calls[0]?.[1];
const recoveryHandler = vi.mocked(
mockSubscriptionService.registerConnectionRecoveryHandler,
).mock.calls[0]![1];
await recoveryHandler(Network.Mainnet);

expect(syncSpy).not.toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class KeyringAccountMonitor {
this.#accountsSynchronizer.synchronize(accounts);

const promises = accounts.flatMap((account) => {
const shouldMonitorOnNetwork = (network: Network) =>
const shouldMonitorOnNetwork = (network: Network): boolean =>
account.scopes.includes(network);

// Monitor native assets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
ConnectionRecoveryHandler,
SignatureNotification,
SignatureNotificationHandler,
SubscribeMethod,
Subscription,
} from '../../../entities';
import { Network } from '../../constants/solana';
Expand All @@ -16,8 +17,8 @@ import * as Errors from '../../utils/errors';
import { trackError } from '../../utils/errors';
import { mockLogger } from '../__mocks__/logger';
import type { AccountsService } from '../accounts';
import type { SolanaConnection } from '../connection';
import type { TransactionsService } from '../transactions';
import type { SignatureMonitorConnection } from './SignatureMonitor';
import { SignatureMonitor } from './SignatureMonitor';
import type { SubscriptionService } from './SubscriptionService';

Expand All @@ -27,7 +28,7 @@ describe('SignatureMonitor', () => {
let mockAccountService: AccountsService;
let mockTransactionsService: TransactionsService;
let mockAnalyticsService: AnalyticsService;
let mockConnection: SolanaConnection;
let mockConnection: SignatureMonitorConnection;

const mockAccount = MOCK_SOLANA_KEYRING_ACCOUNTS[0];

Expand Down Expand Up @@ -58,16 +59,24 @@ describe('SignatureMonitor', () => {
unsubscribe: vi.fn(),
registerNotificationHandler: vi
.fn()
.mockImplementation(async (_method, _network, handler) => {
// This will let us capture and manually call the handler
notificationHandlers.push(handler);
}),
.mockImplementation(
async (
_method: SubscribeMethod,
_network: Network,
handler: SignatureNotificationHandler,
) => {
// This will let us capture and manually call the handler
notificationHandlers.push(handler);
},
),
registerConnectionRecoveryHandler: vi
.fn()
.mockImplementation(async (_network, handler) => {
// This will let us capture and manually call the handler
connectionRecoveryHandlers.push(handler);
}),
.mockImplementation(
async (_network: Network, handler: ConnectionRecoveryHandler) => {
// This will let us capture and manually call the handler
connectionRecoveryHandlers.push(handler);
},
),
} as unknown as SubscriptionService;

mockAccountService = {
Expand All @@ -87,12 +96,12 @@ describe('SignatureMonitor', () => {
mockConnection = {
getRpc: vi.fn().mockImplementation(() => ({
getSignatureStatuses: vi.fn().mockImplementation(() => ({
send: vi.fn().mockReturnValue({
send: vi.fn().mockResolvedValue({
value: [{ confirmationStatus: 'confirmed' }],
}),
})),
})),
} as unknown as SolanaConnection;
};

signatureMonitor = new SignatureMonitor(
mockSubscriptionService,
Expand Down Expand Up @@ -391,11 +400,11 @@ describe('SignatureMonitor', () => {
// The transaction is only processed, not confirmed
vi.spyOn(mockConnection, 'getRpc').mockReturnValue({
getSignatureStatuses: vi.fn().mockReturnValue({
send: vi.fn().mockReturnValue({
send: vi.fn().mockResolvedValue({
value: [{ confirmationStatus: 'processed' }],
}),
}),
} as any);
});

await signatureMonitor.monitor(
signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AnalyticsService, Logger } from '@metamask/snap-networks-utils';
import { assert, string } from '@metamask/superstruct';
import { Duration } from '@metamask/utils';
import { signature as asSignature } from '@solana/kit';
import type { Rpc, SolanaRpcApi } from '@solana/kit';
import { get } from 'lodash';

import { CommitmentStruct } from '../../../entities';
Expand All @@ -16,10 +17,39 @@ import type { Network } from '../../constants/solana';
import { trackError } from '../../utils/errors';
import type { AccountsService } from '../accounts/AccountsService';
import { SUPPORTED_NETWORKS } from '../config/ConfigProvider';
import type { SolanaConnection } from '../connection';
import type { TransactionsService } from '../transactions';
import type { SubscriptionService } from './SubscriptionService';

/**
* The slice of the Solana RPC client that {@link SignatureMonitor} reads from.
*
* The monitor only looks at the confirmation status of a signature, so the
* client is narrowed here to that single call rather than to the whole
* `Rpc<SolanaRpcApi>`. A real RPC client satisfies it, and it documents — and
* lets callers and tests provide — exactly what is used.
*/
export type SignatureStatusesRpc = {
getSignatureStatuses: (
...args: Parameters<Rpc<SolanaRpcApi>['getSignatureStatuses']>
) => {
send: () => Promise<{
value: readonly ({
readonly confirmationStatus?: Commitment | null;
} | null)[];
}>;
};
};

/**
* The slice of the connection service that {@link SignatureMonitor} needs.
*
* Only the RPC client is read from, so the monitor depends on that single
* accessor instead of the whole connection service.
*/
export type SignatureMonitorConnection = {
getRpc: (network: Network) => SignatureStatusesRpc;
};

export class SignatureMonitor {
readonly #subscriptionService: SubscriptionService;

Expand All @@ -29,7 +59,7 @@ export class SignatureMonitor {

readonly #analyticsService: AnalyticsService;

readonly #connection: SolanaConnection;
readonly #connection: SignatureMonitorConnection;

readonly #logger: Logger;

Expand All @@ -48,7 +78,7 @@ export class SignatureMonitor {
accountService: AccountsService,
transactionsService: TransactionsService,
analyticsService: AnalyticsService,
connection: SolanaConnection,
connection: SignatureMonitorConnection,
logger: Logger,
) {
this.#subscriptionService = subscriptionService;
Expand Down Expand Up @@ -195,9 +225,7 @@ export class SignatureMonitor {
accountType: account.type,
chainIdCaip: transaction.chain,
transactionStatus: transaction.status,
transactionType:
transactionType ??
(transaction.type as TransactionType | undefined),
transactionType: transactionType ?? transaction.type,
});
break;
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IStateManager } from '@metamask/snap-networks-utils';
import { hasProperty } from '@metamask/utils';

import type {
ConfirmedSubscription,
Expand Down Expand Up @@ -69,7 +70,7 @@ export class SubscriptionRepository {

return subscriptions.find(
(subscription) =>
key in subscription &&
hasProperty(subscription, key) &&
subscription[key as keyof Subscription] === value,
);
}
Expand Down
Loading
Loading