-
Notifications
You must be signed in to change notification settings - Fork 6
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
Showing
2 changed files
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,55 @@ | ||
import { StreamClient } from "@apibara/protocol"; | ||
import { Filter, StarkNetCursor, v1alpha2 } from "@apibara/starknet"; | ||
|
||
async function main() { | ||
const client = new StreamClient({ | ||
url: "sepolia.starknet.a5a.ch", | ||
token: process.env.DNA_TOKEN, | ||
async onReconnect(err, retryCount) { | ||
console.log("reconnect", err, retryCount); | ||
// Sleep for 1 second before retrying. | ||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
|
||
return { reconnect: true }; | ||
} | ||
}) | ||
|
||
const filter = Filter.create().withHeader({ weak: false }).encode(); | ||
|
||
client.configure({ | ||
filter, | ||
batchSize: 1, | ||
cursor: StarkNetCursor.createWithBlockNumber(18_000), | ||
}) | ||
|
||
for await (const message of client) { | ||
switch (message.message) { | ||
case "data": { | ||
if (!message.data?.data) { | ||
continue; | ||
} | ||
for (const data of message.data.data) { | ||
const block = v1alpha2.Block.decode(data); | ||
const { header } = block; | ||
if (!header) { | ||
continue; | ||
} | ||
console.log("Block " + header.blockNumber?.toString()); | ||
} | ||
break; | ||
} | ||
case "invalidate": { | ||
break; | ||
} | ||
case "heartbeat": { | ||
console.log("Received heartbeat"); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
main().then(() => process.exit(0)).catch(error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
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,15 @@ | ||
{ | ||
"name": "starknet-client-example", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "ts-node index.ts" | ||
}, | ||
"dependencies": { | ||
"@apibara/protocol": "workspace:*", | ||
"@apibara/starknet": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"ts-node": "^10.9.1" | ||
} | ||
} |