From d4798b7223ef176756b9bd9c9c3ee2899a648f2e Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Fri, 1 Nov 2019 21:04:24 +0300 Subject: [PATCH] Find by contract name --- .../block_scout_web/controllers/chain_controller.ex | 13 +++++++++++-- apps/explorer/lib/explorer/chain.ex | 13 +++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) 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`.