Total transactions fee API endpoint

pull/4205/head
Viktor Baranov 4 years ago
parent 52f234ea4f
commit 4dfaed9394
  1. 1
      CHANGELOG.md
  2. 13
      apps/block_scout_web/lib/block_scout_web/controllers/api/rpc/stats_controller.ex
  3. 65
      apps/block_scout_web/lib/block_scout_web/etherscan.ex
  4. 4
      apps/block_scout_web/lib/block_scout_web/views/api/rpc/stats_view.ex
  5. 23
      apps/explorer/lib/explorer/chain.ex

@ -1,6 +1,7 @@
## Current ## Current
### Features ### Features
- [#4205](https://github.com/blockscout/blockscout/pull/4205) - Total transactions fees per day API endpoint
- [#4158](https://github.com/blockscout/blockscout/pull/4158) - Calculate total fee per day - [#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 - [#4067](https://github.com/blockscout/blockscout/pull/4067) - Display LP tokens USD value and custom metadata in tokens dropdown at address page

@ -73,4 +73,17 @@ defmodule BlockScoutWeb.API.RPC.StatsController do
defp to_address_hash(address_hash_string) do defp to_address_hash(address_hash_string) do
{:format, Chain.string_to_address_hash(address_hash_string)} {:format, Chain.string_to_address_hash(address_hash_string)}
end end
def totalfees(conn, params) do
case Map.fetch(params, "date") do
{:ok, date} ->
case Chain.get_total_fees_per_day(date) do
{:ok, total_fees} -> render(conn, "totalfees.json", total_fees: total_fees)
{:error, error} -> render(conn, :error, error: error)
end
_ ->
render(conn, :error, error: "Required date input is missing.")
end
end
end end

@ -324,6 +324,20 @@ defmodule BlockScoutWeb.Etherscan do
} }
} }
@stats_totalfees_example_value %{
"status" => "1",
"message" => "OK",
"result" => %{
"total_fees" => "75411956011480008034"
}
}
@stats_totalfees_example_value_error %{
"status" => "0",
"message" => "An incorrect input date provided. It should be in ISO 8601 format (yyyy-mm-dd).",
"result" => nil
}
@block_getblockreward_example_value %{ @block_getblockreward_example_value %{
"status" => "1", "status" => "1",
"message" => "OK", "message" => "OK",
@ -1134,6 +1148,17 @@ defmodule BlockScoutWeb.Etherscan do
} }
} }
@total_fees_model %{
name: "TotalFees",
fields: %{
total_fees: %{
type: "total_fees",
definition: "Total transaction fees in Wei are paid by users to validators per day.",
example: ~s("75411956011480008034")
}
}
}
@account_eth_get_balance_action %{ @account_eth_get_balance_action %{
name: "eth_get_balance", name: "eth_get_balance",
description: description:
@ -2067,6 +2092,43 @@ defmodule BlockScoutWeb.Etherscan do
] ]
} }
@stats_totalfees_action %{
name: "totalfees",
description: "Gets total transaction fees in Wei are paid by users to validators per day.",
required_params: [
%{
key: "date",
placeholder: "date",
type: "string",
description: "day in ISO 8601 format (yyyy-mm-dd)"
}
],
optional_params: [],
responses: [
%{
code: "200",
description: "successful operation",
example_value: Jason.encode!(@stats_totalfees_example_value),
model: %{
name: "Result",
fields: %{
status: @status_type,
message: @message_type,
result: %{
type: "model",
model: @total_fees_model
}
}
}
},
%{
code: "200",
description: "error",
example_value: Jason.encode!(@stats_totalfees_example_value_error)
}
]
}
@block_eth_block_number_action %{ @block_eth_block_number_action %{
name: "eth_block_number", name: "eth_block_number",
description: "Mimics Ethereum JSON RPC's eth_blockNumber. Returns the lastest block number", description: "Mimics Ethereum JSON RPC's eth_blockNumber. Returns the lastest block number",
@ -2602,7 +2664,8 @@ defmodule BlockScoutWeb.Etherscan do
@stats_ethsupplyexchange_action, @stats_ethsupplyexchange_action,
@stats_ethsupply_action, @stats_ethsupply_action,
@stats_coinsupply_action, @stats_coinsupply_action,
@stats_coinprice_action @stats_coinprice_action,
@stats_totalfees_action
] ]
} }

@ -23,6 +23,10 @@ defmodule BlockScoutWeb.API.RPC.StatsView do
RPCView.render("show.json", data: prepare_rates(rates)) RPCView.render("show.json", data: prepare_rates(rates))
end end
def render("totalfees.json", %{total_fees: total_fees}) do
RPCView.render("show.json", data: total_fees)
end
def render("error.json", assigns) do def render("error.json", assigns) do
RPCView.render("error.json", assigns) RPCView.render("error.json", assigns)
end end

@ -79,6 +79,7 @@ defmodule Explorer.Chain do
alias Explorer.Chain.Import.Runner alias Explorer.Chain.Import.Runner
alias Explorer.Chain.InternalTransaction.{CallType, Type} alias Explorer.Chain.InternalTransaction.{CallType, Type}
alias Explorer.Chain.Transaction.History.TransactionStats
alias Explorer.Counters.{AddressesCounter, AddressesWithBalanceCounter} alias Explorer.Counters.{AddressesCounter, AddressesWithBalanceCounter}
alias Explorer.Market.MarketHistoryCache alias Explorer.Market.MarketHistoryCache
alias Explorer.{PagingOptions, Repo} alias Explorer.{PagingOptions, Repo}
@ -6314,4 +6315,26 @@ defmodule Explorer.Chain do
_ -> "" _ -> ""
end end
end end
@doc """
It is used by `totalfees` API endpoint of `stats` module for retrieving of total fee per day
"""
@spec get_total_fees_per_day(String.t()) :: {:ok, non_neg_integer() | nil} | {:error, String.t()}
def get_total_fees_per_day(date_string) do
case Date.from_iso8601(date_string) do
{:ok, date} ->
query =
from(
tx_stats in TransactionStats,
where: tx_stats.date == ^date,
select: tx_stats.total_fee
)
total_fees = Repo.one(query)
{:ok, total_fees}
_ ->
{:error, "An incorrect input date provided. It should be in ISO 8601 format (yyyy-mm-dd)."}
end
end
end end

Loading…
Cancel
Save