Fix block subscription

This commit is contained in:
Jackie 2022-07-21 01:19:33 +02:00 committed by markspanbroek
parent abb23d5220
commit 52f57894f5
2 changed files with 15 additions and 2 deletions

View File

@ -164,6 +164,18 @@ proc subscribe(provider: JsonRpcProvider,
return JsonRpcSubscription(id: id, provider: provider) return JsonRpcSubscription(id: id, provider: provider)
proc subscribe(provider: JsonRpcProvider,
name: string,
handler: SubscriptionHandler): Future[Subscription] {.async.} =
convertError:
let client = await provider.client
doAssert client of RpcWebSocketClient, "subscriptions require websockets"
let id = await client.eth_subscribe(name)
provider.subscriptions[id] = handler
return JsonRpcSubscription(id: id, provider: provider)
method subscribe*(provider: JsonRpcProvider, method subscribe*(provider: JsonRpcProvider,
filter: Filter, filter: Filter,
callback: LogHandler): callback: LogHandler):
@ -179,7 +191,7 @@ method subscribe*(provider: JsonRpcProvider,
proc handler(id, arguments: JsonNode) {.async.} = proc handler(id, arguments: JsonNode) {.async.} =
if blck =? Block.fromJson(arguments["result"]).catch: if blck =? Block.fromJson(arguments["result"]).catch:
await callback(blck) await callback(blck)
return await provider.subscribe("newHeads", Filter.none, handler) return await provider.subscribe("newHeads", handler)
method unsubscribe*(subscription: JsonRpcSubscription) {.async.} = method unsubscribe*(subscription: JsonRpcSubscription) {.async.} =
convertError: convertError:

View File

@ -10,5 +10,6 @@ proc eth_chainId(): UInt256
proc eth_sendTransaction(transaction: Transaction): TransactionHash proc eth_sendTransaction(transaction: Transaction): TransactionHash
proc eth_getTransactionReceipt(hash: TransactionHash): ?TransactionReceipt proc eth_getTransactionReceipt(hash: TransactionHash): ?TransactionReceipt
proc eth_sign(account: Address, message: seq[byte]): seq[byte] proc eth_sign(account: Address, message: seq[byte]): seq[byte]
proc eth_subscribe(name: string, filter = Filter.none): JsonNode proc eth_subscribe(name: string, filter: ?Filter): JsonNode
proc eth_subscribe(name: string): JsonNode
proc eth_unsubscribe(id: JsonNode): bool proc eth_unsubscribe(id: JsonNode): bool