Merge pull request #619 from poanetwork/614
Fix uniqueness/on_conflict for address_token_balancespull/613/head
commit
dbeb871f27
@ -0,0 +1,39 @@ |
||||
defmodule Indexer.Address.TokenBalances do |
||||
@moduledoc """ |
||||
Extracts `Explorer.Address.TokenBalance` params from other schema's params. |
||||
""" |
||||
|
||||
def params_set(%{} = import_options) do |
||||
Enum.reduce(import_options, MapSet.new(), &reducer/2) |
||||
end |
||||
|
||||
defp reducer({:token_transfers_params, token_transfers_params}, initial) when is_list(token_transfers_params) do |
||||
Enum.reduce(token_transfers_params, initial, fn %{ |
||||
block_number: block_number, |
||||
from_address_hash: from_address_hash, |
||||
to_address_hash: to_address_hash, |
||||
token_contract_address_hash: token_contract_address_hash |
||||
}, |
||||
acc |
||||
when is_integer(block_number) and is_binary(from_address_hash) and |
||||
is_binary(to_address_hash) and |
||||
is_binary(token_contract_address_hash) -> |
||||
acc |
||||
|> MapSet.put(%{ |
||||
address_hash: from_address_hash, |
||||
token_contract_address_hash: token_contract_address_hash, |
||||
block_number: block_number |
||||
}) |
||||
|> MapSet.put(%{ |
||||
address_hash: to_address_hash, |
||||
token_contract_address_hash: token_contract_address_hash, |
||||
block_number: block_number |
||||
}) |
||||
|> MapSet.put(%{ |
||||
address_hash: token_contract_address_hash, |
||||
token_contract_address_hash: token_contract_address_hash, |
||||
block_number: block_number |
||||
}) |
||||
end) |
||||
end |
||||
end |
@ -0,0 +1,38 @@ |
||||
defmodule Indexer.Address.TokenBalancesTest do |
||||
use ExUnit.Case, async: true |
||||
|
||||
alias Explorer.Factory |
||||
alias Indexer.Address.TokenBalances |
||||
|
||||
describe "params_set/1" do |
||||
test "with token transfer extract from_address, to_address, and token_contract_address_hash" do |
||||
block_number = 1 |
||||
|
||||
from_address_hash = |
||||
Factory.address_hash() |
||||
|> to_string() |
||||
|
||||
to_address_hash = |
||||
Factory.address_hash() |
||||
|> to_string() |
||||
|
||||
token_contract_address_hash = |
||||
Factory.address_hash() |
||||
|> to_string() |
||||
|
||||
token_transfer_params = %{ |
||||
block_number: block_number, |
||||
from_address_hash: from_address_hash, |
||||
to_address_hash: to_address_hash, |
||||
token_contract_address_hash: token_contract_address_hash |
||||
} |
||||
|
||||
params_set = TokenBalances.params_set(%{token_transfers_params: [token_transfer_params]}) |
||||
|
||||
assert MapSet.size(params_set) == 3 |
||||
assert %{address_hash: from_address_hash, block_number: block_number} |
||||
assert %{address_hash: to_address_hash, block_number: block_number} |
||||
assert %{address_hash: token_contract_address_hash, block_number: block_number} |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue