@@ -8,20 +8,15 @@ class LiveCells < ActiveInteraction::Base
88 string :sort , default : "block_timestamp.desc"
99 integer :page , default : 1
1010 integer :page_size , default : CellOutput . default_per_page
11+ string :tag , default : nil
1112
1213 def execute
1314 address = Explore . run! ( key :)
1415 raise AddressNotFoundError if address . is_a? ( NullAddress )
1516
1617 order_by , asc_or_desc = live_cells_ordering
17- if bound_status
18- bitcoin_vouts = BitcoinVout . where ( address_id : address . map ( &:id ) , status : bound_status )
19- records = CellOutput . live . where ( id : bitcoin_vouts . map ( &:cell_output_id ) ) . order ( order_by => asc_or_desc ) .
20- page ( page ) . per ( page_size ) . fast_page
21- else
22- records = CellOutput . live . where ( address_id : address . map ( &:id ) ) . order ( order_by => asc_or_desc ) .
23- page ( page ) . per ( page_size ) . fast_page
24- end
18+ records = fetch_cell_output_scope ( address )
19+ records = records . order ( order_by => asc_or_desc ) . page ( page ) . per ( page_size ) . fast_page
2520
2621 options = FastJsonapi ::PaginationMetaGenerator . new ( request :, records :, page :, page_size :) . call
2722 CellOutputSerializer . new ( records , options ) . serialized_json
@@ -37,5 +32,42 @@ def live_cells_ordering
3732
3833 [ sort_by , sort_order ]
3934 end
35+
36+ def fetch_cell_output_scope ( address )
37+ address_ids = address . map ( &:id )
38+
39+ scope =
40+ if bound_status
41+ vout_ids = BitcoinVout . where ( address_id : address_ids , status : bound_status ) . pluck ( :cell_output_id )
42+ CellOutput . live . where ( id : vout_ids )
43+ else
44+ CellOutput . live . where ( address_id : address_ids )
45+ end
46+
47+ tag . present? ? filter_by_tag ( scope ) : scope
48+ end
49+
50+ def filter_by_tag ( scope )
51+ case tag
52+ when "fiber"
53+ lock_script_ids = scope . where . not ( lock_script_id : nil ) . distinct . pluck ( :lock_script_id )
54+ filtered_ids = LockScript . where ( id : lock_script_ids , code_hash : Settings . fiber_funding_code_hash ) . pluck ( :id )
55+ scope . where ( lock_script_id : filtered_ids )
56+ when "multisig"
57+ lock_script_ids = scope . where . not ( lock_script_id : nil ) . distinct . pluck ( :lock_script_id )
58+ filtered_ids = LockScript . where ( id : lock_script_ids ) . where (
59+ "(code_hash = ? AND hash_type = ?) OR (code_hash = ? AND hash_type = ?)" ,
60+ Settings . multisig_code_hash , "data1" ,
61+ Settings . secp_multisig_cell_type_hash , "type"
62+ ) . pluck ( :id )
63+ scope . where ( lock_script_id : filtered_ids )
64+ when "deployment"
65+ scope_ids = scope . pluck ( :id )
66+ matched_ids = Contract . where ( deployed_cell_output_id : scope_ids ) . pluck ( :deployed_cell_output_id )
67+ scope . where ( id : matched_ids )
68+ else
69+ CellOutput . none
70+ end
71+ end
4072 end
4173end
0 commit comments