diff --git a/app/models/suggest_query.rb b/app/models/suggest_query.rb index d53e613ef..84afbec93 100644 --- a/app/models/suggest_query.rb +++ b/app/models/suggest_query.rb @@ -55,7 +55,16 @@ def find_udt_by_type_hash UdtSerializer.new(udt) if udt.present? end + def find_type_script_by_type_id + type_script = TypeScript.find_by(script_hash: query_key) + { data: { args: type_script.args } } if type_script.present? + end + def find_by_hex - Block.cached_find(query_key) || find_ckb_transaction_by_hash || find_address_by_lock_hash || find_udt_by_type_hash + Block.cached_find(query_key) || + find_ckb_transaction_by_hash || + find_address_by_lock_hash || + find_udt_by_type_hash || + find_type_script_by_type_id end end diff --git a/test/controllers/api/v1/suggest_queries_controller_test.rb b/test/controllers/api/v1/suggest_queries_controller_test.rb index c6bec9e09..3adcec79d 100644 --- a/test/controllers/api/v1/suggest_queries_controller_test.rb +++ b/test/controllers/api/v1/suggest_queries_controller_test.rb @@ -185,6 +185,18 @@ class SuggestQueriesControllerTest < ActionDispatch::IntegrationTest assert_equal response_json, response.body end + + test "should return a type_script when query key is a exist script hash" do + type_script = create( + :type_script, + code_hash: Settings.type_id_code_hash, + script_hash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8" + ) + + valid_get api_v1_suggest_queries_url, params: { q: type_script.script_hash } + + assert_equal type_script.args, json.dig("data", "args") + end end end end