This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
/
multi-listen.ts
105 lines (89 loc) · 2.79 KB
/
multi-listen.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
import { socketFactory, runMain, waitFor } from "./config"
import {
EoswsClient,
InboundMessageType,
InboundMessage,
createEoswsSocket,
TableDeltaData,
ActionTraceData,
ListeningData,
ErrorData
} from "@dfuse/eosws-js"
interface Transfer {
from: string
to: string
quantity: string
memo: string
}
async function main() {
const client = new EoswsClient(createEoswsSocket(socketFactory))
await client.connect()
const tableRowsStream = client.getTableRows({
code: "eosio",
scope: "eosio",
table: "global",
json: true
})
const actionTracesStream = client.getActionTraces({
account: "eosio.token",
action_name: "transfer"
})
tableRowsStream.onMessage((message: InboundMessage<any>) => {
switch (message.type) {
case InboundMessageType.TABLE_DELTA:
const tableDelta = message.data as TableDeltaData
console.log(
`Table eosio/eosio#global delta operation ${tableDelta.dbop.op} at block #${
tableDelta.block_num
}`
)
break
case InboundMessageType.LISTENING:
const listeningResp = message as InboundMessage<ListeningData>
console.log(
`Received Listening message event, reqID: ${listeningResp.req_id}, next_block: ${
listeningResp.data.next_block
}`
)
break
case InboundMessageType.ERROR:
const error = message.data as ErrorData
console.log(`Received error: ${error.message} (${error.code})`, error.details)
break
default:
console.log(`Unhandled message of type [${message.type}].`)
}
})
actionTracesStream.onMessage((message: InboundMessage<any>) => {
switch (message.type) {
case InboundMessageType.ACTION_TRACE:
const transfer = (message.data as ActionTraceData<Transfer>).trace.act.data
console.log(
`Transfer [${transfer.from} -> ${transfer.to}, ${transfer.quantity}] (${transfer.memo})`
)
break
case InboundMessageType.LISTENING:
const listeningResp = message as InboundMessage<ListeningData>
console.log(
`Received Listening message event, reqID: ${listeningResp.req_id}, next_block: ${
listeningResp.data.next_block
}`
)
break
case InboundMessageType.ERROR:
const error = message.data as ErrorData
console.log(`Received error: ${error.message} (${error.code})`, error.details)
break
default:
console.log(`Unhandled message of type [${message.type}].`)
}
})
await waitFor(1000)
console.log("Unlistening from table row updates...")
tableRowsStream.unlisten()
await waitFor(2000)
console.log("Unlistening from action trace updates...")
actionTracesStream.unlisten()
client.disconnect()
}
runMain(main)