Current section
Files
Jump to
Current section
Files
src/gleeth@commands@estimate_gas.erl
-module(gleeth@commands@estimate_gas).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleeth/commands/estimate_gas.gleam").
-export([execute/5]).
-file("src/gleeth/commands/estimate_gas.gleam", 102).
-spec print_cost_estimates(integer()) -> nil.
print_cost_estimates(Gas_units) ->
gleam_stdlib:println(<<""/utf8>>),
gleam_stdlib:println(<<"Cost Estimates:"/utf8>>),
Gas_prices = [{10, <<"10 gwei (slow)"/utf8>>},
{20, <<"20 gwei (standard)"/utf8>>},
{50, <<"50 gwei (fast)"/utf8>>},
{100, <<"100 gwei (very fast)"/utf8>>}],
gleam@list:each(
Gas_prices,
fun(Price_info) ->
{Gwei, Label} = Price_info,
Wei_cost = (Gas_units * Gwei) * 1000000000,
Eth_cost = erlang:float(Wei_cost) / 1000000000000000000.0,
gleam_stdlib:println(
<<<<<<<<" "/utf8, Label/binary>>/binary, ": "/utf8>>/binary,
(gleam_stdlib:float_to_string(Eth_cost))/binary>>/binary,
" ETH"/utf8>>
)
end
).
-file("src/gleeth/commands/estimate_gas.gleam", 32).
-spec print_gas_estimate(binary(), binary(), binary(), binary(), binary()) -> nil.
print_gas_estimate(From, To, Value, Data, Gas_estimate) ->
gleam_stdlib:println(<<"Gas Estimation:"/utf8>>),
case From of
<<""/utf8>> ->
nil;
_ ->
gleam_stdlib:println(<<" From: "/utf8, From/binary>>)
end,
case To of
<<""/utf8>> ->
nil;
_ ->
gleam_stdlib:println(<<" To: "/utf8, To/binary>>)
end,
case Value of
<<""/utf8>> ->
nil;
_ ->
case gleeth@utils@hex:hex_to_int(Value) of
{ok, Decimal_wei} ->
gleam_stdlib:println(
<<<<<<<<" Value: "/utf8,
(erlang:integer_to_binary(Decimal_wei))/binary>>/binary,
" wei ("/utf8>>/binary,
Value/binary>>/binary,
")"/utf8>>
);
{error, _} ->
gleam_stdlib:println(<<" Value: "/utf8, Value/binary>>)
end
end,
case Data of
<<""/utf8>> ->
nil;
_ ->
Data_preview = case string:length(Data) > 42 of
true ->
<<(gleam@string:slice(Data, 0, 42))/binary, "..."/utf8>>;
false ->
Data
end,
gleam_stdlib:println(<<" Data: "/utf8, Data_preview/binary>>)
end,
gleam_stdlib:println(<<""/utf8>>),
gleam_stdlib:println(<<"Estimated Gas: "/utf8, Gas_estimate/binary>>),
case gleeth@utils@hex:hex_to_int(Gas_estimate) of
{ok, Decimal_gas} ->
gleam_stdlib:println(
<<<<"Estimated Gas (decimal): "/utf8,
(erlang:integer_to_binary(Decimal_gas))/binary>>/binary,
" units"/utf8>>
),
print_cost_estimates(Decimal_gas);
{error, _} ->
gleam_stdlib:println(
<<"(Could not convert gas estimate to decimal)"/utf8>>
)
end,
gleam_stdlib:println(<<""/utf8>>).
-file("src/gleeth/commands/estimate_gas.gleam", 13).
-spec execute(
gleeth@provider:provider(),
binary(),
binary(),
binary(),
binary()
) -> {ok, nil} | {error, gleeth@rpc@types:gleeth_error()}.
execute(Provider, From, To, Value, Data) ->
gleam@result:'try'(
gleeth@rpc@methods:estimate_gas(Provider, From, To, Value, Data),
fun(Gas_estimate) ->
print_gas_estimate(From, To, Value, Data, Gas_estimate),
{ok, nil}
end
).