feat: Add Fee column to Internal transactions CSV export (#10204)

* feat: Add Fee column to Internal transactions CSV export

* Fix tests
pull/10203/head
nikitosing 6 months ago committed by GitHub
parent d9586c233f
commit 3e77a354ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 16
      apps/explorer/lib/explorer/chain/csv_export/address_internal_transaction_csv_exporter.ex
  2. 21
      apps/explorer/lib/explorer/chain/transaction.ex
  3. 12
      apps/explorer/test/explorer/chain/csv_export/address_internal_transaction_csv_exporter_test.exs

@ -4,7 +4,7 @@ defmodule Explorer.Chain.CSVExport.AddressInternalTransactionCsvExporter do
"""
alias Explorer.{Chain, PagingOptions}
alias Explorer.Chain.{Address, Hash, Wei}
alias Explorer.Chain.{Address, Hash, Transaction, Wei}
alias Explorer.Chain.CSVExport.Helper
@paging_options %PagingOptions{page_size: Helper.limit()}
@ -34,6 +34,9 @@ defmodule Explorer.Chain.CSVExport.AddressInternalTransactionCsvExporter do
|> Keyword.put(:paging_options, paging_options)
|> Keyword.put(:from_block, from_block)
|> Keyword.put(:to_block, to_block)
|> Keyword.put(:necessity_by_association, %{
:transaction => :optional
})
|> (&if(Helper.valid_filter?(filter_type, filter_value, "internal_transactions"),
do: &1 |> Keyword.put(:direction, String.to_atom(filter_value)),
else: &1
@ -61,12 +64,18 @@ defmodule Explorer.Chain.CSVExport.AddressInternalTransactionCsvExporter do
"Value",
"Input",
"Output",
"ErrCode"
"ErrCode",
"Fee"
]
internal_transaction_lists =
internal_transactions
|> Stream.map(fn internal_transaction ->
gas_price =
internal_transaction.transaction &&
(internal_transaction.transaction.gas_price ||
Transaction.effective_gas_price(internal_transaction.transaction, internal_transaction.block))
[
to_string(internal_transaction.transaction_hash),
internal_transaction.index,
@ -85,7 +94,8 @@ defmodule Explorer.Chain.CSVExport.AddressInternalTransactionCsvExporter do
Wei.to(internal_transaction.value, :wei),
internal_transaction.input,
internal_transaction.output,
internal_transaction.error
internal_transaction.error,
gas_price && gas_price |> Wei.mult(internal_transaction.gas_used) |> Wei.to(:wei)
]
end)

@ -266,6 +266,7 @@ defmodule Explorer.Chain.Transaction do
alias Explorer.{Chain, PagingOptions, Repo, SortingHelper}
alias Explorer.Chain.{
Block,
Block.Reward,
ContractMethod,
Data,
@ -1795,17 +1796,23 @@ defmodule Explorer.Chain.Transaction do
end
@doc """
Calculates effective gas price for transaction with type 2 (EIP-1559)
`effective_gas_price = priority_fee_per_gas + block.base_fee_per_gas`
Wrapper around `effective_gas_price/2`
"""
@spec effective_gas_price(Transaction.t()) :: Wei.t() | nil
def effective_gas_price(%Transaction{} = transaction), do: effective_gas_price(transaction, transaction.block)
@doc """
Calculates effective gas price for transaction with type 2 (EIP-1559)
`effective_gas_price = priority_fee_per_gas + block.base_fee_per_gas`
"""
@spec effective_gas_price(Transaction.t(), Block.t()) :: Wei.t() | nil
def effective_gas_price(%Transaction{block: nil}), do: nil
def effective_gas_price(%Transaction{block: %NotLoaded{}}), do: nil
def effective_gas_price(%Transaction{}, %NotLoaded{}), do: nil
def effective_gas_price(%Transaction{}, nil), do: nil
def effective_gas_price(%Transaction{} = transaction) do
base_fee_per_gas = transaction.block.base_fee_per_gas
def effective_gas_price(%Transaction{} = transaction, block) do
base_fee_per_gas = block.base_fee_per_gas
max_priority_fee_per_gas = transaction.max_priority_fee_per_gas
max_fee_per_gas = transaction.max_fee_per_gas

@ -71,6 +71,8 @@ defmodule Explorer.Chain.CSVExport.AddressInternalTransactionCsvExporterTest do
[[], output],
_,
[[], error],
_,
[[], fee],
_
] ->
%{
@ -91,7 +93,8 @@ defmodule Explorer.Chain.CSVExport.AddressInternalTransactionCsvExporterTest do
value: value,
input: input,
output: output,
error: error
error: error,
fee: fee
}
end)
@ -113,6 +116,13 @@ defmodule Explorer.Chain.CSVExport.AddressInternalTransactionCsvExporterTest do
assert result.input == to_string(internal_transaction.input)
assert result.output == to_string(internal_transaction.output)
assert result.error == to_string(internal_transaction.error)
assert result.fee ==
to_string(
internal_transaction.transaction.gas_price
|> Wei.mult(internal_transaction.gas_used)
|> Wei.to(:wei)
)
end
test "fetches all internal transactions" do

Loading…
Cancel
Save