diff --git a/apps/block_scout_web/lib/block_scout_web/controllers/chain_controller.ex b/apps/block_scout_web/lib/block_scout_web/controllers/chain_controller.ex index 936d3ab0a0..037979155c 100644 --- a/apps/block_scout_web/lib/block_scout_web/controllers/chain_controller.ex +++ b/apps/block_scout_web/lib/block_scout_web/controllers/chain_controller.ex @@ -58,12 +58,21 @@ defmodule BlockScoutWeb.ChainController do if term == "" do json(conn, "{}") else - result = + result_tokens = term |> String.trim() |> Chain.search_token() - json(conn, result) + if result_tokens do + json(conn, result_tokens) + else + result_contracts = + term + |> String.trim() + |> Chain.search_contract() + + json(conn, result_contracts) + end end end diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 278317b8e7..f65b709ea0 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -850,6 +850,19 @@ defmodule Explorer.Chain do Repo.all(query) end + @spec search_contract(String.t()) :: [SmartContract.t()] + def search_contract(word) do + term = String.replace(word, ~r/\W/u, "") <> ":*" + + query = + from(token in SmartContract, + where: fragment("to_tsvector('english', name ) @@ to_tsquery(?)", ^term), + limit: 5 + ) + + Repo.all(query) + end + @doc """ Converts `t:Explorer.Chain.Address.t/0` `hash` to the `t:Explorer.Chain.Address.t/0` with that `hash`.