-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
TxMonitorClient.ts
164 lines (146 loc) · 5.92 KB
/
TxMonitorClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { InteractionContext } from '../Connection'
import { ensureSocketIsOpen, eventEmitterToGenerator, safeJSON } from '../util'
import { Ogmios, MempoolSizeAndCapacity, Slot, TxAlonzo, TxId } from "@cardano-ogmios/schema"
import { baseRequest, send } from "../Request"
import { handleAwaitAcquireResponse } from "./awaitAcquire"
import { handleHasTxResponse } from "./hasTx"
import { handleNextTxResponse } from "./nextTx"
import { handleReleaseResponse } from "./release"
import { handleSizeAndCapacityResponse } from "./sizeAndCapacity"
/**
* See also {@link createTxMonitorClient} for creating a client.
*
* @category TxMonitor
**/
export interface TxMonitorClient {
context: InteractionContext
awaitAcquire: (args?: {}) => Promise<Slot>
hasTx: (id: TxId) => Promise<boolean>
nextTx: (args?: { fields?: "all" }) => Promise<TxId | TxAlonzo | null>
sizeAndCapacity: (args?: {}) => Promise<MempoolSizeAndCapacity>
release: (args?: {}) => Promise<void>
shutdown: () => Promise<void>
}
/** @Internal */
const matchAwaitAcquire = (data: string) => {
const response = safeJSON.parse(data) as Ogmios['AwaitAcquireResponse']
if ((response.type as string) !== 'jsonwsp/fault' && response.methodname !== 'AwaitAcquire') {
return null
}
return response
}
/** @Internal */
const matchHasTx = (data: string) => {
const response = safeJSON.parse(data) as Ogmios['HasTxResponse']
if ((response.type as string) !== 'jsonwsp/fault' && response.methodname !== 'HasTx') {
return null
}
return response
}
/** @Internal */
const matchNextTx = (data: string) => {
const response = safeJSON.parse(data) as Ogmios['NextTxResponse']
if ((response.type as string) !== 'jsonwsp/fault' && response.methodname !== 'NextTx') {
return null
}
return response
}
/** @Internal */
const matchSizeAndCapacity = (data: string) => {
const response = safeJSON.parse(data) as Ogmios['SizeAndCapacityResponse']
if ((response.type as string) !== 'jsonwsp/fault' && response.methodname !== 'SizeAndCapacity') {
return null
}
return response
}
/** @Internal */
const matchReleaseMempool = (data: string) => {
const response = safeJSON.parse(data) as Ogmios['ReleaseMempoolResponse']
if ((response.type as string) !== 'jsonwsp/fault' && response.methodname !== 'ReleaseMempool') {
return null
}
return response
}
/**
* Create a client for inspect the node’s local mempool.
*
* @category Constructor
**/
export const createTxMonitorClient = async (
context: InteractionContext
): Promise<TxMonitorClient> => {
const { socket } = context
const awaitAcquireResponse = eventEmitterToGenerator(socket, 'message', matchAwaitAcquire)() as
AsyncGenerator<Ogmios['AwaitAcquireResponse']>
const hasTxResponse = eventEmitterToGenerator(socket, 'message', matchHasTx)() as
AsyncGenerator<Ogmios['HasTxResponse']>
const nextTxResponse = eventEmitterToGenerator(socket, 'message', matchNextTx)() as
AsyncGenerator<Ogmios['NextTxResponse']>
const sizeAndCapacityResponse = eventEmitterToGenerator(socket, 'message', matchSizeAndCapacity)() as
AsyncGenerator<Ogmios['SizeAndCapacityResponse']>
const releaseMempoolResponse = eventEmitterToGenerator(socket, 'message', matchReleaseMempool)() as
AsyncGenerator<Ogmios['ReleaseMempoolResponse']>
return Promise.resolve({
context,
awaitAcquire: (args?: {}) => {
ensureSocketIsOpen(socket)
return send<Slot>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'AwaitAcquire',
args: args
} as unknown as Ogmios['AwaitAcquire']))
return handleAwaitAcquireResponse((await awaitAcquireResponse.next()).value)
}, context)
},
hasTx: (id: TxId) => {
ensureSocketIsOpen(socket)
return send<boolean>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'HasTx',
args: { id }
} as unknown as Ogmios['HasTx']))
return handleHasTxResponse((await hasTxResponse.next()).value)
}, context)
},
nextTx: (args?: {fields?: "all"}) => {
ensureSocketIsOpen(socket)
return send<TxId | TxAlonzo | null>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'NextTx',
args: args
} as unknown as Ogmios['NextTx']))
return handleNextTxResponse((await nextTxResponse.next()).value, args)
}, context)
},
sizeAndCapacity: (args?: {}) => {
ensureSocketIsOpen(socket)
return send<MempoolSizeAndCapacity>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'SizeAndCapacity',
args: args
} as unknown as Ogmios['SizeAndCapacity']))
return handleSizeAndCapacityResponse((await sizeAndCapacityResponse.next()).value)
}, context)
},
release: (args?: {}) => {
ensureSocketIsOpen(socket)
return send<void>(async (socket) => {
socket.send(safeJSON.stringify({
...baseRequest,
methodname: 'ReleaseMempool',
args: args
} as unknown as Ogmios['ReleaseMempool']))
return handleReleaseResponse((await releaseMempoolResponse.next()).value)
}, context)
},
shutdown: () => new Promise(resolve => {
ensureSocketIsOpen(socket)
socket.once('close', resolve)
socket.close()
})
} as TxMonitorClient)
}