[")](https://travis-ci.org/status-im/nim-eth-rpc)
[")](https://ci.appveyor.com/project/jarradh/nim-eth-rpc)
Remote procedure calls are created using the `rpc` macro on an instance of `RpcRouter`.
`rpc` allows you to provide a list of native Nim type parameters and a return type, generates marshalling to and from json for you, so you can concentrate on using native Nim types for your call.
Routing is then performed by the `route` procedure.
When an error occurs, the `error` is populated, otherwise `result` will be populated.
### Parameters
`route`
`path`: The string to match for the `method`.
`body`: The parameters and code to execute for this call.
Note that once invoked all RPC calls are error trapped and any exceptions raised are passed back with the error message encoded as a `JsonNode`.
`route`
`router: RpcRouter`: The router object that contains the RPCs.
`data: string`: A string ready to be processed into a `JsonNode`.
Returns
`Future[string]`: This will be the stringified JSON response, which can be the JSON RPC result or a JSON wrapped error.
This `route` variant will handle all the conversion of `string` to `JsonNode` and check the format and type of the input data.
`route`
`router: RpcRouter`: The router object that contains the RPCs.
`node: JsonNode`: A pre-processed JsonNode that matches the expected format as defined above.
Returns
`Future[JsonNode]`: The JSON RPC result or a JSON wrapped error.
This variant allows simplified processing if you already have a `JsonNode`. However if the required fields are not present within `node`, exceptions will be raised.
`tryRoute`
`router: RpcRouter`: The router object that contains the RPCs.
`node: JsonNode`: A pre-processed JsonNode that matches the expected format as defined above.
`fut: var Future[JsonNode]`: The JSON RPC result or a JSON wrapped error.
Returns
`bool`: Returns `true` if the `method` field provided in `node` matches an available route. Returns `false` when the `method` cannot be found, or if `method` or `params` field cannot be found within `node`.
This `route` variant allows you to invoke a call if possible, without raising an exception.
To see the result of a call, we need to provide Json in the expected format.
Here's an example of how that looks by manually creating the JSON. Later we will see the helper utilities that make this easier.
To make things more readable and allow better static checking client side, Json-Rpc supports generating wrappers for client RPCs using `createRpcSigs`.
This macro takes a type name and the path of a file containing forward declarations of procedures that you wish to convert to client RPCs. The transformation generates procedures that match the forward declarations provided, plus a `client` parameter in the specified type.
Because the signatures are parsed at compile time, the file will be error checked and you can use import to share common types between your client and server.
This allows you to leverage Nim's static type checking whilst also aiding readability and providing a unified location to declare client side RPC definitions.
## Working with client transports
Transport clients should provide a type that is inherited from `RpcClient` where they can store any transport related information.
Additionally, the following two procedures are useful:
*`Call`
`self`: a descendant of `RpcClient`
`name: string`: the method to be called
`params: JsonNode`: The parameters to the RPC call
Returning
`Future[Response]`: A wrapper for the result `JsonNode` and a flag to indicate if this contains an error.
Note: Although `call` isn't necessary for a client to function, it allows RPC signatures to be used by the `createRpcSigs`.
*`Connect`
`client`: a descendant of `RpcClient`
Returning
`FutureBase`: The base future returned when a procedure is annoted with `{.async.}`
### `processMessage`
To simplify and unify processing within the client, the `processMessage` procedure can be used to perform conversion and error checking from the received string originating from the transport to `JsonNode`.
`processMessages`
`self`: a client type descended from `RpcClient`
`line: string`: a string that contains the JSON to be processed
This procedure then completes the futures set by `call` invocations using the `id` field of the processed `JsonNode` from `line`.