Skip to content

Instantly share code, notes, and snippets.

@Nickforall
Created January 8, 2022 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nickforall/58d2feb68e5d1c4262b7022685a5fb3f to your computer and use it in GitHub Desktop.
Save Nickforall/58d2feb68e5d1c4262b7022685a5fb3f to your computer and use it in GitHub Desktop.
Aws Managed Blockchain node for ethereumex
defmodule MyProject.Ethereum.AwsNode do
use Ethereumex.Client.BaseClient
alias Ethereumex.Config
@type opt :: {:url, String.t()}
@type empty_response :: :empty_response
@type invalid_json :: {:invalid_json, any()}
@type http_client_error :: {:error, empty_response() | invalid_json() | any()}
@spec post_request(binary(), [opt()]) :: {:ok, any()} | http_client_error()
def post_request(payload, opts) do
ExAws.Request.request(
:post,
Config.rpc_url(),
payload,
[],
ExAws.Config.new(:s3),
:managedblockchain
)
|> case do
{:ok, %{body: body, status_code: code}} ->
decode_body(body, code)
{:error, error} ->
{:error, error}
end
end
@spec decode_body(binary(), non_neg_integer()) :: {:ok, any()} | http_client_error()
defp decode_body(body, code) do
case Jason.decode(body) do
{:ok, decoded_body} ->
case {code, decoded_body} do
{200, %{"error" => error}} -> {:error, error}
{200, result = [%{} | _]} -> {:ok, format_batch(result)}
{200, %{"result" => result}} -> {:ok, result}
_ -> {:error, decoded_body}
end
{:error, %Jason.DecodeError{data: ""}} ->
{:error, :empty_response}
{:error, error} ->
{:error, {:invalid_json, error}}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment