Skip to content

Commit e790109

Browse files
authored
chore: standardize updates between websockets and socket.io
* chore: function structures * chore: return accepted and streamed microblocks on socket.io block updates * chore: move ws-rpc and socket-io to ws/ directory * feat: add individual tx updates to socket.io * fix: use updated microblock txs query function * chore: add new schemas for block, microblock, mempool rpcs * feat: handle new block, microblock, mempool websocket updates * fix: socket.io type imports * chore: add test for websocket mempool update * chore: add test for block updates * chore: add microblock update test * chore: remove additional properties from rpc schemas * chore: add client support for new websocket events
1 parent e1c6882 commit e790109

35 files changed

+853
-134
lines changed

client/src/socket-io/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ export class StacksApiSocketClient {
113113
this.handleSubscription(`address-stx-balance:${address}` as const, false);
114114
}
115115

116+
subscribeTransaction(txId: string) {
117+
return this.handleSubscription(`transaction:${txId}` as const, true);
118+
}
119+
120+
unsubscribeTransaction(txId: string) {
121+
this.handleSubscription(`transaction:${txId}` as const, false);
122+
}
123+
116124
logEvents() {
117125
this.socket.on('connect', () => console.log('socket connected'));
118126
this.socket.on('disconnect', reason => console.warn('disconnected', reason));

client/src/ws/index.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import {
88
RpcAddressBalanceSubscriptionParams,
99
RpcAddressBalanceNotificationParams,
1010
RpcSubscriptionType,
11+
Block,
12+
RpcBlockSubscriptionParams,
13+
Microblock,
14+
Transaction,
15+
RpcMicroblockSubscriptionParams,
16+
RpcMempoolSubscriptionParams,
1117
} from '@stacks/stacks-blockchain-api-types';
1218
import { BASE_PATH } from '../generated/runtime';
1319

@@ -26,6 +32,9 @@ export class StacksApiWebSocketClient {
2632
>();
2733

2834
eventEmitter = new EventEmitter<{
35+
block: (event: Block) => void;
36+
microblock: (event: Microblock) => void;
37+
mempool: (event: Transaction) => void;
2938
txUpdate: (event: RpcTxUpdateNotificationParams) => any;
3039
addressTxUpdate: (event: RpcAddressTxNotificationParams) => void;
3140
addressBalanceUpdate: (event: RpcAddressBalanceNotificationParams) => void;
@@ -95,6 +104,15 @@ export class StacksApiWebSocketClient {
95104
data.params as RpcAddressBalanceNotificationParams
96105
);
97106
break;
107+
case 'block':
108+
this.eventEmitter.emit('block', data.params as Block);
109+
break;
110+
case 'microblock':
111+
this.eventEmitter.emit('microblock', data.params as Microblock);
112+
break;
113+
case 'mempool':
114+
this.eventEmitter.emit('mempool', data.params as Transaction);
115+
break;
98116
}
99117
}
100118

@@ -106,6 +124,51 @@ export class StacksApiWebSocketClient {
106124
});
107125
}
108126

127+
async subscribeBlocks(update: (event: Block) => any): Promise<Subscription> {
128+
const params: RpcBlockSubscriptionParams = { event: 'block' };
129+
await this.rpcCall('subscribe', params);
130+
const listener = (event: Block) => {
131+
update(event);
132+
};
133+
this.eventEmitter.addListener('block', listener);
134+
return {
135+
unsubscribe: () => {
136+
this.eventEmitter.removeListener('block', listener);
137+
return this.rpcCall('unsubscribe', params);
138+
},
139+
};
140+
}
141+
142+
async subscribeMicroblocks(update: (event: Microblock) => any): Promise<Subscription> {
143+
const params: RpcMicroblockSubscriptionParams = { event: 'microblock' };
144+
await this.rpcCall('subscribe', params);
145+
const listener = (event: Microblock) => {
146+
update(event);
147+
};
148+
this.eventEmitter.addListener('microblock', listener);
149+
return {
150+
unsubscribe: () => {
151+
this.eventEmitter.removeListener('microblock', listener);
152+
return this.rpcCall('unsubscribe', params);
153+
},
154+
};
155+
}
156+
157+
async subscribeMempool(update: (event: Transaction) => any): Promise<Subscription> {
158+
const params: RpcMempoolSubscriptionParams = { event: 'mempool' };
159+
await this.rpcCall('subscribe', params);
160+
const listener = (event: Transaction) => {
161+
update(event);
162+
};
163+
this.eventEmitter.addListener('mempool', listener);
164+
return {
165+
unsubscribe: () => {
166+
this.eventEmitter.removeListener('mempool', listener);
167+
return this.rpcCall('unsubscribe', params);
168+
},
169+
};
170+
}
171+
109172
async subscribeTxUpdates(
110173
txId: string,
111174
update: (event: RpcTxUpdateNotificationParams) => any

docs/entities/ws-rpc/rpc-address-balance-notification-params.schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"address",
88
"balance"
99
],
10+
"additionalProperties": false,
1011
"properties": {
1112
"address": {
1213
"type": "string"

docs/entities/ws-rpc/rpc-address-balance-notification-response.schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"method",
99
"params"
1010
],
11+
"additionalProperties": false,
1112
"properties": {
1213
"jsonrpc": {
1314
"type": "string",

docs/entities/ws-rpc/rpc-address-balance-subscription-params.schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"event",
88
"address"
99
],
10+
"additionalProperties": false,
1011
"properties": {
1112
"event": {
1213
"type": "string",

docs/entities/ws-rpc/rpc-address-balance-subscription-request.schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"method",
1010
"params"
1111
],
12+
"additionalProperties": false,
1213
"properties": {
1314
"jsonrpc": {
1415
"type": "string",

docs/entities/ws-rpc/rpc-address-tx-notification-params.schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"tx_type",
1010
"tx_status"
1111
],
12+
"additionalProperties": false,
1213
"properties": {
1314
"address": {
1415
"type": "string"

docs/entities/ws-rpc/rpc-address-tx-notification-response.schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"method",
99
"params"
1010
],
11+
"additionalProperties": false,
1112
"properties": {
1213
"jsonrpc": {
1314
"type": "string",

docs/entities/ws-rpc/rpc-address-tx-subscription-params.schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"event",
88
"address"
99
],
10+
"additionalProperties": false,
1011
"properties": {
1112
"event": {
1213
"type": "string",

docs/entities/ws-rpc/rpc-address-tx-subscription-request.schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"method",
1010
"params"
1111
],
12+
"additionalProperties": false,
1213
"properties": {
1314
"jsonrpc": {
1415
"type": "string",

0 commit comments

Comments
 (0)