|
| 1 | +import { createRegistry, createRequest, applyParams } from "@substreams/core"; |
| 2 | +import { readPackage } from "@substreams/manifest"; |
| 3 | +import { BlockEmitter } from "@substreams/node"; |
| 4 | +import { createNodeTransport } from "@substreams/node/createNodeTransport"; |
| 5 | + |
| 6 | +// auth API token |
| 7 | +// https://app.streamingfast.io/ |
| 8 | +// https://app.pinax.network/ |
| 9 | +if (!process.env.SUBSTREAMS_API_KEY) { |
| 10 | + throw new Error("SUBSTREAMS_API_KEY is require"); |
| 11 | +} |
| 12 | + |
| 13 | +const token = process.env.SUBSTREAMS_API_KEY; |
| 14 | +const baseUrl = "https://eos.substreams.pinax.network:443"; |
| 15 | + |
| 16 | +// User parameters |
| 17 | +const manifest = "https://spkg.io/pinax-network/antelope-common-v0.4.0.spkg"; |
| 18 | +const outputModule = "filtered_transactions"; |
| 19 | +const startBlockNum = 390603353; |
| 20 | +const stopBlockNum = startBlockNum + 86400*2*4; |
| 21 | +const productionMode = true; |
| 22 | +const params = [`filtered_transactions=code:delphioracle`] |
| 23 | + |
| 24 | +// Read Substream |
| 25 | +const substreamPackage = await readPackage(manifest); |
| 26 | +if (!substreamPackage.modules) { |
| 27 | + throw new Error("No modules found in substream package"); |
| 28 | +} |
| 29 | +applyParams(params, substreamPackage.modules.modules); |
| 30 | + |
| 31 | +for ( const modules of substreamPackage.modules.modules ) { |
| 32 | + for ( const inputs of modules.inputs) { |
| 33 | + console.log(modules.name, inputs.input.value.value) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Connect Transport |
| 38 | +const registry = createRegistry(substreamPackage); |
| 39 | +const transport = createNodeTransport(baseUrl, token, registry); |
| 40 | +const request = createRequest({ |
| 41 | + substreamPackage, |
| 42 | + outputModule, |
| 43 | + startBlockNum, |
| 44 | + stopBlockNum, |
| 45 | + productionMode, |
| 46 | +}); |
| 47 | + |
| 48 | +// NodeJS Events |
| 49 | +const emitter = new BlockEmitter(transport, request, registry); |
| 50 | + |
| 51 | +// Session Trace ID |
| 52 | +emitter.on("session", (session) => { |
| 53 | + console.dir(session); |
| 54 | +}); |
| 55 | + |
| 56 | +emitter.on("progress", (progress) => { |
| 57 | + const runningJobs = progress.runningJobs.length; |
| 58 | + const { processedBytes } = progress |
| 59 | + const moduleStats = {}; |
| 60 | + for ( const moduleStat of progress?.modulesStats ?? [] ) { |
| 61 | + moduleStats[moduleStat.name] = moduleStat.totalProcessedBlockCount; |
| 62 | + } |
| 63 | + console.dir({ runningJobs, ...processedBytes, moduleStats }); |
| 64 | +}); |
| 65 | + |
| 66 | +emitter.on("clock", clock => { |
| 67 | + // console.log(clock.number); |
| 68 | +}); |
| 69 | + |
| 70 | +// Stream Blocks |
| 71 | +emitter.on("anyMessage", (message, cursor, clock) => { |
| 72 | + console.dir(message); |
| 73 | + // console.dir(cursor); |
| 74 | + // console.dir(clock); |
| 75 | +}); |
| 76 | + |
| 77 | +// End of Stream |
| 78 | +emitter.on("close", (error) => { |
| 79 | + if (error) { |
| 80 | + console.error(error); |
| 81 | + } |
| 82 | + console.timeEnd("🆗 close"); |
| 83 | +}); |
| 84 | + |
| 85 | +// Fatal Error |
| 86 | +emitter.on("fatalError", (error) => { |
| 87 | + console.error(error); |
| 88 | +}); |
| 89 | + |
| 90 | +console.log("✅ start"); |
| 91 | +console.time("🆗 close"); |
| 92 | +emitter.start(); |
0 commit comments