np-token-balances-refactoring
Nikita Pozdniakov 1 year ago
parent d92f13ceda
commit 321b90f75c
No known key found for this signature in database
GPG Key ID: F344106F9804FE5F
  1. 3
      apps/block_scout_web/lib/block_scout_web/chain.ex
  2. 6
      apps/block_scout_web/test/block_scout_web/views/address_token_balance_view_test.exs
  3. 12
      apps/explorer/lib/explorer/chain.ex
  4. 4
      apps/explorer/test/explorer/chain/address/current_token_balance_test.exs
  5. 2
      apps/explorer/test/explorer/chain_test.exs
  6. 8
      apps/explorer/test/explorer/counters/addresses_tokens_usd_sum_counter_test.exs

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

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

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

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

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

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

Loading…
Cancel
Save