Skip to content

Commit

Permalink
feat: query script by code_hash (#1689)
Browse files Browse the repository at this point in the history
Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid authored Mar 19, 2024
1 parent 86f3473 commit d66a517
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/models/suggest_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,24 @@ def find_type_script_by_type_id
TypeScriptSerializer.new(type_script) if type_script.present?
end

def find_lock_script_by_code_hash
lock_script = LockScript.find_by(code_hash: query_key)
LockScriptSerializer.new(lock_script) if lock_script.present?
end

def find_type_script_by_code_hash
type_script = TypeScript.find_by(code_hash: query_key)
TypeScriptSerializer.new(type_script) 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 ||
find_type_script_by_type_id ||
find_type_script_by_code_hash ||
find_lock_script_by_code_hash ||
find_bitcoin_transaction_by_txid
end

Expand Down
10 changes: 10 additions & 0 deletions test/models/suggest_query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ class SuggestQueryTest < ActiveSupport::TestCase
SuggestQuery.new(udt.type_hash).find!
end
end

test "should return lock script by code_hash" do
ls = create(:lock_script)
assert_equal LockScriptSerializer.new(ls).serialized_json, SuggestQuery.new(ls.code_hash).find!.serialized_json
end

test "should return type script by code_hash" do
ts = create(:type_script)
assert_equal TypeScriptSerializer.new(ts).serialized_json, SuggestQuery.new(ts.code_hash).find!.serialized_json
end
end

0 comments on commit d66a517

Please sign in to comment.