Merge pull request #2704 from poanetwork/ab-refetch-null-value-token-balances

refetch null values in token balances
pull/2878/head
Victor Baranov 5 years ago committed by GitHub
commit 7788f4efd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 4
      apps/explorer/lib/explorer/chain/address/token_balance.ex
  3. 109
      apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs

@ -24,6 +24,7 @@
- [#2800](https://github.com/poanetwork/blockscout/pull/2800) - return not found for not verified contract for token read_contract - [#2800](https://github.com/poanetwork/blockscout/pull/2800) - return not found for not verified contract for token read_contract
- [#2806](https://github.com/poanetwork/blockscout/pull/2806) - Fix blocks fetching on the main page - [#2806](https://github.com/poanetwork/blockscout/pull/2806) - Fix blocks fetching on the main page
- [#2803](https://github.com/poanetwork/blockscout/pull/2803) - Fix block validator custom tooltip - [#2803](https://github.com/poanetwork/blockscout/pull/2803) - Fix block validator custom tooltip
- [#2704](https://github.com/poanetwork/blockscout/pull/2704) - refetch null values in token balances
- [#2690](https://github.com/poanetwork/blockscout/pull/2690) - do not stich json rpc config into module for net version cache - [#2690](https://github.com/poanetwork/blockscout/pull/2690) - do not stich json rpc config into module for net version cache
### Chore ### Chore

@ -73,7 +73,7 @@ defmodule Explorer.Chain.Address.TokenBalance do
@doc """ @doc """
Builds an `Ecto.Query` to fetch the unfetched token balances. Builds an `Ecto.Query` to fetch the unfetched token balances.
Unfetched token balances are the ones that have the column `value_fetched_at` nil. This query also Unfetched token balances are the ones that have the column `value_fetched_at` nil or the value is null. This query also
ignores the burn_address for tokens ERC-721 since the most tokens ERC-721 don't allow get the ignores the burn_address for tokens ERC-721 since the most tokens ERC-721 don't allow get the
balance for burn_address. balance for burn_address.
""" """
@ -82,7 +82,7 @@ defmodule Explorer.Chain.Address.TokenBalance do
tb in TokenBalance, tb in TokenBalance,
join: t in Token, join: t in Token,
on: tb.token_contract_address_hash == t.contract_address_hash, on: tb.token_contract_address_hash == t.contract_address_hash,
where: is_nil(tb.value_fetched_at), where: is_nil(tb.value_fetched_at) or is_nil(tb.value),
where: (tb.address_hash != ^@burn_address_hash and t.type != "ERC-721") or t.type == "ERC-20" where: (tb.address_hash != ^@burn_address_hash and t.type != "ERC-721") or t.type == "ERC-20"
) )
end end

@ -0,0 +1,109 @@
defmodule Explorer.Chain.Import.Runner.Address.TokenBalancesTest do
use Explorer.DataCase
alias Ecto.Multi
alias Explorer.Chain.Address.TokenBalance
alias Explorer.Chain.Import.Runner.Address.TokenBalances
describe "run/2" do
test "inserts token balance" do
address = insert(:address)
token = insert(:token)
options = %{
timeout: :infinity,
timestamps: %{inserted_at: DateTime.utc_now(), updated_at: DateTime.utc_now()}
}
value_fetched_at = DateTime.utc_now()
block_number = 1
value = Decimal.new(100)
token_contract_address_hash = token.contract_address_hash
address_hash = address.hash
changes = %{
address_hash: address_hash,
block_number: block_number,
token_contract_address_hash: token_contract_address_hash,
value: value,
value_fetched_at: value_fetched_at
}
assert {:ok,
%{
address_token_balances: [
%TokenBalance{
address_hash: address_hash,
block_number: ^block_number,
token_contract_address_hash: ^token_contract_address_hash,
value: ^value,
value_fetched_at: ^value_fetched_at
}
]
}} = run_changes(changes, options)
end
test "does not nillifies existing value" do
address = insert(:address)
token = insert(:token)
options = %{
timeout: :infinity,
timestamps: %{inserted_at: DateTime.utc_now(), updated_at: DateTime.utc_now()}
}
value_fetched_at = DateTime.utc_now()
block_number = 1
value = Decimal.new(100)
token_contract_address_hash = token.contract_address_hash
address_hash = address.hash
changes = %{
address_hash: address_hash,
block_number: block_number,
token_contract_address_hash: token_contract_address_hash,
value: nil,
value_fetched_at: value_fetched_at
}
assert {:ok,
%{
address_token_balances: [
%TokenBalance{
address_hash: address_hash,
block_number: ^block_number,
token_contract_address_hash: ^token_contract_address_hash,
value: nil,
value_fetched_at: ^value_fetched_at
}
]
}} = run_changes(changes, options)
new_changes = %{
address_hash: address_hash,
block_number: block_number,
token_contract_address_hash: token_contract_address_hash,
value: value,
value_fetched_at: DateTime.utc_now()
}
run_changes(new_changes, options) |> IO.inspect()
end
end
defp run_changes(changes, options) when is_map(changes) do
run_changes_list([changes], options)
end
defp run_changes_list(changes_list, options) when is_list(changes_list) do
Multi.new()
|> TokenBalances.run(changes_list, options)
|> Repo.transaction()
end
end
Loading…
Cancel
Save