Blocks import: divide token balance insertion with and without token_id

pull/8050/head
Viktor Baranov 4 years ago
parent 5f4313371a
commit 83ce2d3f9b
  1. 29
      apps/explorer/lib/explorer/chain/import/runner/blocks.ex

@ -467,22 +467,45 @@ defmodule Explorer.Chain.Import.Runner.Blocks do
] ]
) )
ordered_current_token_balance = current_token_balance =
new_current_token_balance_query new_current_token_balance_query
|> repo.all() |> repo.all()
ordered_current_token_balance_no_token_id =
current_token_balance
|> Enum.filter(&is_nil(&1.token_id))
# Enforce CurrentTokenBalance ShareLocks order (see docs: sharelocks.md)
|> Enum.sort_by(&{&1.token_contract_address_hash, &1.address_hash})
{_total, result_with_token_id} =
repo.insert_all(
Address.CurrentTokenBalance,
ordered_current_token_balance_no_token_id,
# No `ON CONFLICT` because `delete_address_current_token_balances`
# should have removed any conflicts.
#
returning: [:address_hash, :token_contract_address_hash, :block_number, :value],
timeout: timeout
)
ordered_current_token_balance_with_token_id =
current_token_balance
|> Enum.filter(&(!is_nil(&1.token_id)))
# Enforce CurrentTokenBalance ShareLocks order (see docs: sharelocks.md) # Enforce CurrentTokenBalance ShareLocks order (see docs: sharelocks.md)
|> Enum.sort_by(&{&1.token_contract_address_hash, &1.token_id, &1.address_hash}) |> Enum.sort_by(&{&1.token_contract_address_hash, &1.token_id, &1.address_hash})
{_total, result} = {_total, result_no_token_id} =
repo.insert_all( repo.insert_all(
Address.CurrentTokenBalance, Address.CurrentTokenBalance,
ordered_current_token_balance, ordered_current_token_balance_with_token_id,
# No `ON CONFLICT` because `delete_address_current_token_balances` # No `ON CONFLICT` because `delete_address_current_token_balances`
# should have removed any conflicts. # should have removed any conflicts.
returning: [:address_hash, :token_contract_address_hash, :block_number, :value], returning: [:address_hash, :token_contract_address_hash, :block_number, :value],
timeout: timeout timeout: timeout
) )
result = result_with_token_id ++ result_no_token_id
derived_address_current_token_balances = derived_address_current_token_balances =
Enum.map(result, &Map.take(&1, [:address_hash, :token_contract_address_hash, :block_number, :value])) Enum.map(result, &Map.take(&1, [:address_hash, :token_contract_address_hash, :block_number, :value]))

Loading…
Cancel
Save