Skip to content

Commit

Permalink
Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Jan 11, 2024
1 parent 97a7c35 commit 0c4cccd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
55 changes: 55 additions & 0 deletions examples/starknet-client/index.ts
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);
});
15 changes: 15 additions & 0 deletions examples/starknet-client/package.json
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"
}
}

0 comments on commit 0c4cccd

Please sign in to comment.