Created
January 8, 2022 18:37
-
-
Save Nickforall/58d2feb68e5d1c4262b7022685a5fb3f to your computer and use it in GitHub Desktop.
Aws Managed Blockchain node for ethereumex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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