Skip to content

Commit 3bddfbe

Browse files
committed
examples: show how to use rpc stream
1 parent 4b6d60f commit 3bddfbe

1 file changed

Lines changed: 12 additions & 46 deletions

File tree

examples/evm-client/src/main-rpc.ts

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { EvmRpcStream, type Filter, rateLimitedHttp } from "@apibara/evm-rpc";
33
import { createRpcClient } from "@apibara/protocol/rpc";
44
import { defineCommand, runMain } from "citty";
55
import consola from "consola";
6-
import { type Chain, createPublicClient } from "viem";
7-
import { mainnet, sepolia } from "viem/chains";
6+
import { createPublicClient } from "viem";
87

98
const command = defineCommand({
109
meta: {
@@ -18,19 +17,14 @@ const command = defineCommand({
1817
description: "EVM RPC endpoint URL (Alchemy, Infura, QuickNode, etc.)",
1918
required: true,
2019
},
21-
network: {
22-
type: "string",
23-
default: "mainnet",
24-
description: "Network name (mainnet, sepolia)",
25-
},
2620
contract: {
2721
type: "string",
28-
default: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
22+
default: "0xf08A50178dfcDe18524640EA6618a1f965821715",
2923
description: "Contract address to monitor",
3024
},
3125
startBlock: {
3226
type: "string",
33-
default: "23911400",
27+
default: "7902564",
3428
description: "Starting block number",
3529
},
3630
finality: {
@@ -41,28 +35,23 @@ const command = defineCommand({
4135
},
4236
async run({ args }) {
4337
consola.info("Creating EVM RPC client");
44-
consola.info("Network:", args.network);
45-
46-
const chain = (args.network === "mainnet" ? mainnet : sepolia) as Chain;
4738

4839
const viemClient = createPublicClient({
49-
chain,
5040
transport: rateLimitedHttp(args.rpcUrl, {
51-
rps: 2,
41+
rps: 1, // Requests per second, to avoid rate limiting
5242
retryCount: 3,
5343
retryDelay: 1_000,
5444
batch: {
55-
wait: 10,
45+
wait: 10, // batch multiple requests together
5646
},
57-
// async onFetchRequest(request) {
58-
// console.log("Fetching request:", request.url);
59-
// },
6047
}),
6148
});
6249

6350
const client = createRpcClient(
6451
new EvmRpcStream(viemClient, {
65-
getLogsRangeSize: 10n,
52+
// This parameter changes based on the rpc provider.
53+
// The stream automatically shrinks the batch size when the provider returns an error.
54+
getLogsRangeSize: 1_000n,
6655
}),
6756
);
6857

@@ -72,19 +61,10 @@ const command = defineCommand({
7261
consola.info("Finalized:", status.finalized?.orderKey);
7362

7463
const filter: Filter = {
75-
// header: "always",
7664
logs: [
7765
{
7866
id: 1,
79-
address: "0xe0e0e08A6A4b9Dc7bD67BCB7aadE5cF48157d444",
80-
},
81-
{
82-
id: 2,
83-
address: "0xA37cc341634AFD9E0919D334606E676dbAb63E17",
84-
},
85-
{
86-
id: 3,
87-
address: "0xe0e0e08A6A4b9Dc7bD67BCB7aadE5cF48157d444",
67+
address: args.contract as `0x${string}`,
8868
},
8969
],
9070
};
@@ -102,29 +82,15 @@ const command = defineCommand({
10282
})) {
10383
switch (message._tag) {
10484
case "data": {
105-
const { data, endCursor, finality, production } = message.data;
85+
const { data } = message.data;
10686

10787
for (const block of data) {
10888
assert(block !== null);
10989
const logs = block.logs;
11090

111-
consola.info(
112-
`block ${block.header?.blockNumber} [${finality}/${production}]`,
91+
console.log(
92+
`block = ${block.header?.blockNumber} logs=${logs.length}`,
11393
);
114-
consola.info(
115-
" logs:",
116-
logs.length,
117-
logs.map(({ logIndex }) => logIndex),
118-
);
119-
120-
for (const log of logs) {
121-
// consola.info(log);
122-
// consola.info(" 🔔 Log");
123-
// consola.info(" Tx:", log.transactionHash);
124-
// consola.info(" Address:", log.address);
125-
// consola.info(" Topics:", log.topics.join(", "));
126-
// consola.info(" Log Index:", log.logIndex);
127-
}
12894
}
12995

13096
break;

0 commit comments

Comments
 (0)