Skip to content

Commit c3abd42

Browse files
committed
multi-address logs subscription
1 parent f3cf3f5 commit c3abd42

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
> ⚠️ This service is provided for testing purposes only. It is not intended for production use.
44
5+
## Install Dependencies
6+
7+
Requires [Node JS](https://nodejs.org/en/download/package-manager).
8+
9+
```bash
10+
npm ci
11+
```
12+
513
## Run
614

715
```bash

server.ts

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import { createLogger, format, transports, Logger } from "winston";
2828
// subscribe Wormhole Core logs on Sepolia
2929
// {"jsonrpc":"2.0","id": 1, "method": "eth_subscribe", "params": ["logs", {"address": "0x4a8bc80Ed5a4067f1CCf107057b8270E0cC11A78", "topics": ["0x6eb224fb001ed210e379b335e35efe88672a8ce935d981a6896b27ffdf52a3b2"]}]}
3030

31+
// Guardian subscription looks like
32+
// {"jsonrpc":"2.0","id":1,"method":"eth_subscribe","params":["logs",{"address":["0x4a8bc80Ed5a4067f1CCf107057b8270E0cC11A78"],"fromBlock":"0x0","toBlock":"latest","topics":[["0x6eb224fb001ed210e379b335e35efe88672a8ce935d981a6896b27ffdf52a3b2"],null]}]}
33+
3134
// unsubscribe
3235
// {"id":1,"jsonrpc":"2.0","method":"eth_unsubscribe","params":["0x00000000000000000000000000000001"]}
3336

@@ -98,22 +101,42 @@ wss.on("connection", (ws: WebSocket) => {
98101
newHeadSubscribers.push({ ws, id });
99102
// {"jsonrpc":"2.0","result":"0x07859641bba6dd7d21cdb79d704f679b","id":1}
100103
ws.send(`{"jsonrpc":"2.0","result":"${id}","id":${json.id}}`);
101-
} else if (
102-
json.params.length === 2 &&
103-
json.params[0] === "logs" &&
104-
typeof json.params[1]?.address === "string" && // for now, enforce 1 address
105-
json.params[1]?.topics?.length === 1 && // for now, enforce 1 topic
106-
typeof json.params[1]?.topics[0] === "string"
107-
) {
108-
// {"jsonrpc":"2.0","id": 1, "method": "eth_subscribe", "params": ["logs", {"address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]}]}
109-
let id = getNextSub();
110-
logSubscribers.push({
111-
ws,
112-
id,
113-
address: json.params[1].address,
114-
topics: json.params[1].topics,
115-
});
116-
ws.send(`{"jsonrpc":"2.0","result":"${id}","id":${json.id}}`);
104+
} else if (json.params.length === 2 && json.params[0] === "logs") {
105+
if (
106+
typeof json.params[1]?.address === "string" && // single address
107+
json.params[1]?.topics?.length === 1 && // one topic
108+
typeof json.params[1]?.topics[0] === "string"
109+
) {
110+
// single array of topics
111+
// {"jsonrpc":"2.0","id": 1, "method": "eth_subscribe", "params": ["logs", {"address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]}]}
112+
let id = getNextSub();
113+
logSubscribers.push({
114+
ws,
115+
id,
116+
address: json.params[1].address,
117+
topics: json.params[1].topics,
118+
});
119+
ws.send(`{"jsonrpc":"2.0","result":"${id}","id":${json.id}}`);
120+
} else if (
121+
json.params[1]?.address?.length && // array of addresses
122+
json.params[1]?.topics?.length >= json.params[1]?.address?.length // array of arrays of topics
123+
) {
124+
// {"jsonrpc":"2.0","id":1,"method":"eth_subscribe","params":["logs",{"address":["0x4a8bc80Ed5a4067f1CCf107057b8270E0cC11A78"],"fromBlock":"0x0","toBlock":"latest","topics":[["0x6eb224fb001ed210e379b335e35efe88672a8ce935d981a6896b27ffdf52a3b2"],null]}]}
125+
let id = getNextSub();
126+
for (let idx = 0; idx < json.params[1].address.length; idx++) {
127+
let address = json.params[1].address[idx];
128+
let topics = json.params[1].topics[idx];
129+
if (address && topics && topics.length) {
130+
logSubscribers.push({
131+
ws,
132+
id,
133+
address,
134+
topics,
135+
});
136+
}
137+
}
138+
ws.send(`{"jsonrpc":"2.0","result":"${id}","id":${json.id}}`);
139+
}
117140
}
118141
} else if (json.method === "eth_unsubscribe") {
119142
if (json.params.length === 1) {

0 commit comments

Comments
 (0)