Fix address_tokens_usd_sum function

pull/5799/head
Viktor Baranov 2 years ago
parent d3daec8c60
commit 913a69abdf
  1. 11
      apps/explorer/lib/explorer/chain.ex
  2. 14
      apps/explorer/lib/explorer/counters/address_tokens_usd_sum.ex
  3. 34
      apps/explorer/test/explorer/counters/addresses_tokens_usd_sum_counter_test.exs

@ -2506,17 +2506,6 @@ defmodule Explorer.Chain do
Decimal.mult(tokens, price) Decimal.mult(tokens, price)
end end
def address_tokens_usd_sum(token_balances) do
token_balances
|> Enum.reduce(Decimal.new(0), fn {token_balance, _, _}, acc ->
if token_balance.value && token_balance.token.usd_value do
Decimal.add(acc, balance_in_usd(token_balance))
else
acc
end
end)
end
defp contract?(%{contract_code: nil}), do: false defp contract?(%{contract_code: nil}), do: false
defp contract?(%{contract_code: _}), do: true defp contract?(%{contract_code: _}), do: true

@ -50,6 +50,18 @@ defmodule Explorer.Counters.AddressTokenUsdSum do
fetch_from_cache("hash_#{address_hash_string}") fetch_from_cache("hash_#{address_hash_string}")
end end
@spec address_tokens_usd_sum([{Address.CurrentTokenBalance, Explorer.Chain.Token}]) :: Decimal.t()
defp address_tokens_usd_sum(token_balances) do
token_balances
|> Enum.reduce(Decimal.new(0), fn {token_balance, _}, acc ->
if token_balance.value && token_balance.token.usd_value do
Decimal.add(acc, Chain.balance_in_usd(token_balance))
else
acc
end
end)
end
def cache_name, do: @cache_name def cache_name, do: @cache_name
defp cache_expired?(address_hash_string) do defp cache_expired?(address_hash_string) do
@ -65,7 +77,7 @@ defmodule Explorer.Counters.AddressTokenUsdSum do
defp update_cache(address_hash_string, token_balances) do defp update_cache(address_hash_string, token_balances) do
put_into_cache("hash_#{address_hash_string}_#{@last_update_key}", Helper.current_time()) put_into_cache("hash_#{address_hash_string}_#{@last_update_key}", Helper.current_time())
new_data = Chain.address_tokens_usd_sum(token_balances) new_data = address_tokens_usd_sum(token_balances)
put_into_cache("hash_#{address_hash_string}", new_data) put_into_cache("hash_#{address_hash_string}", new_data)
end end

@ -0,0 +1,34 @@
defmodule Explorer.Counters.AddressTokenUsdSumTest do
use Explorer.DataCase
alias Explorer.Counters.AddressTokenUsdSum
test "populates the cache with the sum of address tokens" do
address = insert(:address)
address_current_token_balance =
build(:token_balance,
token: build(:token, name: "token name", decimals: Decimal.new(18)) |> Map.put(:usd_value, Decimal.new(10)),
value: Decimal.mult(Decimal.new(100_500), Decimal.from_float(:math.pow(10, 18)))
)
address_current_token_balance_2 =
build(:token_balance,
token: build(:token, name: "token name", decimals: Decimal.new(18)) |> Map.put(:usd_value, Decimal.new(10)),
value: Decimal.mult(Decimal.new(100_500), Decimal.from_float(:math.pow(10, 18)))
)
AddressTokenUsdSum.fetch(address.hash, [
{address_current_token_balance, address_current_token_balance.token},
{address_current_token_balance_2, address_current_token_balance_2.token}
])
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}
]) ==
Decimal.new(2_010_000)
end
end
Loading…
Cancel
Save