From f0fcf7ae8361c8964bdb4e75304c7ae598e9b7fd Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Thu, 14 Sep 2023 11:59:32 +0300 Subject: [PATCH] Handle 0 gas limit --- .../block_scout_web/views/api/v2/block_view.ex | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/views/api/v2/block_view.ex b/apps/block_scout_web/lib/block_scout_web/views/api/v2/block_view.ex index 8da05d3b68..c4793dbace 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/api/v2/block_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/api/v2/block_view.ex @@ -85,13 +85,21 @@ defmodule BlockScoutWeb.API.V2.BlockView do end def gas_target(block) do - elasticity_multiplier = Application.get_env(:explorer, :elasticity_multiplier) - ratio = Decimal.div(block.gas_used, Decimal.div(block.gas_limit, elasticity_multiplier)) - ratio |> Decimal.sub(1) |> Decimal.mult(100) |> Decimal.to_float() + if Decimal.compare(block.gas_limit, 0) == :gt do + elasticity_multiplier = Application.get_env(:explorer, :elasticity_multiplier) + ratio = Decimal.div(block.gas_used, Decimal.div(block.gas_limit, elasticity_multiplier)) + ratio |> Decimal.sub(1) |> Decimal.mult(100) |> Decimal.to_float() + else + Decimal.new(0) + end end def gas_used_percentage(block) do - block.gas_used |> Decimal.div(block.gas_limit) |> Decimal.mult(100) |> Decimal.to_float() + if Decimal.compare(block.gas_limit, 0) == :gt do + block.gas_used |> Decimal.div(block.gas_limit) |> Decimal.mult(100) |> Decimal.to_float() + else + Decimal.new(0) + end end def burnt_fees_percentage(_, %Decimal{coef: 0}), do: nil