add today's token balance from previous value

pull/1756/head
Ayrat Badykov 6 years ago
parent 3069d5addc
commit 115fa6646e
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 19
      apps/explorer/lib/explorer/chain.ex
  2. 16
      apps/explorer/test/explorer/chain_test.exs

@ -2613,11 +2613,20 @@ defmodule Explorer.Chain do
end
defp normalize_balances_by_day(balances_by_day) do
balances_by_day
|> Enum.map(fn day -> Map.take(day, [:date, :value]) end)
|> Enum.filter(fn day -> day.value end)
|> Enum.map(fn day -> Map.update!(day, :date, &to_string(&1)) end)
|> Enum.map(fn day -> Map.update!(day, :value, &Wei.to(&1, :ether)) end)
result =
balances_by_day
|> Enum.map(fn day -> Map.take(day, [:date, :value]) end)
|> Enum.filter(fn day -> day.value end)
|> Enum.map(fn day -> Map.update!(day, :date, &to_string(&1)) end)
|> Enum.map(fn day -> Map.update!(day, :value, &Wei.to(&1, :ether)) end)
today = Date.to_string(NaiveDateTime.utc_now())
if Enum.count(result) > 0 && !Enum.any?(result, fn map -> map[:data] == today end) do
result ++ [%{date: today, value: List.last(result)[:value]}]
else
result
end
end
@spec fetch_token_holders_from_token_hash(Hash.Address.t(), [paging_options]) :: [TokenBalance.t()]

@ -3665,6 +3665,22 @@ defmodule Explorer.ChainTest do
%{date: today |> NaiveDateTime.to_date() |> Date.to_string(), value: Decimal.new("1E-15")}
]
end
test "adds todays entry" do
address = insert(:address)
today = NaiveDateTime.utc_now()
noon = Timex.set(today, hour: 12)
yesterday = Timex.shift(noon, days: -1)
block_one_day_ago = insert(:block, timestamp: yesterday)
insert(:fetched_balance, address_hash: address.hash, value: 1000, block_number: block_one_day_ago.number)
balances = Chain.address_to_balances_by_day(address.hash)
assert balances == [
%{date: "2019-04-14", value: Decimal.new("1E-15")},
%{date: "2019-04-15", value: Decimal.new("1E-15")}
]
end
end
describe "block_combined_rewards/1" do

Loading…
Cancel
Save