-
Notifications
You must be signed in to change notification settings - Fork 28
/
stream-transfers.ts
40 lines (31 loc) · 1.04 KB
/
stream-transfers.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
import { DFUSE_API_KEY, runMain } from "../../config"
import { createDfuseClient, waitFor } from "@dfuse/client"
async function main(): Promise<void> {
const client = createDfuseClient({
apiKey: DFUSE_API_KEY,
network: "mainnet.eth.dfuse.io",
})
const streamTransfer = `subscription($cursor: String) {
searchTransactions(indexName: CALLS, query: "method:'transfer(address,uint256)'", cursor: $cursor) {
undo cursor
node { hash from to value(encoding: ETHER) }
}
}`
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 { cursor, node } = message.data.searchTransactions
console.log(`Transfer [${node.from} -> ${node.to}, ${node.value}]`)
stream.mark({ cursor })
}
if (message.type === "complete") {
console.log("Stream completed")
}
})
await waitFor(5000)
await stream.close()
client.release()
}
runMain(main)