From 321b90f75c6d72c84003d412f31ca6b27de5e97c Mon Sep 17 00:00:00 2001 From: Nikita Pozdniakov Date: Thu, 6 Jul 2023 12:53:36 +0300 Subject: [PATCH] Fix tests --- apps/block_scout_web/lib/block_scout_web/chain.ex | 3 +-- .../views/address_token_balance_view_test.exs | 6 ++---- apps/explorer/lib/explorer/chain.ex | 12 +++++++++++- .../chain/address/current_token_balance_test.exs | 4 ++-- apps/explorer/test/explorer/chain_test.exs | 2 +- .../addresses_tokens_usd_sum_counter_test.exs | 8 ++++---- 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/chain.ex b/apps/block_scout_web/lib/block_scout_web/chain.ex index d97adb0564..488f14279b 100644 --- a/apps/block_scout_web/lib/block_scout_web/chain.ex +++ b/apps/block_scout_web/lib/block_scout_web/chain.ex @@ -5,7 +5,6 @@ defmodule BlockScoutWeb.Chain do import Explorer.Chain, only: [ - balance_in_fiat: 1, find_or_insert_address_from_hash: 1, hash_to_block: 1, hash_to_transaction: 1, @@ -502,7 +501,7 @@ defmodule BlockScoutWeb.Chain do end defp paging_params_with_fiat_value(%CurrentTokenBalance{id: id, value: value} = ctb) do - %{"fiat_value" => balance_in_fiat(ctb), "value" => value, "id" => id} + %{"fiat_value" => ctb.fiat_value, "value" => value, "id" => id} end defp block_or_transaction_from_param(param) do diff --git a/apps/block_scout_web/test/block_scout_web/views/address_token_balance_view_test.exs b/apps/block_scout_web/test/block_scout_web/views/address_token_balance_view_test.exs index eb9fa83061..778c1eb036 100644 --- a/apps/block_scout_web/test/block_scout_web/views/address_token_balance_view_test.exs +++ b/apps/block_scout_web/test/block_scout_web/views/address_token_balance_view_test.exs @@ -20,11 +20,9 @@ defmodule BlockScoutWeb.AddressTokenBalanceViewTest do token_balance_a = build(:token_balance, token: build(:token, type: "ERC-20")) token_balance_b = build(:token_balance, token: build(:token, type: "ERC-721")) - token_balances = [{token_balance_a, token_balance_a.token}, {token_balance_b, token_balance_b.token}] + token_balances = [token_balance_a, token_balance_b] - assert AddressTokenBalanceView.filter_by_type(token_balances, "ERC-20") == [ - {token_balance_a, token_balance_a.token} - ] + assert AddressTokenBalanceView.filter_by_type(token_balances, "ERC-20") == [token_balance_a] end end diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index d921f44fea..7ad0d152b9 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -47,6 +47,7 @@ defmodule Explorer.Chain do Address.CurrentTokenBalance, Address.TokenBalance, Block, + CurrencyHelper, Data, DecompiledSmartContract, Hash, @@ -2689,10 +2690,19 @@ defmodule Explorer.Chain do @doc """ Return the balance in usd corresponding to this token. Return nil if the fiat_value of the token is not present. """ - def balance_in_fiat(token_balance) do + def balance_in_fiat(%{fiat_value: fiat_value} = token_balance) when not is_nil(fiat_value) do token_balance.fiat_value end + def balance_in_fiat(%{token: %{fiat_value: fiat_value, decimals: decimals}}) when nil in [fiat_value, decimals] do + nil + end + + def balance_in_fiat(%{token: %{fiat_value: fiat_value, decimals: decimals}} = token_balance) do + tokens = CurrencyHelper.divide_decimals(token_balance.value, decimals) + Decimal.mult(tokens, fiat_value) + end + defp contract?(%{contract_code: nil}), do: false defp contract?(%{contract_code: _}), do: true diff --git a/apps/explorer/test/explorer/chain/address/current_token_balance_test.exs b/apps/explorer/test/explorer/chain/address/current_token_balance_test.exs index e7a366377e..85d49a8c19 100644 --- a/apps/explorer/test/explorer/chain/address/current_token_balance_test.exs +++ b/apps/explorer/test/explorer/chain/address/current_token_balance_test.exs @@ -157,7 +157,7 @@ defmodule Explorer.Chain.Address.CurrentTokenBalanceTest do address.hash |> CurrentTokenBalance.last_token_balances() |> Repo.all() - |> Enum.map(fn {token_balance, _} -> token_balance.address_hash end) + |> Enum.map(fn token_balance -> token_balance.address_hash end) assert token_balances == [current_token_balance.address_hash] end @@ -195,7 +195,7 @@ defmodule Explorer.Chain.Address.CurrentTokenBalanceTest do address.hash |> CurrentTokenBalance.last_token_balances() |> Repo.all() - |> Enum.map(fn {token_balance, _} -> token_balance.address_hash end) + |> Enum.map(fn token_balance -> token_balance.address_hash end) assert token_balances == [current_token_balance_a.address_hash] end diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 5e284529a8..d3739754eb 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -5080,7 +5080,7 @@ defmodule Explorer.ChainTest do token_balances = address.hash |> Chain.fetch_last_token_balances() - |> Enum.map(fn {token_balance, _} -> token_balance.address_hash end) + |> Enum.map(fn token_balance -> token_balance.address_hash end) assert token_balances == [current_token_balance.address_hash] end diff --git a/apps/explorer/test/explorer/counters/addresses_tokens_usd_sum_counter_test.exs b/apps/explorer/test/explorer/counters/addresses_tokens_usd_sum_counter_test.exs index 12e7153a0a..d56753852f 100644 --- a/apps/explorer/test/explorer/counters/addresses_tokens_usd_sum_counter_test.exs +++ b/apps/explorer/test/explorer/counters/addresses_tokens_usd_sum_counter_test.exs @@ -19,15 +19,15 @@ defmodule Explorer.Counters.AddressTokenUsdSumTest do ) AddressTokenUsdSum.fetch(address.hash, [ - {address_current_token_balance, address_current_token_balance.token}, - {address_current_token_balance_2, address_current_token_balance_2.token} + address_current_token_balance, + address_current_token_balance_2 ]) Process.sleep(200) assert AddressTokenUsdSum.fetch(address.hash, [ - {address_current_token_balance, address_current_token_balance.token}, - {address_current_token_balance_2, address_current_token_balance_2.token} + address_current_token_balance, + address_current_token_balance_2 ]) == Decimal.new(2_010_000) end