-module(gleeth@rpc@response_utils). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/gleeth/rpc/response_utils.gleam"). -export([decode_rpc_response/2, make_string_request/3, make_string_request_with_provider/3, make_decoded_request/4, make_decoded_request_with_provider/4]). -file("src/gleeth/rpc/response_utils.gleam", 10). -spec decode_rpc_error(binary()) -> gleam@option:option(binary()). decode_rpc_error(Body) -> Error_decoder = gleam@dynamic@decode:at( [<<"error"/utf8>>, <<"message"/utf8>>], gleam@dynamic@decode:optional( {decoder, fun gleam@dynamic@decode:decode_string/1} ) ), case gleam@json:parse(Body, Error_decoder) of {ok, {some, Msg}} -> {some, Msg}; _ -> none end. -file("src/gleeth/rpc/response_utils.gleam", 22). -spec decode_rpc_response(binary(), gleam@dynamic@decode:decoder(OFC)) -> {ok, OFC} | {error, gleeth@rpc@types:gleeth_error()}. decode_rpc_response(Body, Result_decoder) -> case decode_rpc_error(Body) of {some, Msg} -> {error, {rpc_error, <<"RPC Error: "/utf8, Msg/binary>>}}; none -> Envelope_decoder = gleam@dynamic@decode:at( [<<"result"/utf8>>], Result_decoder ), case gleam@json:parse(Body, Envelope_decoder) of {ok, Value} -> {ok, Value}; {error, {unable_to_decode, _}} -> {error, {parse_error, <<"Failed to decode RPC result"/utf8>>}}; {error, unexpected_end_of_input} -> {error, {parse_error, <<"Unexpected end of JSON input"/utf8>>}}; {error, {unexpected_byte, B}} -> {error, {parse_error, <<"Unexpected byte in JSON: "/utf8, B/binary>>}}; {error, {unexpected_sequence, S}} -> {error, {parse_error, <<"Unexpected sequence in JSON: "/utf8, S/binary>>}} end end. -file("src/gleeth/rpc/response_utils.gleam", 47). -spec make_string_request( binary(), gleeth@rpc@types:eth_method(), list(gleam@json:json()) ) -> {ok, binary()} | {error, gleeth@rpc@types:gleeth_error()}. make_string_request(Rpc_url, Method, Params) -> gleam@result:'try'( gleeth@rpc@client:make_request( Rpc_url, gleeth@rpc@types:method_to_string(Method), Params ), fun(Body) -> decode_rpc_response( Body, {decoder, fun gleam@dynamic@decode:decode_string/1} ) end ). -file("src/gleeth/rpc/response_utils.gleam", 62). -spec make_string_request_with_provider( gleeth@provider:provider(), gleeth@rpc@types:eth_method(), list(gleam@json:json()) ) -> {ok, binary()} | {error, gleeth@rpc@types:gleeth_error()}. make_string_request_with_provider(P, Method, Params) -> Retry = gleeth@provider:retry_config(P), gleam@result:'try'(case erlang:element(2, Retry) > 0 of true -> gleeth@rpc@client:make_request_with_retry( gleeth@provider:rpc_url(P), gleeth@rpc@types:method_to_string(Method), Params, Retry ); false -> gleeth@rpc@client:make_request( gleeth@provider:rpc_url(P), gleeth@rpc@types:method_to_string(Method), Params ) end, fun(Body) -> decode_rpc_response( Body, {decoder, fun gleam@dynamic@decode:decode_string/1} ) end). -file("src/gleeth/rpc/response_utils.gleam", 87). -spec make_decoded_request( binary(), gleeth@rpc@types:eth_method(), list(gleam@json:json()), gleam@dynamic@decode:decoder(OFN) ) -> {ok, OFN} | {error, gleeth@rpc@types:gleeth_error()}. make_decoded_request(Rpc_url, Method, Params, Decoder) -> gleam@result:'try'( gleeth@rpc@client:make_request( Rpc_url, gleeth@rpc@types:method_to_string(Method), Params ), fun(Body) -> decode_rpc_response(Body, Decoder) end ). -file("src/gleeth/rpc/response_utils.gleam", 103). -spec make_decoded_request_with_provider( gleeth@provider:provider(), gleeth@rpc@types:eth_method(), list(gleam@json:json()), gleam@dynamic@decode:decoder(OFS) ) -> {ok, OFS} | {error, gleeth@rpc@types:gleeth_error()}. make_decoded_request_with_provider(P, Method, Params, Decoder) -> Retry = gleeth@provider:retry_config(P), gleam@result:'try'(case erlang:element(2, Retry) > 0 of true -> gleeth@rpc@client:make_request_with_retry( gleeth@provider:rpc_url(P), gleeth@rpc@types:method_to_string(Method), Params, Retry ); false -> gleeth@rpc@client:make_request( gleeth@provider:rpc_url(P), gleeth@rpc@types:method_to_string(Method), Params ) end, fun(Body) -> decode_rpc_response(Body, Decoder) end).