nim-ethers/ethers/providers/jsonrpc.nim

44 lines
1.2 KiB
Nim
Raw Normal View History

2022-01-18 11:10:20 +00:00
import std/uri
import pkg/json_rpc/rpcclient
import ../basics
import ../provider
import ./rpccalls
export basics
export provider
push: {.upraises: [].}
2022-01-18 11:10:20 +00:00
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))
2022-01-18 13:24:46 +00:00
proc send*(provider: JsonRpcProvider,
call: string,
arguments = %(@[])): Future[JsonNode] {.async.} =
let client = await provider.client
return await client.call(call, arguments)
2022-01-18 11:10:20 +00:00
proc listAccounts*(provider: JsonRpcProvider): Future[seq[Address]] {.async.} =
let client = await provider.client
2022-01-18 11:42:58 +00:00
return await client.eth_accounts()
2022-01-18 13:26:41 +00:00
method getBlockNumber*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
let client = await provider.client
return await client.eth_blockNumber()