@ -92,11 +92,7 @@ defmodule Explorer.Chain.Cache.GasPriceOracle do
from_block_query = max ( from_block_acc , from_block_actual )
average_block_time =
case AverageBlockTime . average_block_time ( ) do
{ :error , _ } -> nil
average_block_time -> average_block_time |> Duration . to_milliseconds ( )
end
average_block_time = get_average_block_time ( )
fee_query =
from (
@ -186,12 +182,27 @@ defmodule Explorer.Chain.Cache.GasPriceOracle do
defp merge_gas_prices ( new , acc , from_block ) , do : Enum . take_while ( new ++ acc , & ( &1 . block_number > from_block ) )
defp process_fee_data_from_db ( [ ] ) do
case Block . next_block_base_fee_per_gas ( ) do
% Decimal { } = base_fee ->
base_fee_wei = base_fee |> Wei . from ( :wei )
exchange_rate = Market . get_coin_exchange_rate ( )
average_block_time = get_average_block_time ( )
%{
slow : compose_gas_price ( base_fee_wei , average_block_time , exchange_rate , base_fee_wei , 0 ) ,
average : compose_gas_price ( base_fee_wei , average_block_time , exchange_rate , base_fee_wei , 0 ) ,
fast : compose_gas_price ( base_fee_wei , average_block_time , exchange_rate , base_fee_wei , 0 )
}
_ ->
%{
slow : nil ,
average : nil ,
fast : nil
}
end
end
defp process_fee_data_from_db ( fees ) do
%{
@ -223,13 +234,12 @@ defmodule Explorer.Chain.Cache.GasPriceOracle do
{ gas_price ( slow_gas_price ) , gas_price ( average_gas_price ) , gas_price ( fast_gas_price ) , nil }
end
exchange_rate_from_db = Market . get_coin_exchange_rate ( )
exchange_rate = Market . get_coin_exchange_rate ( )
%{
slow : compose_gas_price ( slow_fee , slow_time , exchange_rate_from_db , base_fee_wei , slow_priority_fee_per_gas ) ,
average :
compose_gas_price ( average_fee , average_time , exchange_rate_from_db , base_fee_wei , average_priority_fee_per_gas ) ,
fast : compose_gas_price ( fast_fee , fast_time , exchange_rate_from_db , base_fee_wei , fast_priority_fee_per_gas )
slow : compose_gas_price ( slow_fee , slow_time , exchange_rate , base_fee_wei , slow_priority_fee_per_gas ) ,
average : compose_gas_price ( average_fee , average_time , exchange_rate , base_fee_wei , average_priority_fee_per_gas ) ,
fast : compose_gas_price ( fast_fee , fast_time , exchange_rate , base_fee_wei , fast_priority_fee_per_gas )
}
end
@ -251,15 +261,27 @@ defmodule Explorer.Chain.Cache.GasPriceOracle do
{ key , value } ->
value = if is_list ( value ) , do : value , else : [ value ]
count = Enum . count ( value )
{ key , value |> Enum . reduce ( Decimal . new ( 0 ) , & Decimal . add / 2 ) |> Decimal . div ( count ) }
value =
value
|> Enum . reduce ( Decimal . new ( 0 ) , fn
fee , sum when is_float ( fee ) ->
fee |> Decimal . from_float ( ) |> Decimal . add ( sum )
fee , sum ->
Decimal . add ( sum , fee )
end )
|> Decimal . div ( count )
{ key , value }
end )
end
defp compose_gas_price ( fee , time , exchange_rate_from_db , base_fee , priority_fee ) do
defp compose_gas_price ( fee , time , exchange_rate , base_fee , priority_fee ) do
%{
price : fee |> format_wei ( ) ,
time : time && time |> Decimal . to_float ( ) ,
fiat_price : fiat_fee ( fee , exchange_rate_from_db ) ,
fiat_price : fiat_fee ( fee , exchange_rate ) ,
base_fee : base_fee |> format_wei ( ) ,
priority_fee : base_fee && priority_fee && priority_fee |> Decimal . new ( ) |> Wei . from ( :wei ) |> format_wei ( ) ,
priority_fee_wei : base_fee && priority_fee && priority_fee |> Decimal . new ( ) |> Decimal . round ( ) ,
@ -358,4 +380,11 @@ defmodule Explorer.Chain.Cache.GasPriceOracle do
end
defp async_task_on_deletion ( _data ) , do : nil
defp get_average_block_time do
case AverageBlockTime . average_block_time ( ) do
{ :error , :disabled } -> nil
average_block_time -> average_block_time |> Duration . to_milliseconds ( )
end
end
end