Subscribe to logs with polling

This commit is contained in:
Mark Spanbroek 2023-06-27 15:56:57 +02:00 committed by markspanbroek
parent 0aea16047c
commit 2481bda6e4
2 changed files with 15 additions and 0 deletions

View File

@ -16,5 +16,6 @@ proc eth_subscribe(name: string, filter: Filter): JsonNode
proc eth_subscribe(name: string): JsonNode proc eth_subscribe(name: string): JsonNode
proc eth_unsubscribe(id: JsonNode): bool proc eth_unsubscribe(id: JsonNode): bool
proc eth_newBlockFilter(): JsonNode proc eth_newBlockFilter(): JsonNode
proc eth_newFilter(filter: Filter): JsonNode
proc eth_getFilterChanges(id: JsonNode): JsonNode proc eth_getFilterChanges(id: JsonNode): JsonNode
proc eth_uninstallFilter(id: JsonNode): bool proc eth_uninstallFilter(id: JsonNode): bool

View File

@ -146,6 +146,20 @@ method subscribeBlocks(subscriptions: PollingSubscriptions,
subscriptions.callbacks[id] = callback subscriptions.callbacks[id] = callback
return JsonRpcSubscription(subscriptions: subscriptions, id: id) 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, method unsubscribe(subscriptions: PollingSubscriptions,
id: JsonNode) id: JsonNode)
{.async.} = {.async.} =