Skip to content

Commit

Permalink
fix(fee): add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Oct 10, 2024
1 parent 723c7c8 commit 7cb5c64
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
20 changes: 13 additions & 7 deletions lib/fee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,27 @@ defmodule Tezex.Fee do
@spec calculate_fee(map(), pos_integer(),
extra_size: pos_integer(),
minimal_nanotez_per_gas_unit: pos_integer()
) :: pos_integer()
) :: {:ok, pos_integer()} | {:error, nonempty_binary()}
def calculate_fee(content, consumed_gas, opts \\ []) do
extra_size = Keyword.get(opts, :extra_size, @extra_size)

minimal_nanotez_per_gas_unit =
Keyword.get(opts, :minimal_nanotez_per_gas_unit, @minimal_nanotez_per_gas_unit)

{:ok, forged_content} = ForgeOperation.operation(content)
size = byte_size(forged_content) + extra_size
case ForgeOperation.operation(content) do
{:ok, forged_content} ->
size = byte_size(forged_content) + extra_size

fee =
@minimal_fees + @minimal_mutez_per_byte * size +
div(minimal_nanotez_per_gas_unit * consumed_gas, 1000)
fee =
@minimal_fees + @minimal_mutez_per_byte * size +
div(minimal_nanotez_per_gas_unit * consumed_gas, 1000)

fee + @reserve
fees = fee + @reserve
{:ok, fees}

err ->
err
end
end

# Voir pour utiliser hard_gas_limit_per_content aussi ?
Expand Down
32 changes: 22 additions & 10 deletions lib/rpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,32 @@ defmodule Tezex.Rpc do
end

extra_size = 1 + div(Fee.extra_size(), number_contents)
fee = Fee.calculate_fee(content, gas_limit_new, extra_size: extra_size)

%{
content
| "gas_limit" => Integer.to_string(gas_limit_new),
"storage_limit" => Integer.to_string(storage_limit_new),
"fee" => Integer.to_string(fee)
}

case Fee.calculate_fee(content, gas_limit_new, extra_size: extra_size) do
{:ok, fee} ->
{:ok,
%{
content
| "gas_limit" => Integer.to_string(gas_limit_new),
"storage_limit" => Integer.to_string(storage_limit_new),
"fee" => Integer.to_string(fee)
}}

err ->
err
end
else
content
{:ok, content}
end
end)

%{operation | "contents" => contents}
first_error = Enum.find(contents, &(elem(&1, 0) == :error))

if is_nil(first_error) do
%{operation | "contents" => Enum.map(contents, &elem(&1, 1))}
else
first_error
end
end

defp get_preapplied_operation_values(op, value_fun) do
Expand Down

0 comments on commit 7cb5c64

Please sign in to comment.