-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c87ad7
commit e665373
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { createRegistry, createRequest } from "@substreams/core"; | ||
import { readPackage } from "@substreams/manifest"; | ||
import { BlockEmitter } from "@substreams/node"; | ||
import { createNodeTransport } from "@substreams/node/createNodeTransport"; | ||
|
||
// auth API token | ||
// https://app.streamingfast.io/ | ||
// https://app.pinax.network/ | ||
if (!process.env.SUBSTREAMS_API_KEY) { | ||
throw new Error("SUBSTREAMS_API_KEY is require"); | ||
} | ||
|
||
const token = process.env.SUBSTREAMS_API_KEY; | ||
const baseUrl = "https://geotest.substreams.pinax.network:443"; | ||
|
||
// User parameters | ||
const manifest = "https://github.com/pinax-network/substreams/releases/download/blocks-v0.1.0/blocks-v0.1.0.spkg"; | ||
const outputModule = "map_blocks"; | ||
const startBlockNum = 2; | ||
const stopBlockNum = "+3000"; | ||
const productionMode = true; | ||
|
||
// Read Substream | ||
const substreamPackage = await readPackage(manifest); | ||
if (!substreamPackage.modules) { | ||
throw new Error("No modules found in substream package"); | ||
} | ||
|
||
// Connect Transport | ||
const registry = createRegistry(substreamPackage); | ||
const transport = createNodeTransport(baseUrl, token, registry); | ||
const request = createRequest({ | ||
substreamPackage, | ||
outputModule, | ||
startBlockNum, | ||
stopBlockNum, | ||
productionMode, | ||
}); | ||
|
||
// NodeJS Events | ||
const emitter = new BlockEmitter(transport, request, registry); | ||
|
||
// Session Trace ID | ||
emitter.on("session", (session) => { | ||
console.dir(session); | ||
}); | ||
|
||
// Stream Blocks | ||
emitter.on("anyMessage", (message, cursor, clock) => { | ||
console.dir(message); | ||
console.dir(cursor); | ||
console.dir(clock); | ||
}); | ||
|
||
// End of Stream | ||
emitter.on("close", (error) => { | ||
if (error) { | ||
console.error(error); | ||
} | ||
console.timeEnd("🆗 close"); | ||
}); | ||
|
||
// Fatal Error | ||
emitter.on("fatalError", (error) => { | ||
console.error(error); | ||
}); | ||
|
||
console.log("✅ start"); | ||
console.time("🆗 close"); | ||
emitter.start(); |