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@commands@transaction.erl
Raw

src/gleeth@commands@transaction.erl

-module(gleeth@commands@transaction).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleeth/commands/transaction.gleam").
-export([execute/2]).
-file("src/gleeth/commands/transaction.gleam", 27).
-spec print_transaction(gleeth@ethereum@types:transaction()) -> nil.
print_transaction(Transaction) ->
gleam_stdlib:println(<<"Transaction Details:"/utf8>>),
gleam_stdlib:println(
<<" Hash: "/utf8, (erlang:element(2, Transaction))/binary>>
),
case erlang:element(3, Transaction) of
<<""/utf8>> ->
gleam_stdlib:println(<<" Status: Pending"/utf8>>);
Block_num ->
gleam_stdlib:println(
<<" Block: "/utf8,
(gleeth@ethereum@formatting:format_block_number(Block_num))/binary>>
),
case erlang:element(5, Transaction) of
<<""/utf8>> ->
nil;
Index ->
gleeth@ethereum@formatting:format_hex_with_decimal(
Index,
<<"Position"/utf8>>
)
end
end,
gleam_stdlib:println(
<<" From: "/utf8, (erlang:element(6, Transaction))/binary>>
),
case erlang:element(7, Transaction) of
<<""/utf8>> ->
gleam_stdlib:println(<<" To: [Contract Creation]"/utf8>>);
Address ->
gleam_stdlib:println(<<" To: "/utf8, Address/binary>>)
end,
gleam_stdlib:println(
<<" Value: "/utf8,
(gleeth@ethereum@formatting:format_wei_to_ether(
erlang:element(8, Transaction)
))/binary>>
),
gleeth@ethereum@formatting:format_hex_with_decimal(
erlang:element(9, Transaction),
<<"Gas Limit"/utf8>>
),
case {erlang:element(10, Transaction), erlang:element(11, Transaction)} of
{<<""/utf8>>, <<""/utf8>>} ->
nil;
{Gas_price, <<""/utf8>>} ->
gleam_stdlib:println(
<<" Gas Price: "/utf8,
(gleeth@utils@hex:format_wei_to_gwei(Gas_price))/binary>>
);
{<<""/utf8>>, Max_fee} ->
gleam_stdlib:println(
<<" Max Fee Per Gas: "/utf8,
(gleeth@utils@hex:format_wei_to_gwei(Max_fee))/binary>>
),
case erlang:element(12, Transaction) of
<<""/utf8>> ->
nil;
Priority_fee ->
gleam_stdlib:println(
<<" Max Priority Fee: "/utf8,
(gleeth@utils@hex:format_wei_to_gwei(Priority_fee))/binary>>
)
end;
{_, _} ->
gleam_stdlib:println(
<<" Gas Price: "/utf8,
(gleeth@utils@hex:format_wei_to_gwei(
erlang:element(10, Transaction)
))/binary>>
)
end,
gleeth@ethereum@formatting:format_hex_with_decimal(
erlang:element(14, Transaction),
<<"Nonce"/utf8>>
),
case erlang:element(15, Transaction) of
<<""/utf8>> ->
nil;
<<"0x0"/utf8>> ->
gleam_stdlib:println(<<" Type: Legacy"/utf8>>);
<<"0x1"/utf8>> ->
gleam_stdlib:println(<<" Type: EIP-2930 (Access List)"/utf8>>);
<<"0x2"/utf8>> ->
gleam_stdlib:println(<<" Type: EIP-1559 (Dynamic Fee)"/utf8>>);
Type_str ->
gleam_stdlib:println(<<" Type: "/utf8, Type_str/binary>>)
end,
case erlang:element(16, Transaction) of
<<""/utf8>> ->
nil;
Chain ->
gleam_stdlib:println(<<" Chain ID: "/utf8, Chain/binary>>)
end,
Input_preview = case erlang:element(13, Transaction) of
<<"0x"/utf8>> ->
<<"None"/utf8>>;
Input ->
Len = string:length(Input),
case Len > 42 of
true ->
<<<<<<(gleam@string:slice(Input, 0, 42))/binary,
"... ("/utf8>>/binary,
(erlang:integer_to_binary((Len - 2) div 2))/binary>>/binary,
" bytes)"/utf8>>;
false ->
<<<<<<Input/binary, " ("/utf8>>/binary,
(erlang:integer_to_binary((Len - 2) div 2))/binary>>/binary,
" bytes)"/utf8>>
end
end,
gleam_stdlib:println(<<" Input Data: "/utf8, Input_preview/binary>>),
gleam_stdlib:println(<<""/utf8>>),
gleam_stdlib:println(<<"Signature:"/utf8>>),
gleam_stdlib:println(
<<" v: "/utf8, (erlang:element(17, Transaction))/binary>>
),
gleam_stdlib:println(
<<" r: "/utf8, (erlang:element(18, Transaction))/binary>>
),
gleam_stdlib:println(
<<" s: "/utf8, (erlang:element(19, Transaction))/binary>>
).
-file("src/gleeth/commands/transaction.gleam", 14).
-spec execute(gleeth@provider:provider(), binary()) -> {ok, nil} |
{error, gleeth@rpc@types:gleeth_error()}.
execute(Provider, Transaction_hash) ->
gleam@result:'try'(
gleeth@rpc@methods:get_transaction(Provider, Transaction_hash),
fun(Transaction) ->
print_transaction(Transaction),
{ok, nil}
end
).