Add polling interval to constructor of provider

This commit is contained in:
Mark Spanbroek 2023-06-27 15:08:37 +02:00 committed by markspanbroek
parent 88d60b14b0
commit 1b151d589d
2 changed files with 7 additions and 3 deletions

View File

@ -47,11 +47,14 @@ template convertError(body) =
# Provider
const defaultUrl = "http://localhost:8545"
const defaultPollingInterval = 4.seconds
proc jsonHeaders: seq[(string, string)] =
@[("Content-Type", "application/json")]
proc new*(_: type JsonRpcProvider, url=defaultUrl): JsonRpcProvider =
proc new*(_: type JsonRpcProvider,
url=defaultUrl,
pollingInterval=defaultPollingInterval): JsonRpcProvider =
var initialized: Future[void]
var client: RpcClient
var subscriptions: JsonRpcSubscriptions
@ -67,7 +70,8 @@ proc new*(_: type JsonRpcProvider, url=defaultUrl): JsonRpcProvider =
let http = newRpcHttpClient(getHeaders = jsonHeaders)
await http.connect(url)
client = http
subscriptions = JsonRpcSubscriptions.new(http)
subscriptions = JsonRpcSubscriptions.new(http,
pollingInterval = pollingInterval)
proc awaitClient: Future[RpcClient] {.async.} =
await initialized

View File

@ -14,7 +14,7 @@ for url in ["ws://localhost:8545", "http://localhost:8545"]:
var provider: JsonRpcProvider
setup:
provider = JsonRpcProvider.new(url)
provider = JsonRpcProvider.new(url, pollingInterval = 100.millis)
test "can be instantiated with a default URL":
discard JsonRpcProvider.new()