Skip to content

Commit 8ee1da8

Browse files
authored
fix: incorrect websocket/socket.io transaction updates (#1197)
* feat: draft listener class * refactor: interface draft * feat: socket.io channel * docs: comments * feat: ws-rpc channel * refactor: rename metrics.ts * fix: socket.io tests * fix: websocket tests * fix: unused exports * fix: update on stale mempool tx drop * feat: return complete objects in ws-rpc * test: complete ws-rpc responses * docs: update address tx example * fix: also take client and docs pacakge.json * test: try to fix socket flakyness * test: expect tx status explicitly * fix: notify gc mempool txs outside sql tx
1 parent 7329413 commit 8ee1da8

26 files changed

+8215
-7029
lines changed

client/README.md

Lines changed: 66 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -287,61 +287,77 @@ Sent every time a transaction is sent or received by a specific Stacks address.
287287
Example message if subscribed to updates for an address `SP3C5SSYVKPAWTR8Y63CVYBR65GD3MG7K80526D1Q`:
288288
```json
289289
{
290-
"tx_id": "0x1f9e737dfbebcb57f0879a44518c1cc909be0ceb8ab0bc9b38ce63e3b6847917",
291-
"nonce": 6,
292-
"fee_rate": "277600",
293-
"sender_address": "SP3C5SSYVKPAWTR8Y63CVYBR65GD3MG7K80526D1Q",
294-
"sponsored": false,
295-
"post_condition_mode": "deny",
296-
"post_conditions": [
297-
{
298-
"type": "stx",
299-
"condition_code": "sent_less_than_or_equal_to",
300-
"amount": "25000000",
301-
"principal": {
302-
"type_id": "principal_standard",
303-
"address": "SP3C5SSYVKPAWTR8Y63CVYBR65GD3MG7K80526D1Q"
304-
}
305-
},
306-
{
307-
"type": "non_fungible",
308-
"condition_code": "not_sent",
309-
"principal": {
310-
"type_id": "principal_contract",
311-
"contract_name": "stacks-skaters",
312-
"address": "SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG"
313-
},
314-
"asset": {
315-
"contract_name": "stacks-skaters",
316-
"asset_name": "stacks-skaters",
317-
"contract_address": "SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG"
318-
},
319-
"asset_value": {
320-
"hex": "0x0100000000000000000000000000000000",
321-
"repr": "u0"
290+
"tx": {
291+
"tx_id": "0x0c818b9af6356a2eb4d64ee1b2490193d97a82392c02e7264e006ae5979aa726",
292+
"nonce": 32,
293+
"fee_rate": "3000",
294+
"sender_address": "SP3BK1NNSWN719Z6KDW05RBGVS940YCN6X84STYPR",
295+
"sponsored": false,
296+
"post_condition_mode": "deny",
297+
"post_conditions": [
298+
{
299+
"type": "stx",
300+
"condition_code": "sent_equal_to",
301+
"amount": "4375722",
302+
"principal": {
303+
"type_id": "principal_contract",
304+
"contract_name": "newyorkcitycoin-core-v1",
305+
"address": "SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5"
306+
}
322307
}
308+
],
309+
"anchor_mode": "any",
310+
"is_unanchored": false,
311+
"block_hash": "0xe2a811451fed35331cf462a9107e3453fdebba1682dfad83cbbcdc603f644ed3",
312+
"parent_block_hash": "0x6d8653da23188d4d78ab9b6448229be68abe1bca001f8c574c094289107bce15",
313+
"block_height": 58775,
314+
"burn_block_time": 1651720813,
315+
"burn_block_time_iso": "2022-05-05T03:20:13.000Z",
316+
"parent_burn_block_time": 1651720368,
317+
"parent_burn_block_time_iso": "2022-05-05T03:12:48.000Z",
318+
"canonical": true,
319+
"tx_index": 36,
320+
"tx_status": "success",
321+
"tx_result": {
322+
"hex": "0x0703",
323+
"repr": "(ok true)"
323324
},
325+
"microblock_hash": "",
326+
"microblock_sequence": 2147483647,
327+
"microblock_canonical": true,
328+
"event_count": 1,
329+
"events": [],
330+
"execution_cost_read_count": 15,
331+
"execution_cost_read_length": 31147,
332+
"execution_cost_runtime": 81975,
333+
"execution_cost_write_count": 2,
334+
"execution_cost_write_length": 123,
335+
"tx_type": "contract_call",
336+
"contract_call": {
337+
"contract_id": "SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5.newyorkcitycoin-core-v1",
338+
"function_name": "claim-stacking-reward",
339+
"function_signature": "(define-public (claim-stacking-reward (targetCycle uint)))",
340+
"function_args": [
341+
{
342+
"hex": "0x0100000000000000000000000000000008",
343+
"repr": "u8",
344+
"name": "targetCycle",
345+
"type": "uint"
346+
}
347+
]
348+
}
349+
},
350+
"stx_sent": "3000",
351+
"stx_received": "4375722",
352+
"stx_transfers": [
324353
{
325-
"type": "stx",
326-
"condition_code": "sent_less_than_or_equal_to",
327-
"amount": "20000000",
328-
"principal": {
329-
"type_id": "principal_contract",
330-
"contract_name": "stacks-skaters",
331-
"address": "SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG"
332-
}
354+
"amount": "4375722",
355+
"sender": "SP2H8PY27SEZ03MWRKS5XABZYQN17ETGQS3527SA5.newyorkcitycoin-core-v1",
356+
"recipient": "SP3BK1NNSWN719Z6KDW05RBGVS940YCN6X84STYPR"
333357
}
334358
],
335-
"anchor_mode": "any",
336-
"tx_status": "pending",
337-
"receipt_time": 1637172946,
338-
"receipt_time_iso": "2021-11-17T18:15:46.000Z",
339-
"tx_type": "contract_call",
340-
"contract_call": {
341-
"contract_id": "SPJW1XE278YMCEYMXB8ZFGJMH8ZVAAEDP2S2PJYG.stacks-skaters",
342-
"function_name": "mint",
343-
"function_signature": ""
344-
}
359+
"ft_transfers": [],
360+
"nft_transfers": []
345361
}
346362
```
347363
Subscribe via WebSockets:

client/src/ws/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import * as JsonRpcLite from 'jsonrpc-lite';
22
import { EventEmitter } from 'eventemitter3';
33
import {
44
RpcTxUpdateSubscriptionParams,
5-
RpcTxUpdateNotificationParams,
65
RpcAddressTxSubscriptionParams,
7-
RpcAddressTxNotificationParams,
86
RpcAddressBalanceSubscriptionParams,
9-
RpcAddressBalanceNotificationParams,
107
RpcSubscriptionType,
118
Block,
129
RpcBlockSubscriptionParams,
1310
Microblock,
1411
Transaction,
1512
RpcMicroblockSubscriptionParams,
1613
RpcMempoolSubscriptionParams,
14+
MempoolTransaction,
15+
RpcAddressBalanceNotificationParams,
16+
RpcAddressTxNotificationParams,
1717
} from '@stacks/stacks-blockchain-api-types';
1818
import { BASE_PATH } from '../generated/runtime';
1919

@@ -35,7 +35,7 @@ export class StacksApiWebSocketClient {
3535
block: (event: Block) => void;
3636
microblock: (event: Microblock) => void;
3737
mempool: (event: Transaction) => void;
38-
txUpdate: (event: RpcTxUpdateNotificationParams) => any;
38+
txUpdate: (event: Transaction | MempoolTransaction) => any;
3939
addressTxUpdate: (event: RpcAddressTxNotificationParams) => void;
4040
addressBalanceUpdate: (event: RpcAddressBalanceNotificationParams) => void;
4141
}>();
@@ -93,10 +93,11 @@ export class StacksApiWebSocketClient {
9393
const method = data.method as RpcSubscriptionType;
9494
switch (method) {
9595
case 'tx_update':
96-
this.eventEmitter.emit('txUpdate', data.params as RpcTxUpdateNotificationParams);
96+
this.eventEmitter.emit('txUpdate', data.params as (Transaction | MempoolTransaction));
9797
break;
9898
case 'address_tx_update':
99-
this.eventEmitter.emit('addressTxUpdate', data.params as RpcAddressTxNotificationParams);
99+
this.eventEmitter.emit('addressTxUpdate',
100+
data.params as RpcAddressTxNotificationParams);
100101
break;
101102
case 'address_balance_update':
102103
this.eventEmitter.emit(
@@ -171,11 +172,11 @@ export class StacksApiWebSocketClient {
171172

172173
async subscribeTxUpdates(
173174
txId: string,
174-
update: (event: RpcTxUpdateNotificationParams) => any
175+
update: (event: Transaction | MempoolTransaction) => any
175176
): Promise<Subscription> {
176177
const params: RpcTxUpdateSubscriptionParams = { event: 'tx_update', tx_id: txId };
177178
const subscribed = await this.rpcCall<{ tx_id: string }>('subscribe', params);
178-
const listener = (event: RpcTxUpdateNotificationParams) => {
179+
const listener = (event: Transaction | MempoolTransaction) => {
179180
if (event.tx_id === subscribed.tx_id) {
180181
update(event);
181182
}

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,22 @@
1818
"enum": ["address_balance_update"]
1919
},
2020
"params": {
21-
"$ref": "./rpc-address-balance-notification-params.schema.json"
21+
"title": "RpcAddressBalanceNotificationParams",
22+
"allOf": [
23+
{
24+
"type": "object",
25+
"additionalProperties": false,
26+
"required": ["address"],
27+
"properties": {
28+
"address": {
29+
"type": "string"
30+
}
31+
}
32+
},
33+
{
34+
"$ref": "../../api/address/get-address-stx-balance.schema.json"
35+
}
36+
]
2237
}
2338
}
2439
}

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,31 @@
1818
"enum": ["address_tx_update"]
1919
},
2020
"params": {
21-
"$ref": "./rpc-address-tx-notification-params.schema.json"
21+
"title": "RpcAddressTxNotificationParams",
22+
"allOf": [
23+
{
24+
"type": "object",
25+
"additionalProperties": false,
26+
"required": ["address", "tx_id", "tx_type", "tx_status"],
27+
"properties": {
28+
"address": {
29+
"type": "string"
30+
},
31+
"tx_id": {
32+
"type": "string"
33+
},
34+
"tx_type": {
35+
"$ref": "../transactions/transaction-type.schema.json"
36+
},
37+
"tx_status": {
38+
"$ref": "../transactions/transaction-status.schema.json"
39+
}
40+
}
41+
},
42+
{
43+
"$ref": "../../entities/address/transaction-with-transfers.schema.json"
44+
}
45+
]
2246
}
2347
}
2448
}

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

Lines changed: 0 additions & 29 deletions
This file was deleted.

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@
1818
"enum": ["tx_update"]
1919
},
2020
"params": {
21-
"$ref": "./rpc-tx-update-notification-params.schema.json"
21+
"anyOf": [
22+
{
23+
"$ref": "../transactions/transaction.schema.json"
24+
},
25+
{
26+
"$ref": "../mempool-transactions/transaction.schema.json"
27+
}
28+
]
2229
}
2330
}
2431
}

0 commit comments

Comments
 (0)