Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change transaction value fee to a fixed one (base fee) #368

Merged
6 commits merged into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 1 addition & 27 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 = get_transaction_value(tx) / @unit_uco
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 All @@ -60,12 +57,6 @@ defmodule Archethic.Mining.Fee do
end
end

defp get_transaction_value(%Transaction{
data: %TransactionData{ledger: %Ledger{uco: %UCOLedger{transfers: uco_transfers}}}
}) do
Enum.reduce(uco_transfers, 0, &(&1.amount + &2))
end

defp get_transaction_size(tx = %Transaction{}) do
tx
|> Transaction.to_pending()
Expand Down Expand Up @@ -95,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
blackode marked this conversation as resolved.
Show resolved Hide resolved

storage_cost =
fee_for_storage(
Expand All @@ -116,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 =
blackode marked this conversation as resolved.
Show resolved Hide resolved
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 504_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 100_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 514_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