Introduce JsonRpcProvider
This commit is contained in:
parent
fe688bde79
commit
05366c4a49
|
@ -6,6 +6,7 @@ license = "MIT"
|
|||
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 "stew"
|
||||
|
||||
task test, "Run the test suite":
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
import pkg/chronos
|
||||
import pkg/questionable
|
||||
import ./address
|
||||
|
||||
export chronos
|
||||
export address
|
||||
export questionable
|
|
@ -0,0 +1,2 @@
|
|||
type
|
||||
Provider* = ref object of RootObj
|
|
@ -0,0 +1,35 @@
|
|||
import std/uri
|
||||
import pkg/json_rpc/rpcclient
|
||||
import ../basics
|
||||
import ../provider
|
||||
import ./rpccalls
|
||||
|
||||
export basics
|
||||
export provider
|
||||
|
||||
type JsonRpcProvider* = ref object of Provider
|
||||
client: Future[RpcClient]
|
||||
|
||||
const defaultUrl = "http://localhost:8545"
|
||||
|
||||
proc connect(_: type RpcClient, url: string): Future[RpcClient] {.async.} =
|
||||
case parseUri(url).scheme
|
||||
of "ws", "wss":
|
||||
let client = newRpcWebSocketClient()
|
||||
await client.connect(url)
|
||||
return client
|
||||
else:
|
||||
let client = newRpcHttpClient()
|
||||
await client.connect(url)
|
||||
return client
|
||||
|
||||
proc new*(_: type JsonRpcProvider, url=defaultUrl): JsonRpcProvider =
|
||||
JsonRpcProvider(client: RpcClient.connect(url))
|
||||
|
||||
proc listAccounts*(provider: JsonRpcProvider): Future[seq[Address]] {.async.} =
|
||||
let client = await provider.client
|
||||
var addresses: seq[Address]
|
||||
for address in await client.eth_accounts():
|
||||
if address =? Address.init(address):
|
||||
addresses.add(address)
|
||||
return addresses
|
|
@ -0,0 +1,7 @@
|
|||
import std/os
|
||||
import pkg/json_rpc/rpcclient
|
||||
import ../basics
|
||||
|
||||
const file = currentSourcePath.parentDir / "rpccalls" / "signatures.nim"
|
||||
|
||||
createRpcSigs(RpcClient, file)
|
|
@ -0,0 +1 @@
|
|||
proc eth_accounts: seq[string]
|
|
@ -0,0 +1,23 @@
|
|||
import pkg/asynctest
|
||||
import pkg/chronos
|
||||
import pkg/ethers/providers/jsonrpc
|
||||
|
||||
suite "JsonRpcProvider":
|
||||
|
||||
var provider: JsonRpcProvider
|
||||
|
||||
setup:
|
||||
provider = JsonRpcProvider.new("ws://localhost:8545")
|
||||
|
||||
test "can be instantiated with a default URL":
|
||||
discard JsonRpcProvider.new()
|
||||
|
||||
test "can be instantiated with an HTTP URL":
|
||||
discard JsonRpcProvider.new("http://localhost:8545")
|
||||
|
||||
test "can be instantiated with a websocket URL":
|
||||
discard JsonRpcProvider.new("ws://localhost:8545")
|
||||
|
||||
test "lists all accounts":
|
||||
let accounts = await provider.listAccounts()
|
||||
check accounts.len > 0
|
Loading…
Reference in New Issue