Skip to content

Commit 4a5d8f9

Browse files
authored
Fix ci / add docs about websocket (#165)
* update ci * readme changes * try to fix tests * fix dialyzer * fix * fix dialyzer * fix dialyzer * fix test
1 parent 1a79976 commit 4a5d8f9

File tree

6 files changed

+52
-15
lines changed

6 files changed

+52
-15
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,27 @@ jobs:
1515
matrix:
1616
include:
1717
- pair:
18-
elixir: 1.14
19-
otp: 24.3
20-
lint: lint
18+
elixir: 1.14.5
19+
otp: 25.3.2
2120
- pair:
22-
elixir: 1.14
23-
otp: 25.0
24-
lint: lint
21+
elixir: 1.15.7
22+
otp: 25.3.2
23+
- pair:
24+
elixir: 1.16.1
25+
otp: 25.3.2
2526
- pair:
26-
elixir: 1.15
27-
otp: 26.0
27+
elixir: 1.14.5
28+
otp: 26.2.2
29+
- pair:
30+
elixir: 1.15.7
31+
otp: 26.2.2
32+
- pair:
33+
elixir: 1.16.1
34+
otp: 26.2.2
2835
lint: lint
36+
- pair:
37+
elixir: 1.17.3
38+
otp: 27.2
2939

3040
runs-on: ubuntu-latest
3141

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ end
3636

3737
## Configuration
3838

39+
### HTTP
40+
3941
In `config/config.exs`, add Ethereum protocol host params to your config file
4042

4143
```elixir
@@ -55,6 +57,8 @@ config :ethereumex,
5557
`:pool_timeout` - This timeout is applied when we check out a connection from the pool. Default value is `5_000`.
5658
`:receive_timeout` - The maximum time to wait for a response before returning an error. Default value is `15_000`
5759

60+
### IPC
61+
5862
If you want to use IPC you will need to set a few things in your config.
5963

6064
First, specify the `:client_type`:
@@ -73,6 +77,15 @@ config :ethereumex,
7377
ipc_path: "/path/to/ipc"
7478
```
7579

80+
### Websocket
81+
82+
For websocket connection, please set `:websocket_url` and `:client_type` as `:webscoket`:
83+
```elixir
84+
config :ethereumex,
85+
websocket_url: "ws://localhost:8545",
86+
client_type: :websocket
87+
```
88+
7689
If you want to count the number of RPC calls per RPC method or overall,
7790
you can attach yourself to executed telemetry events.
7891
There are two events you can attach yourself to:

lib/ethereumex/client/websocket_server.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ defmodule Ethereumex.WebsocketServer do
7979
Starts the WebSocket connection.
8080
8181
## Options
82-
- :url - WebSocket endpoint URL (defaults to Config.rpc_url())
82+
- :url - WebSocket endpoint URL (defaults to Config.websocket_url())
8383
- :name - Process name (defaults to __MODULE__)
8484
"""
8585
@spec start_link(keyword()) :: {:ok, pid()} | {:error, term()}
8686
def start_link(opts \\ []) do
87-
url = Keyword.get(opts, :url, Config.rpc_url())
87+
url = Keyword.get(opts, :url, Config.websocket_url())
8888
name = Keyword.get(opts, :name, __MODULE__)
8989

9090
WebSockex.start_link(
@@ -102,7 +102,8 @@ defmodule Ethereumex.WebsocketServer do
102102
Returns `{:ok, result}` on success or `{:error, reason}` on failure.
103103
Times out after #{@request_timeout}ms.
104104
"""
105-
@spec post(String.t()) :: {:ok, term()} | {:error, term()}
105+
@spec post(binary()) ::
106+
{:ok, term()} | {:error, :invalid_request_format | :timeout | :decoded_error}
106107
def post(encoded_request) when is_binary(encoded_request) do
107108
with {:ok, decoded} <- decode_request(encoded_request),
108109
id <- get_request_id(decoded),
@@ -157,7 +158,7 @@ defmodule Ethereumex.WebsocketServer do
157158
{:ok, %{"id" => _id} = decoded} -> {:ok, decoded}
158159
{:ok, decoded} when is_list(decoded) -> {:ok, decoded}
159160
{:ok, _} -> {:error, :invalid_request_format}
160-
error -> error
161+
_error -> {:error, :decode_error}
161162
end
162163
end
163164

lib/ethereumex/config.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ defmodule Ethereumex.Config do
4343
end
4444
end
4545

46+
@spec websocket_url() :: binary()
47+
def websocket_url() do
48+
case Application.get_env(:ethereumex, :websocket_url, "") do
49+
url when is_binary(url) and url != "" ->
50+
url
51+
52+
not_a_url ->
53+
raise ArgumentError,
54+
message:
55+
"Please set config variable `config :ethereumex, :websocket_url, \"ws://...\", got `#{not_a_url}`."
56+
end
57+
end
58+
4659
@spec ipc_path() :: binary()
4760
def ipc_path() do
4861
case Application.get_env(:ethereumex, :ipc_path, "") do

test/ethereumex/websocket_client_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule Ethereumex.WebsocketClientTest do
99

1010
setup_all do
1111
_ = Application.put_env(:ethereumex, :client_type, :websocket)
12-
_ = Application.put_env(:ethereumex, :url, @default_url)
12+
_ = Application.put_env(:ethereumex, :websocket_url, @default_url)
1313

1414
on_exit(fn ->
1515
_ = Application.put_env(:ethereumex, :client_type, :http)

test/ethereumex/websocket_server_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ defmodule Ethereumex.WebsocketServerTest do
1313

1414
setup_all do
1515
_ = Application.put_env(:ethereumex, :client_type, :websocket)
16-
_ = Application.put_env(:ethereumex, :url, @default_url)
16+
_ = Application.put_env(:ethereumex, :websocket_url, @default_url)
1717

1818
on_exit(fn ->
1919
_ = Application.put_env(:ethereumex, :client_type, :http)
@@ -133,7 +133,7 @@ defmodule Ethereumex.WebsocketServerTest do
133133
end
134134

135135
test "handles invalid JSON request" do
136-
assert {:error, %Jason.DecodeError{}} = WebsocketServer.post("invalid json")
136+
assert {:error, :decode_error} = WebsocketServer.post("invalid json")
137137
end
138138

139139
test "handles request without id" do

0 commit comments

Comments
 (0)