From d2e88a4ecc2aa63bcf1601e8d06cde7340f630ef Mon Sep 17 00:00:00 2001 From: Samuel Date: Mon, 20 Jun 2022 20:20:38 +0200 Subject: [PATCH] Add NFT transaction additional fee --- lib/archethic/mining/fee.ex | 67 +++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/lib/archethic/mining/fee.ex b/lib/archethic/mining/fee.ex index ae6e3dc44..c8d6760aa 100644 --- a/lib/archethic/mining/fee.ex +++ b/lib/archethic/mining/fee.ex @@ -46,17 +46,50 @@ defmodule Archethic.Mining.Fee do nb_bytes = get_transaction_size(tx) nb_storage_nodes = get_number_replicas(tx) - trunc( - do_calculate( + # TODO: determine the fee for smart contract execution + + storage_cost = + fee_for_storage( uco_price_in_usd, nb_bytes, - nb_storage_nodes, - nb_recipients - ) * @unit_uco - ) + nb_storage_nodes + ) + + replication_cost = cost_per_recipients(nb_recipients, uco_price_in_usd) + + fee = + minimum_fee(uco_price_in_usd) + storage_cost + replication_cost + + get_additional_fee(tx, uco_price_in_usd) + + trunc(fee * @unit_uco) + end + end + + defp get_additional_fee( + %Transaction{type: :nft, data: %TransactionData{content: content}}, + uco_price_in_usd + ) do + with {:ok, json} <- Jason.decode(content), + "non-fungible" <- Map.get(json, "type", "fungible"), + utxos = [_ | _] <- Map.get(json, "properties", []) do + nb_utxos = length(utxos) + base_fee = minimum_fee(uco_price_in_usd) + (:math.log10(nb_utxos) + 1) * nb_utxos * base_fee + else + {:error, _} -> + 0 + + "fungible" -> + 1 * minimum_fee(uco_price_in_usd) + + [] -> + # Invalid non-fungible definition + 0 end end + defp get_additional_fee(_tx, _uco_price_usd), do: 0 + defp get_transaction_size(tx = %Transaction{}) do tx |> Transaction.to_pending() @@ -84,26 +117,8 @@ defmodule Archethic.Mining.Fee do |> length() end - defp do_calculate( - uco_price_in_usd, - nb_bytes, - nb_storage_nodes, - nb_recipients - ) do - # TODO: determine the fee for smart contract execution - - minimum_fee = 0.01 / uco_price_in_usd - - storage_cost = - fee_for_storage( - uco_price_in_usd, - nb_bytes, - nb_storage_nodes - ) - - replication_cost = cost_per_recipients(nb_recipients, uco_price_in_usd) - - minimum_fee + storage_cost + replication_cost + defp minimum_fee(uco_price_in_usd) do + 0.01 / uco_price_in_usd end defp fee_for_storage(uco_price_in_usd, nb_bytes, nb_storage_nodes) do