From 8d5d4a760059a6bc00ec21c1c1cb013dfc500279 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 18 Sep 2019 13:09:03 +0300 Subject: [PATCH 1/5] add tests for token balances runner --- .../runner/address/token_balances_test.exs | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs diff --git a/apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs b/apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs new file mode 100644 index 0000000000..a40cd8599e --- /dev/null +++ b/apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs @@ -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 From 28c153fcdd32a5f03b63ab96f6ca59d0458c5c36 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 18 Sep 2019 13:09:32 +0300 Subject: [PATCH 2/5] refetch token balances with empty value --- apps/explorer/lib/explorer/chain.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 15f6644f25..99da578445 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -1496,7 +1496,7 @@ defmodule Explorer.Chain do query = from( balance in CoinBalance, - where: is_nil(balance.value_fetched_at), + where: is_nil(balance.value_fetched_at) or is_nil(balance.value), select: %{address_hash: balance.address_hash, block_number: balance.block_number} ) From 79fa24013c401dfb29c69dd091c2d01c4c2bbb42 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 18 Sep 2019 13:12:24 +0300 Subject: [PATCH 3/5] check if value is null in token balances refetch query --- apps/explorer/lib/explorer/chain.ex | 2 +- apps/explorer/lib/explorer/chain/address/token_balance.ex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 99da578445..15f6644f25 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -1496,7 +1496,7 @@ defmodule Explorer.Chain do query = from( balance in CoinBalance, - where: is_nil(balance.value_fetched_at) or is_nil(balance.value), + where: is_nil(balance.value_fetched_at), select: %{address_hash: balance.address_hash, block_number: balance.block_number} ) diff --git a/apps/explorer/lib/explorer/chain/address/token_balance.ex b/apps/explorer/lib/explorer/chain/address/token_balance.ex index 1033d0a7ca..9a1c2f7b19 100644 --- a/apps/explorer/lib/explorer/chain/address/token_balance.ex +++ b/apps/explorer/lib/explorer/chain/address/token_balance.ex @@ -82,7 +82,7 @@ defmodule Explorer.Chain.Address.TokenBalance do tb in TokenBalance, join: t in Token, 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" ) end From 83bc664f9ceb7b587b7b3a1b93a9d92cd59d4ed0 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 18 Sep 2019 13:18:28 +0300 Subject: [PATCH 4/5] add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba474acce5..1358eade8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - [#2682](https://github.com/poanetwork/blockscout/pull/2682) - Use Task.start instead of Task.async in caches - [#2671](https://github.com/poanetwork/blockscout/pull/2671) - fixed buttons color at smart contract section - [#2691](https://github.com/poanetwork/blockscout/pull/2691) - fix exchange rate websocket update for Rootstock +- [#2704](https://github.com/poanetwork/blockscout/pull/2704) - refetch null values in token balances ### Chore From cc8e8b0a5d17713eba817620407b3e8835ede807 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Mon, 25 Nov 2019 19:29:11 +0300 Subject: [PATCH 5/5] Update documentation of unfetched_token_balances function --- apps/explorer/lib/explorer/chain/address/token_balance.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/explorer/lib/explorer/chain/address/token_balance.ex b/apps/explorer/lib/explorer/chain/address/token_balance.ex index 9a1c2f7b19..d3941fd9e2 100644 --- a/apps/explorer/lib/explorer/chain/address/token_balance.ex +++ b/apps/explorer/lib/explorer/chain/address/token_balance.ex @@ -73,7 +73,7 @@ defmodule Explorer.Chain.Address.TokenBalance do @doc """ 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 balance for burn_address. """