ERC-1155: token balanaces indexer update

(cherry picked from commit 03b4f4b7db)
pull/4761/head
Viktor Baranov 4 years ago
parent a3fba9d1cb
commit 9bc4f8b5ad
  1. 1
      apps/block_scout_web/priv/gettext/default.pot
  2. 95
      apps/block_scout_web/priv/gettext/en/LC_MESSAGES/default.po
  3. 19
      apps/explorer/priv/repo/migrations/20210423084253_address_current_token_balances_add_token_id_to_unique_index.exs
  4. 19
      apps/explorer/priv/repo/migrations/20210423091652_address_token_balances_add_token_id_to_unique_index.exs
  5. 13
      apps/explorer/priv/repo/migrations/20210423094801_address_token_balances_change_unfetched_token_balances_unique_index.exs
  6. 54
      apps/explorer/test/explorer/chain/import/runner/address/token_balances_test.exs
  7. 29
      apps/indexer/test/indexer/fetcher/token_balance_test.exs

@ -3193,6 +3193,7 @@ msgid "validator"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#:
#: lib/block_scout_web/templates/common_components/_token_transfer_type_display_name.html.eex:7 #: lib/block_scout_web/templates/common_components/_token_transfer_type_display_name.html.eex:7
#: lib/block_scout_web/views/transaction_view.ex:392 #: lib/block_scout_web/views/transaction_view.ex:392
msgid "Token Creation" msgid "Token Creation"

@ -76,16 +76,12 @@ msgid "%{subnetwork} Explorer - BlockScout"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
<<<<<<< HEAD
#: lib/block_scout_web/templates/stakes/_metatags.html.eex:2 #: lib/block_scout_web/templates/stakes/_metatags.html.eex:2
msgid "%{subnetwork} Staking DApp - BlockScout" msgid "%{subnetwork} Staking DApp - BlockScout"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:356 #: lib/block_scout_web/views/transaction_view.ex:356
=======
#: lib/block_scout_web/views/transaction_view.ex:310
>>>>>>> 7148d5f99 (ERC-1155 finalization)
msgid "(Awaiting internal transactions for status)" msgid "(Awaiting internal transactions for status)"
msgstr "" msgstr ""
@ -234,15 +230,8 @@ msgid "All pool participant addresses. The top address belongs to the %{pool_typ
msgstr "" msgstr ""
#, elixir-format #, elixir-format
<<<<<<< HEAD
#: lib/block_scout_web/templates/address/overview.html.eex:191 #: lib/block_scout_web/templates/address/overview.html.eex:191
msgid "All tokens in the account and total value." msgid "All tokens in the account and total value."
=======
#: lib/block_scout_web/templates/block/_link.html.eex:2
#: lib/block_scout_web/templates/internal_transaction/_tile.html.eex:28
#: lib/block_scout_web/templates/tokens/transfer/_token_transfer.html.eex:43
msgid "Block #%{number}"
>>>>>>> 7148d5f99 (ERC-1155 finalization)
msgstr "" msgstr ""
#, elixir-format #, elixir-format
@ -277,13 +266,8 @@ msgid "Anything not in this list is not supported. Click on the method to be tak
msgstr "" msgstr ""
#, elixir-format #, elixir-format
<<<<<<< HEAD
#: lib/block_scout_web/templates/stakes/_stakes_modal_delegators_list.html.eex:80 #: lib/block_scout_web/templates/stakes/_stakes_modal_delegators_list.html.eex:80
msgid "Approximate Current Annual Percentage Yield. If you see N/A, please reopen the popup in a few blocks (APY cannot be calculated at the very beginning of a staking epoch). Predicted Reward is the amount of %{symbol} a participant will receive for staking and can claim once the current epoch ends." msgid "Approximate Current Annual Percentage Yield. If you see N/A, please reopen the popup in a few blocks (APY cannot be calculated at the very beginning of a staking epoch). Predicted Reward is the amount of %{symbol} a participant will receive for staking and can claim once the current epoch ends."
=======
#: lib/block_scout_web/views/transaction_view.ex:33
msgid "Block Pending"
>>>>>>> 7148d5f99 (ERC-1155 finalization)
msgstr "" msgstr ""
#, elixir-format #, elixir-format
@ -409,7 +393,6 @@ msgid "Block number"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
<<<<<<< HEAD
#: lib/block_scout_web/templates/transaction/overview.html.eex:139 #: lib/block_scout_web/templates/transaction/overview.html.eex:139
msgid "Block number containing the transaction." msgid "Block number containing the transaction."
msgstr "" msgstr ""
@ -417,15 +400,6 @@ msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/address/overview.html.eex:249 #: lib/block_scout_web/templates/address/overview.html.eex:249
msgid "Block number in which the address was updated." msgid "Block number in which the address was updated."
=======
#: lib/block_scout_web/views/transaction_view.ex:400
msgid "Contract Call"
msgstr ""
#, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:397
msgid "Contract Creation"
>>>>>>> 7148d5f99 (ERC-1155 finalization)
msgstr "" msgstr ""
#, elixir-format #, elixir-format
@ -606,7 +580,6 @@ msgid "Compiler version"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
<<<<<<< HEAD
#: lib/block_scout_web/views/transaction_view.ex:349 #: lib/block_scout_web/views/transaction_view.ex:349
msgid "Confirmed" msgid "Confirmed"
msgstr "" msgstr ""
@ -614,15 +587,6 @@ msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/templates/transaction/overview.html.eex:118 #: lib/block_scout_web/templates/transaction/overview.html.eex:118
msgid "Confirmed by " msgid "Confirmed by "
=======
#: lib/block_scout_web/views/transaction_view.ex:195
msgid "ERC-20 "
msgstr ""
#, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:196
msgid "ERC-721 "
>>>>>>> 7148d5f99 (ERC-1155 finalization)
msgstr "" msgstr ""
#, elixir-format #, elixir-format
@ -684,7 +648,6 @@ msgid "Contract Address"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
<<<<<<< HEAD
#: lib/block_scout_web/templates/transaction/_pending_tile.html.eex:16 #: lib/block_scout_web/templates/transaction/_pending_tile.html.eex:16
#: lib/block_scout_web/views/address_view.ex:42 lib/block_scout_web/views/address_view.ex:76 #: lib/block_scout_web/views/address_view.ex:42 lib/block_scout_web/views/address_view.ex:76
msgid "Contract Address Pending" msgid "Contract Address Pending"
@ -693,15 +656,6 @@ msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:460 #: lib/block_scout_web/views/transaction_view.ex:460
msgid "Contract Call" msgid "Contract Call"
=======
#: lib/block_scout_web/views/transaction_view.ex:314
msgid "Error: %{reason}"
msgstr ""
#, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:312
msgid "Error: (Awaiting internal transactions for reason)"
>>>>>>> 7148d5f99 (ERC-1155 finalization)
msgstr "" msgstr ""
#, elixir-format #, elixir-format
@ -716,20 +670,8 @@ msgid "Contract Creation Code"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
<<<<<<< HEAD
#: lib/block_scout_web/templates/address_contract_verification_via_flattened_code/new.html.eex:170 #: lib/block_scout_web/templates/address_contract_verification_via_flattened_code/new.html.eex:170
msgid "Contract Libraries" msgid "Contract Libraries"
=======
#: lib/block_scout_web/templates/address/_balance_card.html.eex:18
#: lib/block_scout_web/templates/internal_transaction/_tile.html.eex:20
#: lib/block_scout_web/templates/layout/app.html.eex:33
#: lib/block_scout_web/templates/transaction/_pending_tile.html.eex:20
#: lib/block_scout_web/templates/transaction/_tile.html.eex:33
#: lib/block_scout_web/templates/transaction/overview.html.eex:215
#: lib/block_scout_web/templates/transaction/overview.html.eex:279
#: lib/block_scout_web/views/wei_helpers.ex:78
msgid "Ether"
>>>>>>> 7148d5f99 (ERC-1155 finalization)
msgstr "" msgstr ""
#, elixir-format #, elixir-format
@ -860,14 +802,8 @@ msgid "Copy Txn Hex Input"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
<<<<<<< HEAD
#: lib/block_scout_web/templates/transaction/overview.html.eex:466 #: lib/block_scout_web/templates/transaction/overview.html.eex:466
msgid "Copy Txn UTF-8 Input" msgid "Copy Txn UTF-8 Input"
=======
#: lib/block_scout_web/templates/transaction/_emission_reward_tile.html.eex:8
#: lib/block_scout_web/views/transaction_view.ex:311
msgid "Success"
>>>>>>> 7148d5f99 (ERC-1155 finalization)
msgstr "" msgstr ""
#, elixir-format #, elixir-format
@ -976,7 +912,6 @@ msgid "Decompiled contract code"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
<<<<<<< HEAD
#: lib/block_scout_web/templates/address_decompiled_contract/index.html.eex:10 #: lib/block_scout_web/templates/address_decompiled_contract/index.html.eex:10
msgid "Decompiler version" msgid "Decompiler version"
msgstr "" msgstr ""
@ -984,29 +919,6 @@ msgstr ""
#, elixir-format #, elixir-format
#: lib/block_scout_web/views/internal_transaction_view.ex:23 #: lib/block_scout_web/views/internal_transaction_view.ex:23
msgid "Delegate Call" msgid "Delegate Call"
=======
#:
#: lib/block_scout_web/templates/common_components/_token_transfer_type_display_name.html.eex:9
#: lib/block_scout_web/templates/common_components/_token_transfer_type_display_name.html.eex:11
#: lib/block_scout_web/views/transaction_view.ex:393
msgid "Token Transfer"
msgstr ""
#, elixir-format
#: lib/block_scout_web/templates/address/_tabs.html.eex:13
#: lib/block_scout_web/templates/address_token_transfer/index.html.eex:17
#: lib/block_scout_web/templates/tokens/instance/overview/_tabs.html.eex:3
#: lib/block_scout_web/templates/tokens/instance/transfer/index.html.eex:16
#: lib/block_scout_web/templates/tokens/overview/_tabs.html.eex:3
#: lib/block_scout_web/templates/tokens/transfer/index.html.eex:14
#: lib/block_scout_web/templates/transaction/_tabs.html.eex:4
#: lib/block_scout_web/templates/transaction_token_transfer/index.html.eex:7
#: lib/block_scout_web/views/address_view.ex:346
#: lib/block_scout_web/views/tokens/instance/overview_view.ex:125
#: lib/block_scout_web/views/tokens/overview_view.ex:41
#: lib/block_scout_web/views/transaction_view.ex:454
msgid "Token Transfers"
>>>>>>> 7148d5f99 (ERC-1155 finalization)
msgstr "" msgstr ""
#, elixir-format #, elixir-format
@ -1022,10 +934,14 @@ msgstr ""
#, elixir-format #, elixir-format
<<<<<<< HEAD <<<<<<< HEAD
<<<<<<< HEAD
#: lib/block_scout_web/templates/stakes/_stakes_modal_pool_info.html.eex:53 #: lib/block_scout_web/templates/stakes/_stakes_modal_pool_info.html.eex:53
msgid "Delegators’ Staked Amount" msgid "Delegators’ Staked Amount"
======= =======
#: lib/block_scout_web/templates/address_logs/_logs.html.eex:18 #: lib/block_scout_web/templates/address_logs/_logs.html.eex:18
=======
#: lib/block_scout_web/templates/address_logs/_logs.html.eex:25
>>>>>>> 03b4f4b7d (ERC-1155: token balanaces indexer update)
#: lib/block_scout_web/views/transaction_view.ex:403 #: lib/block_scout_web/views/transaction_view.ex:403
msgid "Transaction" msgid "Transaction"
>>>>>>> 7148d5f99 (ERC-1155 finalization) >>>>>>> 7148d5f99 (ERC-1155 finalization)
@ -3365,7 +3281,7 @@ msgid "string"
msgid " Token Creation" msgid " Token Creation"
msgstr "" msgstr ""
#, elixir-format, fuzzy #, elixir-format
#: lib/block_scout_web/views/transaction_view.ex:197 #: lib/block_scout_web/views/transaction_view.ex:197
msgid "ERC-1155 " msgid "ERC-1155 "
msgstr "" msgstr ""
@ -3388,6 +3304,7 @@ msgid "validator"
msgstr "" msgstr ""
#, elixir-format #, elixir-format
#:
#: lib/block_scout_web/templates/common_components/_token_transfer_type_display_name.html.eex:7 #: lib/block_scout_web/templates/common_components/_token_transfer_type_display_name.html.eex:7
#: lib/block_scout_web/views/transaction_view.ex:392 #: lib/block_scout_web/views/transaction_view.ex:392
msgid "Token Creation" msgid "Token Creation"

@ -3,6 +3,23 @@ defmodule Explorer.Repo.Migrations.AddressCurrentTokenBalancesAddTokenIdToUnique
def change do def change do
drop(unique_index(:address_current_token_balances, ~w(address_hash token_contract_address_hash)a)) drop(unique_index(:address_current_token_balances, ~w(address_hash token_contract_address_hash)a))
create(unique_index(:address_current_token_balances, ~w(address_hash token_contract_address_hash token_id)a))
create(
unique_index(
:address_current_token_balances,
~w(address_hash token_contract_address_hash token_id)a,
name: :fetched_current_token_balances_with_token_id,
where: "token_id IS NOT NULL"
)
)
create(
unique_index(
:address_current_token_balances,
~w(address_hash token_contract_address_hash)a,
name: :fetched_current_token_balances,
where: "token_id IS NULL"
)
)
end end
end end

@ -3,6 +3,23 @@ defmodule Explorer.Repo.Migrations.AddressTokenBalancesAddTokenIdToUniqueIndex d
def change do def change do
drop(unique_index(:address_token_balances, ~w(address_hash token_contract_address_hash block_number)a)) drop(unique_index(:address_token_balances, ~w(address_hash token_contract_address_hash block_number)a))
create(unique_index(:address_token_balances, ~w(address_hash token_contract_address_hash token_id block_number)a))
create(
unique_index(
:address_token_balances,
~w(address_hash token_contract_address_hash token_id block_number)a,
name: :fetched_token_balances_with_token_id,
where: "token_id IS NOT NULL"
)
)
create(
unique_index(
:address_token_balances,
~w(address_hash token_contract_address_hash block_number)a,
name: :fetched_token_balances,
where: "token_id IS NULL"
)
)
end end
end end

@ -14,9 +14,18 @@ defmodule Explorer.Repo.Migrations.AddressTokenBalancesChangeUnfetchedTokenBalan
create( create(
unique_index( unique_index(
:address_token_balances, :address_token_balances,
~w(address_hash token_contract_address_hash token_id block_number)a, ~w(address_hash token_contract_address_hash block_number)a,
name: :unfetched_token_balances, name: :unfetched_token_balances,
where: "value_fetched_at IS NULL" where: "value_fetched_at IS NULL and token_id IS NULL"
)
)
create(
unique_index(
:address_token_balances,
~w(address_hash token_contract_address_hash token_id block_number)a,
name: :unfetched_token_balances_with_token_id,
where: "value_fetched_at IS NULL and token_id IS NOT NULL"
) )
) )
end end

@ -72,7 +72,7 @@ defmodule Explorer.Chain.Import.Runner.Address.TokenBalancesTest do
token_contract_address_hash: token_contract_address_hash, token_contract_address_hash: token_contract_address_hash,
value: nil, value: nil,
value_fetched_at: value_fetched_at, value_fetched_at: value_fetched_at,
token_id: 11, token_id: nil,
token_type: "ERC-20" token_type: "ERC-20"
} }
@ -101,6 +101,58 @@ defmodule Explorer.Chain.Import.Runner.Address.TokenBalancesTest do
end end
end end
test "does not nillifies existing value ERC-1155" 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,
token_id: 11,
token_type: "ERC-1155"
}
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)
end
defp run_changes(changes, options) when is_map(changes) do defp run_changes(changes, options) when is_map(changes) do
run_changes_list([changes], options) run_changes_list([changes], options)
end end

@ -177,7 +177,7 @@ defmodule Indexer.Fetcher.TokenBalanceTest do
assert {:ok, _} = Explorer.Chain.hash_to_address(address_hash) assert {:ok, _} = Explorer.Chain.hash_to_address(address_hash)
end end
test "import the token balances and return :ok when there are multiple balances for the same address on the batch" do test "import the token balances and return :ok when there are multiple balances for the same address on the batch (ERC-20)" do
contract = insert(:token) contract = insert(:token)
contract2 = insert(:token) contract2 = insert(:token)
insert(:block, number: 19999) insert(:block, number: 19999)
@ -187,15 +187,40 @@ defmodule Indexer.Fetcher.TokenBalanceTest do
address_hash: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", address_hash: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
block_number: 19999, block_number: 19999,
token_contract_address_hash: to_string(contract.contract_address_hash), token_contract_address_hash: to_string(contract.contract_address_hash),
token_id: 11, token_id: nil,
token_type: "ERC-20" token_type: "ERC-20"
}, },
%{ %{
address_hash: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", address_hash: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
block_number: 19999, block_number: 19999,
token_contract_address_hash: to_string(contract2.contract_address_hash), token_contract_address_hash: to_string(contract2.contract_address_hash),
token_id: nil,
token_type: "ERC-20"
}
]
assert TokenBalance.import_token_balances(token_balances_params) == :ok
end
test "import the token balances and return :ok when there are multiple balances for the same address on the batch (ERC-1155)" do
contract = insert(:token)
contract2 = insert(:token)
insert(:block, number: 19999)
token_balances_params = [
%{
address_hash: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
block_number: 19999,
token_contract_address_hash: to_string(contract.contract_address_hash),
token_id: 11, token_id: 11,
token_type: "ERC-20" token_type: "ERC-20"
},
%{
address_hash: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
block_number: 19999,
token_contract_address_hash: to_string(contract2.contract_address_hash),
token_id: 11,
token_type: "ERC-1155"
} }
] ]

Loading…
Cancel
Save