-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", 46). ?DOC( " Estimate gas for a legacy transaction by fetching gas price and gas limit.\n" "\n" " Combines `eth_gasPrice` and `eth_estimateGas` into a single call.\n" " The returned `LegacyGasEstimate` has `gas_price` and `gas_limit` as hex\n" " strings, ready to pass to `transaction.create_legacy_transaction`.\n" "\n" " ## Examples\n" "\n" " ```gleam\n" " let sender = wallet.get_address(w)\n" " let to = \"0x70997970C51812dc3A010C7d01b50e0d17dc79C8\"\n" "\n" " let assert Ok(est) = gas.estimate_legacy(provider, sender, to, \"0xde0b6b3a7640000\", \"0x\")\n" "\n" " // Use the estimate in a transaction\n" " let assert Ok(tx) = transaction.create_legacy_transaction(\n" " to, \"0xde0b6b3a7640000\", est.gas_limit, est.gas_price, nonce, \"0x\", 1,\n" " )\n" " ```\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", 83). ?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" "\n" " ## Examples\n" "\n" " ```gleam\n" " let sender = wallet.get_address(w)\n" " let to = \"0x70997970C51812dc3A010C7d01b50e0d17dc79C8\"\n" "\n" " let assert Ok(est) = gas.estimate_eip1559(provider, sender, to, \"0xde0b6b3a7640000\", \"0x\")\n" "\n" " // Use the estimate in a Type 2 transaction\n" " let assert Ok(tx) = transaction.create_eip1559_transaction(\n" " to, \"0xde0b6b3a7640000\",\n" " est.gas_limit, est.max_fee_per_gas, est.max_priority_fee_per_gas,\n" " nonce, \"0x\", 1,\n" " )\n" " ```\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 ).