-
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.
add ethereum erc-20 balance chnages example
- Loading branch information
1 parent
a76c650
commit 7beec65
Showing
1 changed file
with
69 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,69 @@ | ||
import { createModuleHashHex, createRegistry, createRequest } from "@substreams/core"; | ||
import { readPackage } from "@substreams/manifest"; | ||
import { BlockEmitter, createNodeTransport } from "@substreams/node"; | ||
|
||
// auth API token | ||
// https://app.streamingfast.io/ | ||
// https://app.pinax.network/ | ||
if (!process.env.SUBSTREAMS_API_TOKEN) { | ||
throw new Error("SUBSTREAMS_API_TOKEN is require"); | ||
} | ||
|
||
const token = process.env.SUBSTREAMS_API_TOKEN; | ||
const baseUrl = "https://eth.substreams.pinax.network:443"; | ||
|
||
// User parameters | ||
const manifest = "https://github.com/pinax-network/substreams-erc20-balance-changes/releases/download/v1.2.0/erc20-balance-changes-mainnet-v1.2.0.spkg"; | ||
const outputModule = "map_balance_changes"; | ||
const startBlockNum = -1000; | ||
|
||
// Read Substream | ||
const substreamPackage = await readPackage(manifest); | ||
if (!substreamPackage.modules) { | ||
throw new Error("No modules found in substream package"); | ||
} | ||
const moduleHash = await createModuleHashHex(substreamPackage.modules, outputModule); | ||
console.log({ moduleHash }); | ||
|
||
// Connect Transport | ||
const headers = new Headers({ "User-Agent": "@substreams/node" }); | ||
const registry = createRegistry(substreamPackage); | ||
const transport = createNodeTransport(baseUrl, token, registry, headers); | ||
const request = createRequest({ | ||
substreamPackage, | ||
outputModule, | ||
startBlockNum, | ||
}); | ||
|
||
// 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) => { | ||
for ( const balanceChange of message.balanceChanges ?? []) { | ||
console.dir(balanceChange); | ||
} | ||
console.dir({cursor, ...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(); |