forked from dfuse-io/client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream-transfers-graphql.ts
47 lines (37 loc) · 1.21 KB
/
stream-transfers-graphql.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
import { DFUSE_API_KEY, runMain, DFUSE_API_NETWORK } from "../../config"
import { createDfuseClient, waitFor } from "@dfuse/client"
async function main(): Promise<void> {
const client = createDfuseClient({
apiKey: DFUSE_API_KEY,
network: DFUSE_API_NETWORK,
})
const streamTransfer = `subscription($cursor: String!) {
searchTransactionsForward(query: "receiver:eosio.token action:transfer", cursor: $cursor) {
undo cursor
trace {
matchingActions { json }
}
}
}`
const stream = await client.graphql(streamTransfer, (message) => {
if (message.type === "error") {
console.log("An error occurred", message.errors, message.terminal)
}
if (message.type === "data") {
const data = message.data.searchTransactionsForward
const actions = data.trace.matchingActions
actions.forEach(({ json }: any) => {
const { from, to, quantity, memo } = json
console.log(`Transfer [${from} -> ${to}, ${quantity}] (${memo})`)
})
stream.mark({ cursor: data.cursor })
}
if (message.type === "complete") {
console.log("Stream completed")
}
})
await waitFor(5000)
await stream.close()
client.release()
}
runMain(main)