mirror of
https://github.com/logos-storage/nim-ethers.git
synced 2026-01-05 23:23:08 +00:00
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 "chronos >= 3.0.0 & < 4.0.0"
|
||||||
requires "contractabi >= 0.4.0 & < 0.5.0"
|
requires "contractabi >= 0.4.0 & < 0.5.0"
|
||||||
requires "questionable >= 0.10.2 & < 0.11.0"
|
requires "questionable >= 0.10.2 & < 0.11.0"
|
||||||
|
requires "json_rpc"
|
||||||
requires "stew"
|
requires "stew"
|
||||||
|
|
||||||
task test, "Run the test suite":
|
task test, "Run the test suite":
|
||||||
|
|||||||
7
ethers/basics.nim
Normal file
7
ethers/basics.nim
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import pkg/chronos
|
||||||
|
import pkg/questionable
|
||||||
|
import ./address
|
||||||
|
|
||||||
|
export chronos
|
||||||
|
export address
|
||||||
|
export questionable
|
||||||
2
ethers/provider.nim
Normal file
2
ethers/provider.nim
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
type
|
||||||
|
Provider* = ref object of RootObj
|
||||||
35
ethers/providers/jsonrpc.nim
Normal file
35
ethers/providers/jsonrpc.nim
Normal file
@ -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
|
||||||
7
ethers/providers/rpccalls.nim
Normal file
7
ethers/providers/rpccalls.nim
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import std/os
|
||||||
|
import pkg/json_rpc/rpcclient
|
||||||
|
import ../basics
|
||||||
|
|
||||||
|
const file = currentSourcePath.parentDir / "rpccalls" / "signatures.nim"
|
||||||
|
|
||||||
|
createRpcSigs(RpcClient, file)
|
||||||
1
ethers/providers/rpccalls/signatures.nim
Normal file
1
ethers/providers/rpccalls/signatures.nim
Normal file
@ -0,0 +1 @@
|
|||||||
|
proc eth_accounts: seq[string]
|
||||||
23
testmodule/testJsonRpcProvider.nim
Normal file
23
testmodule/testJsonRpcProvider.nim
Normal file
@ -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…
x
Reference in New Issue
Block a user