-module(gleeth@gas). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/gleeth/gas.gleam"). -export([estimate_legacy/5, estimate_eip1559/5]). -export_type([legacy_gas_estimate/0, eip1559_gas_estimate/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC( " Gas estimation helpers for legacy and EIP-1559 transactions.\n" "\n" " These functions combine multiple RPC calls to produce a complete\n" " gas estimate that can be passed directly to a transaction builder.\n" ). -type legacy_gas_estimate() :: {legacy_gas_estimate, binary(), binary()}. -type eip1559_gas_estimate() :: {eip1559_gas_estimate, binary(), binary(), binary()}. -file("src/gleeth/gas.gleam", 28). ?DOC(" Estimate gas for a legacy transaction by fetching gas price and gas limit.\n"). -spec estimate_legacy( gleeth@provider:provider(), binary(), binary(), binary(), binary() ) -> {ok, legacy_gas_estimate()} | {error, gleeth@rpc@types:gleeth_error()}. estimate_legacy(Provider, From, To, Value, Data) -> gleam@result:'try'( gleeth@rpc@methods:get_gas_price(Provider), fun(Gas_price) -> gleam@result:'try'( gleeth@rpc@methods:estimate_gas(Provider, From, To, Value, Data), fun(Gas_limit) -> {ok, {legacy_gas_estimate, Gas_price, Gas_limit}} end ) end ). -file("src/gleeth/gas.gleam", 49). ?DOC( " Estimate gas for an EIP-1559 transaction by fetching priority fee,\n" " base fee from fee history, and gas limit.\n" " Computes max_fee = 2 * base_fee + priority_fee.\n" ). -spec estimate_eip1559( gleeth@provider:provider(), binary(), binary(), binary(), binary() ) -> {ok, eip1559_gas_estimate()} | {error, gleeth@rpc@types:gleeth_error()}. estimate_eip1559(Provider, From, To, Value, Data) -> gleam@result:'try'( gleeth@rpc@methods:get_max_priority_fee(Provider), fun(Priority_fee) -> gleam@result:'try'( gleeth@rpc@methods:get_fee_history( Provider, 1, <<"latest"/utf8>>, [] ), fun(Fee_history) -> gleam@result:'try'( gleeth@rpc@methods:estimate_gas( Provider, From, To, Value, Data ), fun(Gas_limit) -> gleam@result:'try'( begin _pipe = gleam@list:last( erlang:element(3, Fee_history) ), gleam@result:map_error( _pipe, fun(_) -> {parse_error, <<"Fee history returned empty base_fee_per_gas"/utf8>>} end ) end, fun(Base_fee_hex) -> gleam@result:'try'( begin _pipe@1 = gleeth@utils@hex:to_int( Base_fee_hex ), gleam@result:map_error( _pipe@1, fun(_) -> {parse_error, <<"Failed to parse base fee hex: "/utf8, Base_fee_hex/binary>>} end ) end, fun(Base_fee_int) -> gleam@result:'try'( begin _pipe@2 = gleeth@utils@hex:to_int( Priority_fee ), gleam@result:map_error( _pipe@2, fun(_) -> {parse_error, <<"Failed to parse priority fee hex: "/utf8, Priority_fee/binary>>} end ) end, fun(Priority_fee_int) -> Max_fee_int = (2 * Base_fee_int) + Priority_fee_int, Max_fee_hex = gleeth@utils@hex:from_int( Max_fee_int ), {ok, {eip1559_gas_estimate, Max_fee_hex, Priority_fee, Gas_limit}} end ) end ) end ) end ) end ) end ).