From 2481bda6e434926ddfdaf3cd80fbf8c88f1f6536 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 27 Jun 2023 15:56:57 +0200 Subject: [PATCH] Subscribe to logs with polling --- ethers/providers/jsonrpc/signatures.nim | 1 + ethers/providers/jsonrpc/subscriptions.nim | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/ethers/providers/jsonrpc/signatures.nim b/ethers/providers/jsonrpc/signatures.nim index 99cfc98..f30cc3d 100644 --- a/ethers/providers/jsonrpc/signatures.nim +++ b/ethers/providers/jsonrpc/signatures.nim @@ -16,5 +16,6 @@ proc eth_subscribe(name: string, filter: Filter): JsonNode proc eth_subscribe(name: string): JsonNode proc eth_unsubscribe(id: JsonNode): bool proc eth_newBlockFilter(): JsonNode +proc eth_newFilter(filter: Filter): JsonNode proc eth_getFilterChanges(id: JsonNode): JsonNode proc eth_uninstallFilter(id: JsonNode): bool diff --git a/ethers/providers/jsonrpc/subscriptions.nim b/ethers/providers/jsonrpc/subscriptions.nim index b381219..0f33ea1 100644 --- a/ethers/providers/jsonrpc/subscriptions.nim +++ b/ethers/providers/jsonrpc/subscriptions.nim @@ -146,6 +146,20 @@ method subscribeBlocks(subscriptions: PollingSubscriptions, subscriptions.callbacks[id] = callback return JsonRpcSubscription(subscriptions: subscriptions, id: id) +method subscribeLogs(subscriptions: PollingSubscriptions, + filter: Filter, + onLog: LogHandler): + Future[JsonRpcSubscription] + {.async.} = + + proc callback(id, change: JsonNode) = + if log =? Log.fromJson(change).catch: + onLog(log) + + let id = await subscriptions.client.eth_newFilter(filter) + subscriptions.callbacks[id] = callback + return JsonRpcSubscription(subscriptions: subscriptions, id: id) + method unsubscribe(subscriptions: PollingSubscriptions, id: JsonNode) {.async.} =