From 83ce2d3f9b8e66525a277e584331fcaef54c9b9e Mon Sep 17 00:00:00 2001 From: Viktor Baranov Date: Thu, 6 May 2021 13:08:02 +0300 Subject: [PATCH] Blocks import: divide token balance insertion with and without token_id --- .../explorer/chain/import/runner/blocks.ex | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/apps/explorer/lib/explorer/chain/import/runner/blocks.ex b/apps/explorer/lib/explorer/chain/import/runner/blocks.ex index acd419ce42..1abbf05932 100644 --- a/apps/explorer/lib/explorer/chain/import/runner/blocks.ex +++ b/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 |> 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) |> Enum.sort_by(&{&1.token_contract_address_hash, &1.token_id, &1.address_hash}) - {_total, result} = + {_total, result_no_token_id} = repo.insert_all( Address.CurrentTokenBalance, - ordered_current_token_balance, + ordered_current_token_balance_with_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 ) + result = result_with_token_id ++ result_no_token_id + derived_address_current_token_balances = Enum.map(result, &Map.take(&1, [:address_hash, :token_contract_address_hash, :block_number, :value]))