Skip to content

Commit

Permalink
Update tx value fee and related test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
blackode committed Jun 9, 2022
1 parent 44c6c5d commit af729f1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 33 deletions.
22 changes: 1 addition & 21 deletions lib/archethic/mining/fee.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ defmodule Archethic.Mining.Fee do
alias Archethic.TransactionChain.TransactionData.NFTLedger
alias Archethic.TransactionChain.TransactionData.UCOLedger

@min_tx_fee_in_usd 0.1
@unit_uco 100_000_000

@doc """
Expand Down Expand Up @@ -43,15 +42,13 @@ defmodule Archethic.Mining.Fee do
0

true ->
transaction_value = 0.01 / uco_price_in_usd
nb_recipients = get_number_recipients(tx)
nb_bytes = get_transaction_size(tx)
nb_storage_nodes = get_number_replicas(tx)

trunc(
do_calculate(
uco_price_in_usd,
transaction_value,
nb_bytes,
nb_storage_nodes,
nb_recipients
Expand Down Expand Up @@ -89,14 +86,13 @@ defmodule Archethic.Mining.Fee do

defp do_calculate(
uco_price_in_usd,
transaction_value,
nb_bytes,
nb_storage_nodes,
nb_recipients
) do
# TODO: determine the fee for smart contract execution

value_cost = fee_for_value(uco_price_in_usd, transaction_value)
value_cost = 0.01 / uco_price_in_usd

storage_cost =
fee_for_storage(
Expand All @@ -110,22 +106,6 @@ defmodule Archethic.Mining.Fee do
value_cost + storage_cost + replication_cost
end

# if transaction value less than minimum transaction value => txn fee is minimum txn fee
defp fee_for_value(uco_price_in_usd, transaction_value_in_uco)
when transaction_value_in_uco <= @min_tx_fee_in_usd / uco_price_in_usd * 1000 do
get_min_transaction_fee(uco_price_in_usd)
end

defp fee_for_value(uco_price_in_usd, transaction_value_in_uco) do
min_tx_fee = get_min_transaction_fee(uco_price_in_usd)
min_tx_value = min_tx_fee * 1_000
min_tx_fee * (transaction_value_in_uco / min_tx_value)
end

defp get_min_transaction_fee(uco_price_in_usd) do
@min_tx_fee_in_usd / uco_price_in_usd
end

defp fee_for_storage(uco_price_in_usd, nb_bytes, nb_storage_nodes) do
price_per_byte = 1.0e-8 / uco_price_in_usd
price_per_storage_node = price_per_byte * nb_bytes
Expand Down
22 changes: 11 additions & 11 deletions test/archethic/mining/fee_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Archethic.Mining.FeeTest do

test "should return a fee less than amount to send for a single transfer" do
# 0.050048 UCO for 1 UCO at $0.2
assert 50_048_000 =
assert 5_048_000 =
%Transaction{
address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>,
type: :transfer,
Expand All @@ -45,7 +45,7 @@ defmodule Archethic.Mining.FeeTest do

test "should increase fee when the amount increases for single transfer " do
# 0.050048 UCO for 1 UCO
assert 5_004_800 ==
assert 5_04_800 ==
%Transaction{
address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>,
type: :transfer,
Expand All @@ -68,7 +68,7 @@ defmodule Archethic.Mining.FeeTest do
|> Fee.calculate(2.0)

# 0.060048 UCO for 60 UCO
assert 6_004_800 =
assert 504_800 =
%Transaction{
address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>,
type: :transfer,
Expand All @@ -93,7 +93,7 @@ defmodule Archethic.Mining.FeeTest do

test "should decrease the fee when the amount stays the same but the price of UCO increases" do
# 0.050048 UCO for 1 UCO at $ 2.0
assert 5_004_800 =
assert 504_800 =
%Transaction{
address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>,
type: :transfer,
Expand All @@ -116,7 +116,7 @@ defmodule Archethic.Mining.FeeTest do
|> Fee.calculate(2.0)

# 0.0100096 UCO for 1 UCO at $10.0
assert 1_000_960 =
assert 1_00_960 =
%Transaction{
address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>,
type: :transfer,
Expand All @@ -141,7 +141,7 @@ defmodule Archethic.Mining.FeeTest do

test "sending multiple transfers should cost more than sending a single big transfer" do
# 1.00048 UCO for 1_000 UCO
assert 100_048_000 =
assert 5_048_000 =
%Transaction{
address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>,
type: :transfer,
Expand All @@ -164,7 +164,7 @@ defmodule Archethic.Mining.FeeTest do
|> Fee.calculate(0.2)

# 501.1028775.UCO for 1000 transfer of 1 UCO
assert 50_110_287_750 =
assert 50_015_287_750 =
%Transaction{
address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>,
type: :transfer,
Expand All @@ -190,7 +190,7 @@ defmodule Archethic.Mining.FeeTest do

test "should increase the fee when the transaction size increases" do
# 0.5 UCO to store 1KB
assert 50_287_750 =
assert 5_287_749 =
%Transaction{
address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>,
type: :transfer,
Expand All @@ -204,7 +204,7 @@ defmodule Archethic.Mining.FeeTest do
|> Fee.calculate(0.2)

# 25.5 UCO to store 10MB
assert 2_550_037_750 =
assert 2_505_037_750 =
%Transaction{
address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>,
type: :transfer,
Expand All @@ -220,7 +220,7 @@ defmodule Archethic.Mining.FeeTest do

test "should cost more with more replication nodes" do
# 50 nodes: 0.050048 UCO
assert 5_004_800 =
assert 504_800 =
%Transaction{
address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>,
type: :transfer,
Expand All @@ -245,7 +245,7 @@ defmodule Archethic.Mining.FeeTest do
add_nodes(100)

# 150 nodes: 0.050144 UCO
assert 5_014_400 =
assert 5_14_400 =
%Transaction{
address: <<0::8, :crypto.strong_rand_bytes(32)::binary>>,
type: :transfer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule ArchethicWeb.API.TransactionControllerTest do
})

assert %{
"fee" => 0.50001325,
"fee" => 0.05001324,
"rates" => %{
"eur" => 0.2,
"usd" => 0.2
Expand Down

0 comments on commit af729f1

Please sign in to comment.