-
Notifications
You must be signed in to change notification settings - Fork 1
/
NeonEventListener.ts
215 lines (186 loc) · 6.86 KB
/
NeonEventListener.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import {
Neo3ApplicationLog,
Neo3Event,
Neo3EventListener,
Neo3EventListenerCallback,
Neo3EventWithState,
Neo3StackItem,
} from '@cityofzion/neon-dappkit-types'
import { rpc } from '@cityofzion/neon-js'
import type * as NeonTypes from '@cityofzion/neon-core'
export type NeonEventListenerOptions = {
debug?: boolean | undefined
waitForApplicationLog?:
| {
maxAttempts?: number | undefined
waitMs?: number | undefined
}
| undefined
waitForEventMs?: number | undefined
}
export class NeonEventListener implements Neo3EventListener {
static MAINNET = 'https://mainnet1.neo.coz.io:443'
static TESTNET = 'https://testnet1.neo.coz.io:443'
private blockPollingLoopActive = false
private listeners = new Map<string, Map<string, Neo3EventListenerCallback[]>>()
private readonly rpcClient: NeonTypes.rpc.RPCClient
constructor(
rpcUrl: string,
private options: NeonEventListenerOptions | undefined = undefined,
) {
this.rpcClient = new rpc.RPCClient(rpcUrl)
}
addEventListener(contract: string, eventname: string, callback: Neo3EventListenerCallback): void {
const listenersOfContract = this.listeners.get(contract)
if (!listenersOfContract) {
this.listeners.set(contract, new Map([[eventname, [callback]]]))
} else {
listenersOfContract.set(eventname, [...(listenersOfContract.get(eventname) ?? []), callback])
}
if (!this.blockPollingLoopActive) {
this.blockPollingLoopActive = true
this.blockPollingLoop()
}
}
removeEventListener(contract: string, eventname: string, callback: Neo3EventListenerCallback): void {
const listenersOfContract = this.listeners.get(contract)
if (listenersOfContract) {
let listenersOfEvent = listenersOfContract.get(eventname)
if (listenersOfEvent) {
listenersOfEvent = listenersOfEvent.filter((l) => l !== callback)
listenersOfContract.set(eventname, listenersOfEvent)
if (listenersOfEvent.length === 0) {
listenersOfContract.delete(eventname)
if (listenersOfContract.size === 0) {
this.listeners.delete(contract)
if (this.listeners.size === 0) {
this.blockPollingLoopActive = false
}
}
}
}
}
}
removeAllEventListenersOfContract(contract: string) {
this.listeners.delete(contract)
if (this.listeners.size === 0) {
this.blockPollingLoopActive = false
}
}
removeAllEventListenersOfEvent(contract: string, eventname: string) {
const listenersOfContract = this.listeners.get(contract)
if (listenersOfContract) {
listenersOfContract.delete(eventname)
if (listenersOfContract.size === 0) {
this.listeners.delete(contract)
if (this.listeners.size === 0) {
this.blockPollingLoopActive = false
}
}
}
}
async waitForApplicationLog(txId: string): Promise<Neo3ApplicationLog> {
const maxAttempts = this.options?.waitForApplicationLog?.maxAttempts ?? 30
const waitMs = this.options?.waitForApplicationLog?.waitMs ?? 1000
let attempts = 0
let error = new Error("Couldn't get application log")
do {
try {
return await this.rpcClient.getApplicationLog(txId)
} catch (e) {
error = e
}
attempts++
await this.wait(waitMs)
} while (attempts < maxAttempts)
throw error
}
confirmHalt(txResult: Neo3ApplicationLog) {
if (txResult?.executions[0]?.vmstate !== 'HALT')
throw new Error('Transaction failed. VMState: ' + txResult?.executions[0]?.vmstate)
}
confirmStackTrue(txResult: Neo3ApplicationLog) {
if (
!txResult ||
!txResult.executions ||
txResult.executions.length === 0 ||
!txResult.executions[0].stack ||
txResult.executions[0].stack.length === 0
) {
throw new Error('Transaction failed. No stack found in transaction result')
}
const stack: Neo3StackItem = txResult.executions[0].stack[0]
if (stack.value !== true) {
throw new Error('Transaction failed. Stack value is not true')
}
}
getNotificationState(txResult: Neo3ApplicationLog, eventToCheck: Neo3Event): Neo3EventWithState | undefined {
return txResult?.executions[0].notifications.find((e) => {
return e.contract === eventToCheck.contract && e.eventname === eventToCheck.eventname
})
}
confirmTransaction(
txResult: Neo3ApplicationLog,
eventToCheck?: Neo3Event | undefined,
confirmStackTrue = false,
): void {
this.confirmHalt(txResult)
if (confirmStackTrue) {
this.confirmStackTrue(txResult)
}
if (eventToCheck) {
const state = this.getNotificationState(txResult, eventToCheck)
if (!state) {
throw new Error('Transaction failed. Event not found in transaction result')
}
}
}
private async blockPollingLoop(): Promise<void> {
let height = await this.rpcClient.getBlockCount()
while (this.blockPollingLoopActive) {
await this.wait(this.options?.waitForEventMs ?? 4000)
try {
this.options?.debug && console.log('Checking block ' + height)
if (height > (await this.rpcClient.getBlockCount())) {
this.options?.debug && console.log('Block height is ahead of node. Waiting for node to catch up...')
continue
}
const block = await this.rpcClient.getBlock(height - 1, true)
for (const transaction of block.tx) {
if (!transaction.hash) {
this.options?.debug && console.log('Transaction hash not found. Skipping transaction')
continue
}
const log = await this.rpcClient.getApplicationLog(transaction.hash)
for (const notification of log.executions[0].notifications) {
const listenersOfContract = this.listeners.get(notification.contract)
if (!listenersOfContract) {
this.options?.debug && console.log('No listeners for contract ' + notification.contract)
continue
}
const listenersOfEvent = listenersOfContract.get(notification.eventname)
if (!listenersOfEvent) {
this.options?.debug && console.log('No listeners for event ' + notification.eventname)
continue
}
for (const listener of listenersOfEvent) {
try {
this.options?.debug && console.log('Calling listener')
listener(notification as Neo3EventWithState)
} catch (e) {
this.options?.debug && console.error(e)
}
}
}
}
height++ // this is important to avoid skipping blocks when the code throws exceptions
} catch (error) {
this.options?.debug && console.error(error)
}
}
this.options?.debug && console.log('Block polling loop stopped')
}
private wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
}