Provider.getBlockNumber()

This commit is contained in:
Mark Spanbroek 2022-01-18 14:26:41 +01:00
parent bd20199f87
commit 7bc102a421
8 changed files with 34 additions and 2 deletions

View File

@ -7,6 +7,7 @@ requires "chronos >= 3.0.0 & < 4.0.0"
requires "contractabi >= 0.4.0 & < 0.5.0"
requires "questionable >= 0.10.2 & < 0.11.0"
requires "json_rpc"
requires "stint"
requires "stew"
task test, "Run the test suite":

View File

@ -1,7 +1,9 @@
import pkg/chronos
import pkg/questionable
import pkg/stint
import ./address
export chronos
export address
export questionable
export stint

View File

@ -1,2 +1,9 @@
import ./basics
export basics
type
Provider* = ref object of RootObj
method getBlockNumber*(provider: Provider): Future[UInt256] {.base.} =
doAssert false, "not implemented"

View File

@ -35,3 +35,7 @@ proc send*(provider: JsonRpcProvider,
proc listAccounts*(provider: JsonRpcProvider): Future[seq[Address]] {.async.} =
let client = await provider.client
return await client.eth_accounts()
method getBlockNumber*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
let client = await provider.client
return await client.eth_blockNumber()

View File

@ -1,11 +1,21 @@
import std/json
import ../../basics
# Address
func `%`*(address: Address): JsonNode =
%($address)
func fromJson*(json: JsonNode, argname: string, result: var Address) =
func fromJson*(json: JsonNode, name: string, result: var Address) =
if address =? Address.init(json.getStr()):
result = address
else:
raise newException(ValueError, "\"" & argname & "\"is not an Address")
raise newException(ValueError, "\"" & name & "\"is not an Address")
# UInt256
func `%`*(integer: UInt256): JsonNode =
%toHex(integer)
func fromJson*(json: JsonNode, name: string, result: var UInt256) =
result = UInt256.fromHex(json.getStr())

View File

@ -1 +1,2 @@
proc eth_accounts: seq[Address]
proc eth_blockNumber: UInt256

View File

@ -1 +1,2 @@
-d:"chronicles_log_level=INFO"
--warning[LockLevel]:off

View File

@ -26,3 +26,9 @@ suite "JsonRpcProvider":
test "sends raw messages to the provider":
let response = await provider.send("evm_mine")
check response == %"0x0"
test "returns block number":
let blocknumber1 = await provider.getBlockNumber()
discard await provider.send("evm_mine")
let blocknumber2 = await provider.getBlockNumber()
check blocknumber2 > blocknumber1