Skip to content

Commit 5f9beac

Browse files
chore: tmp
1 parent 71b957c commit 5f9beac

4 files changed

Lines changed: 66 additions & 35 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './onStartHandler';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { SubscriptionConnectionManagerPort } from '../../ports/SubscriptionConnectionManagerPort';
2+
3+
export class OnStartHandler {
4+
constructor(
5+
private readonly subscriptionConnectionManager: SubscriptionConnectionManagerPort,
6+
) {}
7+
}

packages/snap/src/core/services/websocket/WebSocketService.ts

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import type { Address, Signature } from '@solana/kit';
22

33
import type { SolanaKeyringAccount } from '../../../entities';
44
import { Network } from '../../constants/solana';
5-
import type { WebSocketTransportPort } from '../../ports/WebSocketTransportPort';
5+
import type {
6+
JsonRpcSubscription,
7+
SubscriptionTransportPort,
8+
} from '../../ports';
69
import type { ILogger } from '../../utils/logger';
710
import type { AssetsService } from '../assets/AssetsService';
811
import type { State } from '../state/State';
@@ -16,19 +19,32 @@ export type SubscriptionInfo = {
1619
createdAt: number;
1720
};
1821

19-
export type WebSocketSubscription = {
20-
id: string;
21-
method: 'accountSubscribe' | 'signatureSubscribe';
22-
params: any[];
23-
callback: (notification: any) => Promise<void>;
24-
};
22+
// export type WebSocketMethod =
23+
// | 'accountSubscribe'
24+
// | 'accountUnsubscribe'
25+
// | 'blockSubscribe'
26+
// | 'blockUnsubscribe'
27+
// | 'logsSubscribe'
28+
// | 'logsUnsubscribe'
29+
// | 'programSubscribe'
30+
// | 'programUnsubscribe'
31+
// | 'rootSubscribe'
32+
// | 'rootUnsubscribe'
33+
// | 'signatureSubscribe'
34+
// | 'signatureUnsubscribe'
35+
// | 'slotSubscribe'
36+
// | 'slotUpdatesSubscribe'
37+
// | 'slotUpdatesUnsubscribe'
38+
// | 'slotUnsubscribe'
39+
// | 'voteSubscribe'
40+
// | 'voteUnsubscribe';
2541

2642
/**
2743
* Service that manages WebSocket subscriptions and handles real-time updates
2844
* for accounts and transactions, replacing the HTTP polling mechanism.
2945
*/
3046
export class WebSocketService {
31-
readonly #webSocketTransport: WebSocketTransportPort;
47+
readonly #subscriptionManager: SubscriptionTransportPort;
3248

3349
readonly #assetsService: AssetsService;
3450

@@ -49,20 +65,20 @@ export class WebSocketService {
4965
readonly #signatureTimeoutMs = 5 * 60 * 1000; // 5 minutes timeout
5066

5167
constructor(
52-
rpcTransport: WebSocketTransportPort,
68+
subscriptionManager: SubscriptionTransportPort,
5369
assetsService: AssetsService,
5470
transactionsService: TransactionsService,
5571
stateService: State<any>,
5672
logger: ILogger,
5773
) {
58-
this.#webSocketTransport = rpcTransport;
74+
this.#subscriptionManager = subscriptionManager;
5975
this.#assetsService = assetsService;
6076
this.#transactionsService = transactionsService;
6177
this.#stateService = stateService;
6278
this.#logger = logger;
6379

6480
// Set up connection recovery callback
65-
this.#webSocketTransport.onConnectionRecovery(async () => {
81+
this.#subscriptionManager.onConnectionRecovery(async () => {
6682
await this.#recoverAllSubscriptions();
6783
});
6884
}
@@ -82,7 +98,7 @@ export class WebSocketService {
8298
);
8399

84100
// Open WebSocket connection
85-
await this.#webSocketTransport.openConnection(network);
101+
await this.#subscriptionManager.openConnection(network);
86102

87103
// Subscribe to all user accounts
88104
await this.subscribeToAllAccounts(accounts, network);
@@ -138,17 +154,21 @@ export class WebSocketService {
138154
}
139155

140156
try {
141-
const subscriptionId = await this.#webSocketTransport.subscribe(
142-
network,
143-
accountAddress,
144-
async (notification) => {
157+
const subscriptionId = 'account_XXXxxxXXx';
158+
const subscription: JsonRpcSubscription = {
159+
id: subscriptionId,
160+
method: 'accountSubscribe',
161+
unsubscribeMethod: 'accountUnsubscribe',
162+
params: [accountAddress],
163+
onNotification: async (notification) => {
145164
await this.#handleAccountNotification(
146165
accountAddress,
147166
notification,
148167
network,
149168
);
150169
},
151-
);
170+
};
171+
await this.#subscriptionManager.subscribe(network, subscription);
152172

153173
// Track the subscription
154174
const subscriptionInfo: SubscriptionInfo = {
@@ -195,18 +215,21 @@ export class WebSocketService {
195215
}
196216

197217
try {
198-
const subscriptionId =
199-
await this.#webSocketTransport.subscribeToSignature(
200-
network,
201-
signature,
202-
async (notification) => {
203-
await this.#handleSignatureNotification(
204-
signature,
205-
notification,
206-
network,
207-
);
208-
},
209-
);
218+
const subscriptionId = 'signature_XXXxxxXXx';
219+
const subscription: JsonRpcSubscription = {
220+
id: subscriptionId,
221+
method: 'signatureSubscribe',
222+
unsubscribeMethod: 'signatureUnsubscribe',
223+
params: [signature],
224+
onNotification: async (notification) => {
225+
await this.#handleSignatureNotification(
226+
signature,
227+
notification,
228+
network,
229+
);
230+
},
231+
};
232+
await this.#subscriptionManager.subscribe(network, subscription);
210233

211234
// Track the subscription
212235
const subscriptionInfo: SubscriptionInfo = {
@@ -255,7 +278,7 @@ export class WebSocketService {
255278
return;
256279
}
257280

258-
await this.#webSocketTransport.unsubscribe(subscriptionId);
281+
await this.#subscriptionManager.unsubscribe(subscriptionId);
259282
this.#activeSubscriptions.delete(subscriptionId);
260283
this.#accountSubscriptions.delete(accountAddress);
261284

@@ -281,7 +304,7 @@ export class WebSocketService {
281304
this.#signatureTimeouts.delete(signature);
282305
}
283306

284-
await this.#webSocketTransport.unsubscribe(subscriptionId);
307+
await this.#subscriptionManager.unsubscribe(subscriptionId);
285308
this.#activeSubscriptions.delete(subscriptionId);
286309
this.#signatureSubscriptions.delete(signature);
287310

@@ -314,7 +337,7 @@ export class WebSocketService {
314337
const unsubscribePromises = Array.from(
315338
this.#activeSubscriptions.keys(),
316339
).map(async (subscriptionId) => {
317-
await this.#webSocketTransport.unsubscribe(subscriptionId);
340+
await this.#subscriptionManager.unsubscribe(subscriptionId);
318341
});
319342

320343
await Promise.allSettled(unsubscribePromises);
@@ -325,7 +348,7 @@ export class WebSocketService {
325348
this.#signatureSubscriptions.clear();
326349

327350
// Close connections
328-
await this.#webSocketTransport.closeConnection(Network.Mainnet);
351+
await this.#subscriptionManager.closeConnection(Network.Mainnet);
329352

330353
this.#logger.info('[WebSocketService] WebSocket service cleaned up');
331354
}
@@ -550,7 +573,7 @@ export class WebSocketService {
550573
/**
551574
* Fetches new transactions for an account since the last known signature.
552575
* @param accountAddress - The account address to fetch new transactions for.
553-
* @param network
576+
* @param network - The network to fetch new transactions for.
554577
*/
555578
async #fetchNewTransactions(
556579
accountAddress: Address,

packages/snap/src/entities/subscriptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Represents a subscription to real-time notifications using JSON-RPC.
2+
* Represents a subscription to real-time notifications using the Solana RPC WebSocket API.
33
*
44
* @property id - Caller-provided ID for the subscription.
55
* @property method - The method to subscribe to.

0 commit comments

Comments
 (0)