-
Notifications
You must be signed in to change notification settings - Fork 28
/
stream-pending-transactions.ts
69 lines (58 loc) · 1.96 KB
/
stream-pending-transactions.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
import { DFUSE_API_KEY, runMain, DFUSE_API_NETWORK } from "../../config"
import { createDfuseClient, waitFor } from "@dfuse/client"
import WebSocketClient from "ws"
async function main(): Promise<void> {
const client = createDfuseClient({
apiKey: DFUSE_API_KEY,
network: DFUSE_API_NETWORK,
graphqlStreamClientOptions: {
socketOptions: {
webSocketFactory: (url) => webSocketFactory(url, ["graphql-ws"]),
},
},
streamClientOptions: {
socketOptions: {
webSocketFactory: (url) => webSocketFactory(url),
},
},
})
// Could be a list coming from a file, we tested successfully with 90K addresses without any problem
const addresses = ["0xA4e5961B58DBE487639929643dCB1Dc3848dAF5E"]
const streamPendingTrxs = `subscription ($addresses: [String!]!, $fields: FILTER_FIELD!) {
_alphaPendingTransactions(filterAddresses: $addresses, filterField: $fields) {
hash from to gasLimit gasPrice(encoding: ETHER) value(encoding: ETHER)
}
}`
const stream = await client.graphql(
streamPendingTrxs,
(message) => {
if (message.type === "error") {
console.log("An error occurred", message.errors, message.terminal)
}
if (message.type === "data") {
const data = message.data._alphaPendingTransactions
const { hash, from, to, value, gasLimit } = data
console.log(`Pending [${from} -> ${to}, ${value} ETH, Gas ${gasLimit}] (${hash})`)
}
if (message.type === "complete") {
console.log("Stream completed")
}
},
{
variables: {
addresses,
fields: "FROM_OR_TO",
},
}
)
await waitFor(10000)
await stream.close()
client.release()
}
runMain(main)
async function webSocketFactory(url: string, protocols: string[] = []): Promise<WebSocketClient> {
console.log("Creating new client with updated max payload")
return new WebSocketClient(url, protocols, {
maxPayload: 45 * 1024 * 1024, // 45 Mib
})
}