Calc total fee per day

pull/4158/head
Viktor Baranov 4 years ago
parent 5397b97faf
commit 518fc23117
  1. 21
      apps/explorer/lib/explorer/chain/transaction/history/historian.ex
  2. 5
      apps/explorer/lib/explorer/chain/transaction/history/transaction_stats.ex
  3. 9
      apps/explorer/priv/repo/migrations/20210527093756_transaction_stats_add_total_fee_column.exs
  4. 32
      apps/explorer/test/explorer/chain/transaction/history/historian_test.exs

@ -59,16 +59,31 @@ defmodule Explorer.Chain.Transaction.History.Historian do
join: block in Block, join: block in Block,
on: transaction.block_hash == block.hash, on: transaction.block_hash == block.hash,
where: block.consensus == true, where: block.consensus == true,
select: transaction.hash select: transaction
) )
num_transactions = Repo.aggregate(query, :count, :hash, timeout: :infinity) num_transactions = Repo.aggregate(query, :count, :hash, timeout: :infinity)
gas_used = Repo.aggregate(query, :sum, :gas_used, 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) compile_records(num_days - 1, records)
else 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) compile_records(num_days - 1, records)
end end
end end

@ -18,6 +18,7 @@ defmodule Explorer.Chain.Transaction.History.TransactionStats do
field(:date, :date) field(:date, :date)
field(:number_of_transactions, :integer) field(:number_of_transactions, :integer)
field(:gas_used, :decimal) field(:gas_used, :decimal)
field(:total_fee, :decimal)
end end
@typedoc """ @typedoc """
@ -25,11 +26,13 @@ defmodule Explorer.Chain.Transaction.History.TransactionStats do
* `:date` - The date in UTC. * `:date` - The date in UTC.
* `:number_of_transactions` - Number of transactions processed by the vm for a given date. * `:number_of_transactions` - Number of transactions processed by the vm for a given date.
* `:gas_used` - Gas used in transactions per single day * `: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__{ @type t :: %__MODULE__{
date: Date.t(), date: Date.t(),
number_of_transactions: integer(), 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__] @spec by_date_range(Date.t(), Date.t()) :: [__MODULE__]

@ -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

@ -16,7 +16,7 @@ defmodule Explorer.Chain.Transaction.History.HistorianTest do
end end
describe "compile_records/1" do 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 = [ blocks = [
# 1970-01-03 00:00:60 # 1970-01-03 00:00:60
insert(:block, timestamp: DateTime.from_unix!(days_to_secs(2) + 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))) insert(:block, timestamp: DateTime.from_unix!(days_to_secs(1)))
] ]
transaction_1 = insert(:transaction) |> with_block(Enum.at(blocks, 0)) transaction_1 = insert(:transaction) |> with_block(Enum.at(blocks, 0), status: :ok)
transaction_2 = insert(:transaction) |> with_block(Enum.at(blocks, 1)) transaction_2 = insert(:transaction) |> with_block(Enum.at(blocks, 1), status: :ok)
transaction_3 = insert(:transaction) |> with_block(Enum.at(blocks, 2)) transaction_3 = insert(:transaction) |> with_block(Enum.at(blocks, 2), status: :ok)
expected = [ 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) assert {:ok, ^expected} = Historian.compile_records(1)
total_gas_used_1 = Decimal.add(transaction_1.gas_used, transaction_2.gas_used) 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 = [ 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},
%{date: ~D[1970-01-03], gas_used: total_gas_used_1, number_of_transactions: 2} %{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) assert {:ok, ^expected} = Historian.compile_records(2)
expected = [ 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},
%{date: ~D[1970-01-03], gas_used: total_gas_used_1, number_of_transactions: 2}, %{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} %{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) assert {:ok, ^expected} = Historian.compile_records(3)

Loading…
Cancel
Save