diff --git a/CHANGELOG.md b/CHANGELOG.md index cba4a60544..f2171ccfc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## Current ### Features +- [#3515](https://github.com/poanetwork/blockscout/pull/3515) - CRC total balance - [#3513](https://github.com/poanetwork/blockscout/pull/3513) - Allow square brackets for an array input data in contracts interaction - [#3480](https://github.com/poanetwork/blockscout/pull/3480) - Add support of Autonity client - [#3470](https://github.com/poanetwork/blockscout/pull/3470) - Display sum of tokens' USD value at tokens holder's address page diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/address_token_balance_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/address_token_balance_controller.ex index 42a78fb01d..c59760f682 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/address_token_balance_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/address_token_balance_controller.ex @@ -1,7 +1,8 @@ defmodule BlockScoutWeb.AddressTokenBalanceController do use BlockScoutWeb, :controller - alias BlockScoutWeb.AccessHelpers + import BlockScoutWeb.AddressView, only: [from_address_hash: 1] + alias BlockScoutWeb.{AccessHelpers, CustomContractsHelpers} alias Explorer.{Chain, Market} def index(conn, %{"address_id" => address_hash_string} = params) do @@ -12,18 +13,50 @@ defmodule BlockScoutWeb.AddressTokenBalanceController do |> Chain.fetch_last_token_balances() |> Market.add_price() + circles_addresses_list = CustomContractsHelpers.get_custom_addresses_list(:circles_addresses) + + circles_total_balance = + if Enum.count(circles_addresses_list) > 0 do + token_balances + |> Enum.reduce(Decimal.new(0), fn token_balance, acc_balance -> + {:ok, token_address} = Chain.hash_to_address(token_balance.address_hash) + + created_from_address_hash = + if from_address_hash(token_address), + do: "0x" <> Base.encode16(from_address_hash(token_address).bytes, case: :lower), + else: nil + + if Enum.member?(circles_addresses_list, created_from_address_hash) && token_balance.token.name == "Circles" && + token_balance.token.symbol == "CRC" do + Decimal.add(acc_balance, token_balance.value) + else + acc_balance + end + end) + else + Decimal.new(0) + end + case AccessHelpers.restricted_access?(address_hash_string, params) do {:ok, false} -> conn |> put_status(200) |> put_layout(false) - |> render("_token_balances.html", address_hash: address_hash, token_balances: token_balances) + |> render("_token_balances.html", + address_hash: address_hash, + token_balances: token_balances, + circles_total_balance: circles_total_balance + ) _ -> conn |> put_status(200) |> put_layout(false) - |> render("_token_balances.html", address_hash: address_hash, token_balances: []) + |> render("_token_balances.html", + address_hash: address_hash, + token_balances: [], + circles_total_balance: Decimal.new(0) + ) end else _ -> diff --git a/apps/block_scout_web/lib/block_scout_web/templates/address_token_balance/_token_balances.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/address_token_balance/_token_balances.html.eex index 2e5a4d520d..aef7524341 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/address_token_balance/_token_balances.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/address_token_balance/_token_balances.html.eex @@ -1,3 +1,6 @@ +<%= if Decimal.cmp(@circles_total_balance, 0) == :gt do %> +
<%= format_according_to_decimals(@circles_total_balance, Decimal.new(18)) %> CRC
+<% end %> <%= if Enum.any?(@token_balances) do %>