From 518fc231171c83fc793604d8c2dc2b4c72974986 Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Thu, 27 May 2021 13:16:30 +0300 Subject: [PATCH 1/2] Calc total fee per day --- .../chain/transaction/history/historian.ex | 21 ++++++++++-- .../transaction/history/transaction_stats.ex | 5 ++- ...transaction_stats_add_total_fee_column.exs | 9 ++++++ .../transaction/history/historian_test.exs | 32 +++++++++++++------ 4 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 apps/explorer/priv/repo/migrations/20210527093756_transaction_stats_add_total_fee_column.exs diff --git a/apps/explorer/lib/explorer/chain/transaction/history/historian.ex b/apps/explorer/lib/explorer/chain/transaction/history/historian.ex index 5d70143760..18a7e32b7d 100644 --- a/apps/explorer/lib/explorer/chain/transaction/history/historian.ex +++ b/apps/explorer/lib/explorer/chain/transaction/history/historian.ex @@ -59,16 +59,31 @@ defmodule Explorer.Chain.Transaction.History.Historian do join: block in Block, on: transaction.block_hash == block.hash, where: block.consensus == true, - select: transaction.hash + select: transaction ) num_transactions = Repo.aggregate(query, :count, :hash, timeout: :infinity) gas_used = Repo.aggregate(query, :sum, :gas_used, timeout: :infinity) - records = [%{date: day_to_fetch, number_of_transactions: num_transactions, gas_used: gas_used} | records] + total_fee_query = + from(transaction in subquery(all_transactions_query), + join: block in Block, + on: transaction.block_hash == block.hash, + where: block.consensus == true, + where: transaction.status == ^1, + select: fragment("SUM(? * ?)", transaction.gas_price, transaction.gas_used) + ) + + total_fee = Repo.one(total_fee_query, timeout: :infinity) + + records = [ + %{date: day_to_fetch, number_of_transactions: num_transactions, gas_used: gas_used, total_fee: total_fee} + | records + ] + compile_records(num_days - 1, records) else - records = [%{date: day_to_fetch, number_of_transactions: 0} | records] + records = [%{date: day_to_fetch, number_of_transactions: 0, gas_used: 0, total_fee: 0} | records] compile_records(num_days - 1, records) end end diff --git a/apps/explorer/lib/explorer/chain/transaction/history/transaction_stats.ex b/apps/explorer/lib/explorer/chain/transaction/history/transaction_stats.ex index c74ea1c526..5e5f10b9ce 100644 --- a/apps/explorer/lib/explorer/chain/transaction/history/transaction_stats.ex +++ b/apps/explorer/lib/explorer/chain/transaction/history/transaction_stats.ex @@ -18,6 +18,7 @@ defmodule Explorer.Chain.Transaction.History.TransactionStats do field(:date, :date) field(:number_of_transactions, :integer) field(:gas_used, :decimal) + field(:total_fee, :decimal) end @typedoc """ @@ -25,11 +26,13 @@ defmodule Explorer.Chain.Transaction.History.TransactionStats do * `:date` - The date in UTC. * `:number_of_transactions` - Number of transactions processed by the vm for a given date. * `:gas_used` - Gas used in transactions per single day + * `:total_fee` - Total fee paid to validators from success transactions per single day """ @type t :: %__MODULE__{ date: Date.t(), number_of_transactions: integer(), - gas_used: non_neg_integer() + gas_used: non_neg_integer(), + total_fee: non_neg_integer() } @spec by_date_range(Date.t(), Date.t()) :: [__MODULE__] diff --git a/apps/explorer/priv/repo/migrations/20210527093756_transaction_stats_add_total_fee_column.exs b/apps/explorer/priv/repo/migrations/20210527093756_transaction_stats_add_total_fee_column.exs new file mode 100644 index 0000000000..3684c20ad1 --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20210527093756_transaction_stats_add_total_fee_column.exs @@ -0,0 +1,9 @@ +defmodule Explorer.Repo.Migrations.TransactionStatsAddTotalFeeColumn do + use Ecto.Migration + + def change do + alter table(:transaction_stats) do + add(:total_fee, :numeric, precision: 100, null: true) + end + end +end diff --git a/apps/explorer/test/explorer/chain/transaction/history/historian_test.exs b/apps/explorer/test/explorer/chain/transaction/history/historian_test.exs index 379852c30b..39c4995325 100644 --- a/apps/explorer/test/explorer/chain/transaction/history/historian_test.exs +++ b/apps/explorer/test/explorer/chain/transaction/history/historian_test.exs @@ -16,7 +16,7 @@ defmodule Explorer.Chain.Transaction.History.HistorianTest do end describe "compile_records/1" do - test "fetches transactions from blocks mined in the past num_days" do + test "fetches transactions, total gas, total fee from blocks mined in the past num_days" do blocks = [ # 1970-01-03 00:00:60 insert(:block, timestamp: DateTime.from_unix!(days_to_secs(2) + 60)), @@ -28,29 +28,41 @@ defmodule Explorer.Chain.Transaction.History.HistorianTest do insert(:block, timestamp: DateTime.from_unix!(days_to_secs(1))) ] - transaction_1 = insert(:transaction) |> with_block(Enum.at(blocks, 0)) - transaction_2 = insert(:transaction) |> with_block(Enum.at(blocks, 1)) - transaction_3 = insert(:transaction) |> with_block(Enum.at(blocks, 2)) + transaction_1 = insert(:transaction) |> with_block(Enum.at(blocks, 0), status: :ok) + transaction_2 = insert(:transaction) |> with_block(Enum.at(blocks, 1), status: :ok) + transaction_3 = insert(:transaction) |> with_block(Enum.at(blocks, 2), status: :ok) expected = [ - %{date: ~D[1970-01-04], number_of_transactions: 0} + %{date: ~D[1970-01-04], number_of_transactions: 0, gas_used: 0, total_fee: 0} ] assert {:ok, ^expected} = Historian.compile_records(1) total_gas_used_1 = Decimal.add(transaction_1.gas_used, transaction_2.gas_used) + %Explorer.Chain.Wei{value: transaction_1_gas_price_value} = transaction_1.gas_price + %Explorer.Chain.Wei{value: transaction_2_gas_price_value} = transaction_2.gas_price + %Explorer.Chain.Wei{value: transaction_3_gas_price_value} = transaction_3.gas_price + + total_fee_1 = + Decimal.add( + Decimal.mult(transaction_1.gas_used, transaction_1_gas_price_value), + Decimal.mult(transaction_2.gas_used, transaction_2_gas_price_value) + ) + + total_fee_3 = Decimal.mult(transaction_3.gas_used, transaction_3_gas_price_value) + expected = [ - %{date: ~D[1970-01-04], number_of_transactions: 0}, - %{date: ~D[1970-01-03], gas_used: total_gas_used_1, number_of_transactions: 2} + %{date: ~D[1970-01-04], number_of_transactions: 0, gas_used: 0, total_fee: 0}, + %{date: ~D[1970-01-03], gas_used: total_gas_used_1, number_of_transactions: 2, total_fee: total_fee_1} ] assert {:ok, ^expected} = Historian.compile_records(2) expected = [ - %{date: ~D[1970-01-04], number_of_transactions: 0}, - %{date: ~D[1970-01-03], gas_used: total_gas_used_1, number_of_transactions: 2}, - %{date: ~D[1970-01-02], gas_used: transaction_3.gas_used, number_of_transactions: 1} + %{date: ~D[1970-01-04], number_of_transactions: 0, gas_used: 0, total_fee: 0}, + %{date: ~D[1970-01-03], gas_used: total_gas_used_1, number_of_transactions: 2, total_fee: total_fee_1}, + %{date: ~D[1970-01-02], gas_used: transaction_3.gas_used, number_of_transactions: 1, total_fee: total_fee_3} ] assert {:ok, ^expected} = Historian.compile_records(3) From fb35b5d44bf9dd597cb8acd8443491171e5d5a5b Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Thu, 27 May 2021 17:02:06 +0300 Subject: [PATCH 2/2] Add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d4eb80843..37513b419f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Current ### Features +- [#4158](https://github.com/blockscout/blockscout/pull/4158) - Calculate total fee per day - [#4067](https://github.com/blockscout/blockscout/pull/4067) - Display LP tokens USD value and custom metadata in tokens dropdown at address page ### Fixes