Subscribe to new blocks
This commit is contained in:
parent
82043c2fcc
commit
bbf133725f
|
@ -18,6 +18,7 @@ type
|
||||||
data*: seq[byte]
|
data*: seq[byte]
|
||||||
topics*: seq[Topic]
|
topics*: seq[Topic]
|
||||||
LogHandler* = proc(log: Log) {.gcsafe, upraises:[].}
|
LogHandler* = proc(log: Log) {.gcsafe, upraises:[].}
|
||||||
|
BlockHandler* = proc(blck: Block) {.gcsafe, upraises:[].}
|
||||||
Topic* = array[32, byte]
|
Topic* = array[32, byte]
|
||||||
Block* = object
|
Block* = object
|
||||||
number*: UInt256
|
number*: UInt256
|
||||||
|
@ -57,5 +58,10 @@ method subscribe*(provider: Provider,
|
||||||
Future[Subscription] {.base.} =
|
Future[Subscription] {.base.} =
|
||||||
doAssert false, "not implemented"
|
doAssert false, "not implemented"
|
||||||
|
|
||||||
|
method subscribe*(provider: Provider,
|
||||||
|
callback: BlockHandler):
|
||||||
|
Future[Subscription] {.base.} =
|
||||||
|
doAssert false, "not implemented"
|
||||||
|
|
||||||
method unsubscribe*(subscription: Subscription) {.base, async.} =
|
method unsubscribe*(subscription: Subscription) {.base, async.} =
|
||||||
doAssert false, "not implemented"
|
doAssert false, "not implemented"
|
||||||
|
|
|
@ -127,21 +127,34 @@ method getChainId*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
|
||||||
except CatchableError:
|
except CatchableError:
|
||||||
return parse(await client.net_version(), UInt256)
|
return parse(await client.net_version(), UInt256)
|
||||||
|
|
||||||
|
proc subscribe(provider: JsonRpcProvider,
|
||||||
|
name: string,
|
||||||
|
filter: ?Filter,
|
||||||
|
handler: SubscriptionHandler): Future[Subscription] {.async.} =
|
||||||
|
let client = await provider.client
|
||||||
|
doAssert client of RpcWebSocketClient, "subscriptions require websockets"
|
||||||
|
|
||||||
|
let id = await client.eth_subscribe(name, filter)
|
||||||
|
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):
|
||||||
Future[Subscription] {.async.} =
|
Future[Subscription] {.async.} =
|
||||||
let client = await provider.client
|
|
||||||
doAssert client of RpcWebSocketClient, "subscriptions require websockets"
|
|
||||||
|
|
||||||
proc handler(id, arguments: JsonNode) =
|
proc handler(id, arguments: JsonNode) =
|
||||||
if log =? Log.fromJson(arguments["result"]).catch:
|
if log =? Log.fromJson(arguments["result"]).catch:
|
||||||
callback(log)
|
callback(log)
|
||||||
|
return await provider.subscribe("logs", filter.some, handler)
|
||||||
|
|
||||||
let id = await client.eth_subscribe("logs", some filter)
|
method subscribe*(provider: JsonRpcProvider,
|
||||||
provider.subscriptions[id] = handler
|
callback: BlockHandler):
|
||||||
|
Future[Subscription] {.async.} =
|
||||||
return JsonRpcSubscription(id: id, provider: provider)
|
proc handler(id, arguments: JsonNode) =
|
||||||
|
if blck =? Block.fromJson(arguments["result"]).catch:
|
||||||
|
callback(blck)
|
||||||
|
return await provider.subscribe("newHeads", Filter.none, handler)
|
||||||
|
|
||||||
method unsubscribe*(subscription: JsonRpcSubscription) {.async.} =
|
method unsubscribe*(subscription: JsonRpcSubscription) {.async.} =
|
||||||
let provider = subscription.provider
|
let provider = subscription.provider
|
||||||
|
|
|
@ -9,5 +9,5 @@ proc eth_estimateGas(transaction: Transaction): UInt256
|
||||||
proc eth_chainId(): UInt256
|
proc eth_chainId(): UInt256
|
||||||
proc eth_sendTransaction(transaction: Transaction): array[32, byte]
|
proc eth_sendTransaction(transaction: Transaction): array[32, byte]
|
||||||
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): JsonNode
|
proc eth_subscribe(name: string, filter = Filter.none): JsonNode
|
||||||
proc eth_unsubscribe(id: JsonNode): bool
|
proc eth_unsubscribe(id: JsonNode): bool
|
||||||
|
|
|
@ -39,3 +39,14 @@ suite "JsonRpcProvider":
|
||||||
check block1.hash != block2.hash
|
check block1.hash != block2.hash
|
||||||
check block1.number < block2.number
|
check block1.number < block2.number
|
||||||
check block1.timestamp < block2.timestamp
|
check block1.timestamp < block2.timestamp
|
||||||
|
|
||||||
|
test "subscribes to new blocks":
|
||||||
|
let oldBlock = !await provider.getBlock(BlockTag.latest)
|
||||||
|
var newBlock: Block
|
||||||
|
let blockHandler = proc(blck: Block) = newBlock = blck
|
||||||
|
let subscription = await provider.subscribe(blockHandler)
|
||||||
|
discard await provider.send("evm_mine")
|
||||||
|
check newBlock.number > oldBlock.number
|
||||||
|
check newBlock.timestamp > oldBlock.timestamp
|
||||||
|
check newBlock.hash != oldBlock.hash
|
||||||
|
await subscription.unsubscribe()
|
||||||
|
|
Loading…
Reference in New Issue