@@ -4,8 +4,7 @@ module CellDataComparator
44 private
55
66 def compare_cells ( transaction )
7- combine_transfers ( transaction ) . map do |address_id , transfers |
8- address = Address . find_by ( id : address_id )
7+ combine_transfers ( transaction ) . map do |address , transfers |
98 { address : address . address_hash , transfers : }
109 end
1110 end
@@ -26,8 +25,13 @@ def combine_transfers(transaction)
2625
2726 def diff_normal_cells ( inputs , outputs )
2827 transfers = Hash . new { |h , k | h [ k ] = Array . new }
29- inputs = inputs . normal . group ( :address_id ) . sum ( :capacity )
30- outputs = outputs . normal . group ( :address_id ) . sum ( :capacity )
28+ inputs = inputs . find_all { |i | i . cell_type . to_s == "normal" } . group_by ( &:address ) . transform_values do |items |
29+ items . sum { |item | item [ :capacity ] }
30+ end
31+
32+ outputs = outputs . find_all { |i | i . cell_type . to_s == "normal" } . group_by ( &:address ) . transform_values do |items |
33+ items . sum { |item | item [ :capacity ] }
34+ end
3135
3236 ( inputs . keys | outputs . keys ) . each do |k |
3337 capacity = outputs [ k ] . to_f - inputs [ k ] . to_f
@@ -42,7 +46,7 @@ def diff_udt_cells(inputs, outputs)
4246 udt_infos = Hash . new { |h , k | h [ k ] = nil }
4347
4448 process_udt = -> ( c , h ) {
45- info = Udt . find_by ( type_hash : c . type_hash , published : true )
49+ info = c . udt_cell
4650 unless udt_infos [ c . type_hash ]
4751 udt_infos [ c . type_hash ] = {
4852 symbol : info &.symbol ,
@@ -51,15 +55,16 @@ def diff_udt_cells(inputs, outputs)
5155 }
5256 end
5357
54- k = [ c . address_id , c . type_hash , c . cell_type ]
58+ k = [ c . address , c . type_hash , c . cell_type ]
5559 h [ k ] ||= { capacity : 0.0 , amount : 0.0 }
5660 h [ k ] [ :capacity ] += c . capacity
5761 h [ k ] [ :amount ] += c . udt_amount . to_f
5862 }
5963
6064 cell_types = %w( udt omiga_inscription xudt xudt_compatible )
61- inputs = inputs . where ( cell_type : cell_types ) . each_with_object ( { } ) { |c , h | process_udt . call ( c , h ) }
62- outputs = outputs . where ( cell_type : cell_types ) . each_with_object ( { } ) { |c , h | process_udt . call ( c , h ) }
65+
66+ inputs = inputs . find_all { |i | cell_types . include? ( i . cell_type . to_s ) } . each_with_object ( { } ) { |c , h | process_udt . call ( c , h ) }
67+ outputs = outputs . find_all { |i | cell_types . include? ( i . cell_type . to_s ) } . each_with_object ( { } ) { |c , h | process_udt . call ( c , h ) }
6368
6469 ( inputs . keys | outputs . keys ) . each do |k |
6570 input = inputs [ k ]
@@ -77,8 +82,14 @@ def diff_udt_cells(inputs, outputs)
7782 def diff_dao_capacities ( inputs , outputs )
7883 transfers = Hash . new { |h , k | h [ k ] = Array . new }
7984 cell_types = %w( nervos_dao_deposit nervos_dao_withdrawing )
80- inputs = inputs . where ( cell_type : cell_types ) . group ( :address_id , :cell_type ) . sum ( :capacity )
81- outputs = outputs . where ( cell_type : cell_types ) . group ( :address_id , :cell_type ) . sum ( :capacity )
85+
86+ inputs = inputs . find_all { |i | cell_types . include? ( i . cell_type . to_s ) } . group_by { |i | [ i . address , i . cell_type ] } . transform_values do |items |
87+ items . sum { |item | item [ :capacity ] }
88+ end
89+
90+ outputs = outputs . find_all { |i | cell_types . include? ( i . cell_type . to_s ) } . group_by { |i | [ i . address , i . cell_type ] } . transform_values do |items |
91+ items . sum { |item | item [ :capacity ] }
92+ end
8293
8394 ( inputs . keys | outputs . keys ) . each do |k |
8495 capacity = outputs [ k ] . to_f - inputs [ k ] . to_f
@@ -90,8 +101,14 @@ def diff_dao_capacities(inputs, outputs)
90101
91102 def diff_cota_nft_cells ( transaction , inputs , outputs )
92103 transfers = Hash . new { |h , k | h [ k ] = Array . new }
93- inputs = inputs . cota_regular . group ( :address_id ) . sum ( :capacity )
94- outputs = outputs . cota_regular . group ( :address_id ) . sum ( :capacity )
104+
105+ inputs = inputs . find_all { |i | i . cell_type . to_s == 'cota_regular' } . group_by ( &:address ) . transform_values do |items |
106+ items . sum { |item | item [ :capacity ] }
107+ end
108+
109+ outputs = outputs . find_all { |i | i . cell_type . to_s == 'cota_regular' } . group_by ( &:address ) . transform_values do |items |
110+ items . sum { |item | item [ :capacity ] }
111+ end
95112
96113 ( inputs . keys | outputs . keys ) . each do |k |
97114 capacity = outputs [ k ] . to_f - inputs [ k ] . to_f
@@ -108,7 +125,7 @@ def diff_normal_nft_cells(inputs, outputs)
108125 m_nft_class nrc_721_factory cota_registry spore_cluster )
109126
110127 process_nft = -> ( c , h , o ) {
111- k = [ c . address_id , c . cell_type , c . type_hash ]
128+ k = [ c . address , c . cell_type , c . type_hash ]
112129 h [ k ] ||= { capacity : 0.0 , count : 0 }
113130 h [ k ] [ :capacity ] += c . capacity
114131 h [ k ] [ :count ] += o
@@ -117,18 +134,18 @@ def diff_normal_nft_cells(inputs, outputs)
117134 nft_infos [ c . type_hash ] = nft_info ( c )
118135 end
119136 }
120- inputs = inputs . where ( cell_type : cell_types ) . each_with_object ( { } ) { |c , h | process_nft . call ( c , h , -1 ) }
121- outputs = outputs . where ( cell_type : cell_types ) . each_with_object ( { } ) { |c , h | process_nft . call ( c , h , 1 ) }
137+ inputs = inputs . find_all { | i | cell_types . include? ( i . cell_type . to_s ) } . each_with_object ( { } ) { |c , h | process_nft . call ( c , h , -1 ) }
138+ outputs = outputs . find_all { | i | cell_types . include? ( i . cell_type . to_s ) } . each_with_object ( { } ) { |c , h | process_nft . call ( c , h , 1 ) }
122139
123140 ( inputs . keys | outputs . keys ) . each do |k |
124- address_id , cell_type , type_hash = k
141+ address , cell_type , type_hash = k
125142 input = inputs [ k ]
126143 output = outputs [ k ]
127144 capacity = output &.dig ( :capacity ) . to_f - input &.dig ( :capacity ) . to_f
128145 count = output &.dig ( :count ) . to_i + input &.dig ( :count ) . to_i
129146 transfer = { capacity :, cell_type :, count : }
130147 transfer . merge! ( nft_infos [ type_hash ] ) if nft_infos [ type_hash ]
131- transfers [ address_id ] << CkbUtils . hash_value_to_s ( transfer )
148+ transfers [ address ] << CkbUtils . hash_value_to_s ( transfer )
132149 end
133150
134151 transfers
@@ -157,7 +174,7 @@ def nft_info(cell)
157174 end
158175 end
159176
160- def cota_info ( transaction , address_id )
177+ def cota_info ( transaction , address )
161178 info = Array . new
162179 process_transfer = -> ( item , count ) {
163180 collection = item . collection
@@ -171,8 +188,8 @@ def cota_info(transaction, address_id)
171188 }
172189
173190 transaction . token_transfers . each do |t |
174- process_transfer . call ( t . item , -1 ) if t . from_id == address_id
175- process_transfer . call ( t . item , 1 ) if t . to_id == address_id
191+ process_transfer . call ( t . item , -1 ) if t . from_id == address . id
192+ process_transfer . call ( t . item , 1 ) if t . to_id == address . id
176193 end
177194
178195 info
0 commit comments