diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 5870a10941..a53a2f634f 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -628,11 +628,6 @@ defmodule Explorer.Chain do iex> {:ok, %Explorer.Chain.Address{hash: found_hash}} = Explorer.Chain.hash_to_address(hash) iex> found_hash == hash true - - Returns `:error` if it cannot process the value passed in. - iex> :error = Explorer.Chain.hash_to_address(:not_a_hash) - :error - """ @spec find_or_insert_address_from_hash(Hash.Address.t()) :: {:ok, Address.t()} def find_or_insert_address_from_hash(%Hash{byte_count: unquote(Hash.Address.byte_count())} = hash) do diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 705a4b39bf..ab27c9ca8b 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -697,6 +697,37 @@ defmodule Explorer.ChainTest do end end + describe "hash_to_address/1" do + test "returns not found if the address doesn't exist" do + hash_str = "0xcbbcd5ac86f9a50e13313633b262e16f695a90c2" + {:ok, hash} = Chain.string_to_address_hash(hash_str) + + assert {:error, :not_found} = Chain.hash_to_address(hash) + end + + test "returns the correct address if it exists" do + address = insert(:address) + + assert {:ok, address} = Chain.hash_to_address(address.hash) + end + end + + describe "find_or_insert_address_from_hash/1" do + test "returns an address if it already exists" do + address = insert(:address) + + assert {:ok, address} = Chain.find_or_insert_address_from_hash(address.hash) + end + + test "returns an address if it doesn't exist" do + hash_str = "0xcbbcd5ac86f9a50e13313633b262e16f695a90c2" + {:ok, hash} = Chain.string_to_address_hash(hash_str) + + assert {:ok, %Chain.Address{hash: hash}} = + Chain.find_or_insert_address_from_hash(hash) + end + end + describe "hashes_to_transactions/2" do test "with transaction with block required without block returns nil" do [%Transaction{hash: hash_with_block1}, %Transaction{hash: hash_with_block2}] =