Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic request method #4

Merged
merged 4 commits into from
Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 0.1.2
* Added generic request method

# 0.1.1
* Added necessary JSON header for parity
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Add Ethereumex to your `mix.exs` dependencies:
1. Add `ethereumex` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:ethereumex, "~> 0.1.0"}]
[{:ethereumex, "~> 0.1.2"}]
end
```

Expand All @@ -33,9 +33,6 @@ config :ethereumex,

## Usage

### Note
Currently only official [DApp APIs](https://github.com/ethereum/wiki/wiki/JSON-RPC) are implemented. More features (new api methods, smart contract creation) will be added soon.

### Available methods:

* web3_clientVersion
Expand Down Expand Up @@ -119,6 +116,19 @@ iex> Ethereumex.HttpClient.eth_get_balance(["0x407d73d8a49eeb85d32cf465507dd71d5
```
Note that all method names are snakecases, so, for example, shh_getMessages method has corresponding Ethereumex.HttpClient.shh_get_messages/1 method

### Custom requests
Many Ethereum protocol implementations support additional JSON-RPC API methods. To use them, you should call Ethereumex.HttpClient.send_request/2 method.

For example, let's call geth's rpc_modules method.

```elixir
iex> Ethereumex.HttpClient.send_request("rpc_modules")
{:ok,
%{"id" => 5, "jsonrpc" => "2.0",
"result" => %{"eth" => "1.0", "net" => "1.0", "rpc" => "1.0",
"web3" => "1.0"}}}
```

## Contributing

1. [Fork it!](http://github.com/ayrat555/ethereumex/fork)
Expand Down
22 changes: 22 additions & 0 deletions fixture/vcr_cassettes/http_client_custom_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"request": {
"body": "{\"params\":[],\"method\":\"rpc_modules\",\"jsonrpc\":\"2.0\",\"id\":3}",
"headers": [],
"method": "post",
"options": [],
"request_body": "",
"url": "http://localhost:8545"
},
"response": {
"body": "{\"jsonrpc\":\"2.0\",\"id\":3,\"result\":{\"eth\":\"1.0\",\"net\":\"1.0\",\"rpc\":\"1.0\",\"web3\":\"1.0\"}}\n",
"headers": {
"Content-Type": "application/json",
"Date": "Sun, 24 Sep 2017 15:55:06 GMT",
"Content-Length": "85"
},
"status_code": 200,
"type": "ok"
}
}
]
18 changes: 12 additions & 6 deletions lib/ethereumex/client.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Ethereumex.Client do
@callback request(payload :: map) :: tuple
@callback request(map()) :: any()
@moduledoc false

alias Ethereumex.Client.Utils
Expand Down Expand Up @@ -34,16 +34,22 @@ defmodule Ethereumex.Client do
methods
|> Enum.each(fn({original_name, formatted_name}) ->
def unquote(formatted_name)(params \\ []) when is_list(params) do
params = params |> add_method_info(unquote(original_name))

GenServer.call __MODULE__, {:request, params}
send_request(unquote(original_name), params)
end
end)

def request(_) do
@spec send_request(binary(), [binary()] | [map()]) :: any()
def send_request(method_name, params \\ []) when is_list(params) do
params = params |> add_method_info(method_name)

GenServer.call __MODULE__, {:request, params}
end

@spec request(map()) :: any()
def request(_payload) do
end

@spec add_method_info([binary] | [map], binary()) :: map()
@spec add_method_info([binary()] | [map()], binary()) :: map()
defp add_method_info(params, method_name) do
%{}
|> Map.put("method", method_name)
Expand Down
3 changes: 2 additions & 1 deletion lib/ethereumex/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ defmodule Ethereumex.HttpClient do
import Ethereumex.Config
@moduledoc false

@spec post_request(map()) :: {:ok | :error, any()}
@spec request(map()) :: {:ok | :error, any()}
def request(payload) do
payload
|> encode_payload
|> post_request
end

@spec encode_payload(map()) :: binary()
defp encode_payload(payload) do
payload |> Poison.encode!
end
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Ethereumex.Mixfile do

def project do
[app: :ethereumex,
version: "0.1.0",
version: "0.1.2",
elixir: "~> 1.4",
description: "Elixir JSON-RPC client for the Ethereum blockchain",
package: [
Expand Down
19 changes: 19 additions & 0 deletions test/ethereum/http_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,23 @@ defmodule Ethereumex.HttpClientTest do
} = result
end
end

test "sends custom geth request" do
use_cassette "http_client_custom_request" do
result = HttpClient.send_request("rpc_modules")

{:ok,
%{"id" => 3,
"jsonrpc" => "2.0",
"result" =>
%{
"eth" => "1.0",
"net" => "1.0",
"rpc" => "1.0",
"web3" => "1.0"
}
}
} = result
end
end
end