Packages

Ethereum library for Gleam - JSON-RPC client, transaction signing, ABI encoding, and wallet management on the BEAM

Current section

Files

Jump to
gleeth src gleeth@rpc@client.erl
Raw

src/gleeth@rpc@client.erl

-module(gleeth@rpc@client).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleeth/rpc/client.gleam").
-export([make_request/3]).
-file("src/gleeth/rpc/client.gleam", 39).
-spec encode_request(gleeth@rpc@types:json_rpc_request()) -> {ok, binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
encode_request(Req) ->
Json_object = gleam@json:object(
[{<<"jsonrpc"/utf8>>, gleam@json:string(erlang:element(2, Req))},
{<<"method"/utf8>>, gleam@json:string(erlang:element(3, Req))},
{<<"params"/utf8>>,
gleam@json:array(erlang:element(4, Req), fun(X) -> X end)},
{<<"id"/utf8>>, gleam@json:int(erlang:element(5, Req))}]
),
{ok, gleam@json:to_string(Json_object)}.
-file("src/gleeth/rpc/client.gleam", 85).
-spec parse_url(binary()) -> {ok, {binary(), binary(), binary()}} |
{error, gleeth@rpc@types:gleeth_error()}.
parse_url(Url) ->
case gleam@string:split(Url, <<"://"/utf8>>) of
[Scheme, Rest] ->
case gleam@string:split_once(Rest, <<"/"/utf8>>) of
{ok, {Host, Path}} ->
{ok, {Scheme, Host, <<"/"/utf8, Path/binary>>}};
{error, _} ->
{ok, {Scheme, Rest, <<"/"/utf8>>}}
end;
_ ->
{error,
{invalid_rpc_url,
<<"Invalid URL format - must include scheme (http:// or https://)"/utf8>>}}
end.
-file("src/gleeth/rpc/client.gleam", 54).
-spec send_http_request(binary(), binary()) -> {ok,
gleam@http@response:response(binary())} |
{error, gleeth@rpc@types:gleeth_error()}.
send_http_request(Rpc_url, Body) ->
Req = begin
_pipe = gleam@http@request:new(),
_pipe@1 = gleam@http@request:set_method(_pipe, post),
_pipe@2 = gleam@http@request:set_body(_pipe@1, Body),
_pipe@3 = gleam@http@request:set_header(
_pipe@2,
<<"content-type"/utf8>>,
<<"application/json"/utf8>>
),
gleam@http@request:set_header(
_pipe@3,
<<"user-agent"/utf8>>,
<<"gleeth/1.0"/utf8>>
)
end,
case parse_url(Rpc_url) of
{ok, {Scheme, Host, Path}} ->
Req_with_scheme = case Scheme of
<<"https"/utf8>> ->
gleam@http@request:set_scheme(Req, https);
_ ->
gleam@http@request:set_scheme(Req, http)
end,
Req_with_host = gleam@http@request:set_host(Req_with_scheme, Host),
Req_with_path = gleam@http@request:set_path(Req_with_host, Path),
case gleam@httpc:send(Req_with_path) of
{ok, Response} ->
{ok, Response};
{error, _} ->
{error,
{network_error, <<"Failed to send HTTP request"/utf8>>}}
end;
{error, Err} ->
{error, Err}
end.
-file("src/gleeth/rpc/client.gleam", 13).
-spec make_request(binary(), binary(), list(gleam@json:json())) -> {ok,
binary()} |
{error, gleeth@rpc@types:gleeth_error()}.
make_request(Rpc_url, Method, Params) ->
Json_rpc_request = {json_rpc_request, <<"2.0"/utf8>>, Method, Params, 1},
gleam@result:'try'(
encode_request(Json_rpc_request),
fun(Request_json) ->
gleam@result:'try'(
send_http_request(Rpc_url, Request_json),
fun(Http_response) -> case erlang:element(2, Http_response) of
200 ->
{ok, erlang:element(4, Http_response)};
Status ->
{error,
{network_error,
<<"HTTP request failed with status: "/utf8,
(erlang:integer_to_binary(Status))/binary>>}}
end end
)
end
).