2018-03-02 11:46:59 +00:00
|
|
|
import asyncnet, asyncdispatch, tables, json, oids, ethcalls, macros
|
|
|
|
|
|
|
|
type
|
|
|
|
RpcClient* = ref object
|
|
|
|
socket: AsyncSocket
|
|
|
|
awaiting: Table[string, Future[Response]]
|
|
|
|
address: string
|
|
|
|
port: Port
|
2018-04-11 20:07:24 +01:00
|
|
|
nextId: int64
|
2018-03-02 11:46:59 +00:00
|
|
|
Response* = tuple[error: bool, result: JsonNode]
|
|
|
|
|
2018-04-11 20:07:24 +01:00
|
|
|
|
2018-03-02 11:46:59 +00:00
|
|
|
proc newRpcClient*(): RpcClient =
|
|
|
|
## Creates a new ``RpcClient`` instance.
|
|
|
|
RpcClient(
|
|
|
|
socket: newAsyncSocket(),
|
2018-04-11 20:07:24 +01:00
|
|
|
awaiting: initTable[string, Future[Response]](),
|
|
|
|
nextId: 1
|
2018-03-02 11:46:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc call*(self: RpcClient, name: string, params: JsonNode): Future[Response] {.async.} =
|
|
|
|
## Remotely calls the specified RPC method.
|
2018-04-11 20:07:24 +01:00
|
|
|
let id = $self.nextId
|
|
|
|
self.nextId.inc
|
|
|
|
let msg = $ %{"jsonrpc": %"2.0", "method": %name, "params": params, "id": %id} & "\c\l"
|
|
|
|
await self.socket.send(msg)
|
2018-03-02 11:46:59 +00:00
|
|
|
|
2018-04-11 20:07:24 +01:00
|
|
|
# completed by processMessage.
|
2018-03-22 17:27:28 +00:00
|
|
|
var newFut = newFuture[Response]()
|
2018-04-11 20:07:24 +01:00
|
|
|
# add to awaiting responses
|
|
|
|
self.awaiting[id] = newFut
|
2018-03-22 17:27:28 +00:00
|
|
|
result = await newFut
|
2018-03-02 11:46:59 +00:00
|
|
|
|
|
|
|
proc processMessage(self: RpcClient, line: string) =
|
|
|
|
let node = parseJson(line)
|
|
|
|
|
2018-04-11 20:07:24 +01:00
|
|
|
# TODO: Use more appropriate exception objects
|
|
|
|
if not node.hasKey("jsonrpc"): raise newException(ValueError, "Message is missing rpc version field")
|
|
|
|
elif node["jsonrpc"].str != "2.0": raise newException(ValueError, "Unsupported version of JSON, expected 2.0, received \"" & node["jsonrpc"].str & "\"")
|
|
|
|
elif not node.hasKey("id"): raise newException(ValueError, "Message is missing id field")
|
|
|
|
elif not self.awaiting.hasKey(node["id"].str): raise newException(ValueError, "Cannot find message id \"" & node["id"].str & "\"")
|
2018-03-02 11:46:59 +00:00
|
|
|
|
|
|
|
if node["error"].kind == JNull:
|
|
|
|
self.awaiting[node["id"].str].complete((false, node["result"]))
|
|
|
|
self.awaiting.del(node["id"].str)
|
|
|
|
else:
|
2018-04-11 20:07:24 +01:00
|
|
|
self.awaiting[node["id"].str].complete((true, node["error"]))
|
|
|
|
self.awaiting.del(node["id"].str)
|
2018-03-02 11:46:59 +00:00
|
|
|
|
|
|
|
proc connect*(self: RpcClient, address: string, port: Port): Future[void]
|
|
|
|
|
|
|
|
proc processData(self: RpcClient) {.async.} =
|
|
|
|
while true:
|
|
|
|
# read until no data
|
|
|
|
let line = await self.socket.recvLine()
|
|
|
|
|
|
|
|
if line == "":
|
|
|
|
# transmission ends
|
|
|
|
self.socket.close() # TODO: Do we need to drop/reacquire sockets?
|
|
|
|
self.socket = newAsyncSocket()
|
|
|
|
break
|
|
|
|
|
|
|
|
processMessage(self, line)
|
|
|
|
# async loop reconnection and waiting
|
|
|
|
await connect(self, self.address, self.port)
|
|
|
|
|
|
|
|
proc connect*(self: RpcClient, address: string, port: Port) {.async.} =
|
|
|
|
await self.socket.connect(address, port)
|
|
|
|
self.address = address
|
|
|
|
self.port = port
|
|
|
|
asyncCheck processData(self)
|
|
|
|
|
|
|
|
macro generateCalls: untyped =
|
|
|
|
## Generate templates for client calls so that:
|
|
|
|
## client.call("web3_clientVersion", params)
|
|
|
|
## can be written as:
|
|
|
|
## client.web3_clientVersion(params)
|
|
|
|
result = newStmtList()
|
|
|
|
for callName in ETHEREUM_RPC_CALLS:
|
2018-04-11 20:07:24 +01:00
|
|
|
let nameLit = ident(callName)
|
|
|
|
result.add(quote do:
|
2018-04-12 18:49:58 +01:00
|
|
|
proc `nameLit`*(client: RpcClient, params: JsonNode): Future[Response] {.inline.} = client.call(`callName`, params) # TODO: Back to template
|
2018-04-11 20:07:24 +01:00
|
|
|
)
|
2018-03-02 11:46:59 +00:00
|
|
|
|
|
|
|
# generate all client ethereum rpc calls
|
|
|
|
generateCalls()
|