Merge pull request #4158 from blockscout/vb-calc-total-fee-per-day

Calculate total fee to validators per day
pull/4205/head
Victor Baranov 4 years ago committed by GitHub
commit 52f234ea4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 21
      apps/explorer/lib/explorer/chain/transaction/history/historian.ex
  3. 5
      apps/explorer/lib/explorer/chain/transaction/history/transaction_stats.ex
  4. 9
      apps/explorer/priv/repo/migrations/20210527093756_transaction_stats_add_total_fee_column.exs
  5. 32
      apps/explorer/test/explorer/chain/transaction/history/historian_test.exs

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

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

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

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

Loading…
Cancel
Save