From 77e8f457e006a0f151a67050b61ba844764e1b05 Mon Sep 17 00:00:00 2001 From: zachdaniel Date: Wed, 29 May 2019 14:06:23 -0400 Subject: [PATCH] fix: uniq by hash, instead of transaction --- CHANGELOG.md | 1 + apps/explorer/lib/explorer/chain.ex | 2 +- .../lib/explorer/chain/token_transfer.ex | 60 +++---------------- 3 files changed, 11 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 775c006a5c..84592d868b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ - [#2008](https://github.com/poanetwork/blockscout/pull/2008) - add new function clause for xDai network beneficiaries - [#2009](https://github.com/poanetwork/blockscout/pull/2009) - addresses page improvements - [#2027](https://github.com/poanetwork/blockscout/pull/2027) - fix: `BlocksTransactionsMismatch` ignoring blocks without transactions +- [#2062](https://github.com/poanetwork/blockscout/pull/2062) - fix: uniq by hash, instead of transaction ### Chore diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 9446388531..cc2375c2ef 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -266,7 +266,7 @@ defmodule Explorer.Chain do queries |> Stream.flat_map(&Repo.all/1) - |> Stream.uniq() + |> Stream.uniq_by(& &1.hash) |> Stream.concat(rewards_list) |> Enum.sort_by(fn item -> case item do diff --git a/apps/explorer/lib/explorer/chain/token_transfer.ex b/apps/explorer/lib/explorer/chain/token_transfer.ex index fa7ba99347..905d576aa1 100644 --- a/apps/explorer/lib/explorer/chain/token_transfer.ex +++ b/apps/explorer/lib/explorer/chain/token_transfer.ex @@ -204,59 +204,17 @@ defmodule Explorer.Chain.TokenTransfer do transaction_hashes_from_token_transfers_sql(address_bytes, paging_options) end - defp transaction_hashes_from_token_transfers_sql(address_bytes, %PagingOptions{key: nil, page_size: page_size}) do - {:ok, %Postgrex.Result{rows: transaction_hashes_from_token_transfers}} = - Repo.query( - """ - SELECT transaction_hash - FROM - ( - SELECT transaction_hash - FROM token_transfers - WHERE from_address_hash = $1 - - UNION - - SELECT transaction_hash - FROM token_transfers - WHERE to_address_hash = $1 - ) as token_transfers_transaction_hashes - LIMIT $2 - """, - [address_bytes, page_size] - ) - - List.flatten(transaction_hashes_from_token_transfers) - end - - defp transaction_hashes_from_token_transfers_sql(address_bytes, %PagingOptions{ - key: {block_number, _index}, - page_size: page_size - }) do - {:ok, %Postgrex.Result{rows: transaction_hashes_from_token_transfers}} = - Repo.query( - """ - SELECT transaction_hash - FROM - ( - SELECT transaction_hash - FROM token_transfers - WHERE from_address_hash = $1 - AND block_number < $2 - - UNION - - SELECT transaction_hash - FROM token_transfers - WHERE to_address_hash = $1 - AND block_number < $2 - ) as token_transfers_transaction_hashes - LIMIT $3 - """, - [address_bytes, block_number, page_size] + defp transaction_hashes_from_token_transfers_sql(address_bytes, %PagingOptions{page_size: page_size} = paging_options) do + query = + from(token_transfer in TokenTransfer, + where: token_transfer.to_address_hash == ^address_bytes or token_transfer.from_address_hash == ^address_bytes, + select: type(token_transfer.transaction_hash, :binary), + limit: ^page_size ) - List.flatten(transaction_hashes_from_token_transfers) + query + |> page_transaction_hashes_from_token_transfers(paging_options) + |> Repo.all() end defp page_transaction_hashes_from_token_transfers(query, %PagingOptions{key: nil}), do: query