@@ -76,9 +76,23 @@ def liquidity
7676 else
7777 claimed_compensation_today = 0
7878
79- DaoEvent . processed . withdraw_from_dao . consumed_between ( started_at ,
80- ended_at ) . find_each do |dao_event |
81- nervos_dao_withdrawing_cell = CellOutput . find_by ( ckb_transaction_id : dao_event . ckb_transaction_id , cell_index : dao_event . cell_index )
79+ events = DaoEvent . processed . withdraw_from_dao . consumed_between ( started_at , ended_at ) . select ( :ckb_transaction_id , :cell_index ) . all
80+
81+ results = if events . blank?
82+ [ ]
83+ else
84+ pairs = events . map { |e | [ e . ckb_transaction_id , e . cell_index ] }
85+ conditions = pairs . map do |tx_id , cell_idx |
86+ [ "ckb_transaction_id = #{ tx_id } AND cell_index = #{ cell_idx } " ]
87+ end
88+ query = conditions . map do |cond |
89+ "(#{ cond [ 0 ] } )"
90+ end . join ( " OR " )
91+ CellOutput . where ( query )
92+ end
93+
94+
95+ results . each do |nervos_dao_withdrawing_cell |
8296 claimed_compensation_today += CkbUtils . dao_interest ( nervos_dao_withdrawing_cell )
8397 end
8498
@@ -92,13 +106,16 @@ def liquidity
92106 sum_interest_bearing = 0
93107 sum_uninterest_bearing = 0
94108
95- DaoEvent . processed . withdraw_from_dao . created_before ( ended_at ) . unconsumed_at ( ended_at ) . find_each do |nervos_dao_withdrawing_cell |
96- nervos_dao_deposit_cell = CellInput . find_by ( ckb_transaction_id : nervos_dao_withdrawing_cell . ckb_transaction_id , index : nervos_dao_withdrawing_cell . cell_index ) . previous_cell_output
97- interest_bearing_deposits += nervos_dao_deposit_cell . capacity
98- sum_interest_bearing += nervos_dao_deposit_cell . capacity * ( nervos_dao_withdrawing_cell . block_timestamp - nervos_dao_deposit_cell . block_timestamp ) / MILLISECONDS_IN_DAY
109+ phase1_cells . each do |nervos_dao_withdrawing_cell |
110+ interest_bearing_deposits += nervos_dao_withdrawing_cell . capacity
111+
112+ block_number = CKB ::Utils . hex_to_bin ( nervos_dao_withdrawing_cell . data ) . unpack ( "Q<" ) . pack ( "Q>" ) . unpack1 ( "H*" ) . hex
113+ nervos_dao_deposit_block_timestamp = Block . find_by_number ( block_number ) . timestamp
114+
115+ sum_interest_bearing += nervos_dao_withdrawing_cell . capacity * ( nervos_dao_withdrawing_cell . block_timestamp - nervos_dao_deposit_block_timestamp ) / MILLISECONDS_IN_DAY
99116 end
100117
101- DaoEvent . includes ( :cell_output ) . processed . deposit_to_dao . created_before ( ended_at ) . unconsumed_at ( ended_at ) . find_each do |dao_event |
118+ unmaed_cells . each do |dao_event |
102119 nervos_dao_deposit_cell = dao_event . cell_output
103120 uninterest_bearing_deposits += nervos_dao_deposit_cell . capacity
104121
@@ -139,8 +156,7 @@ def liquidity
139156 if from_scratch
140157 CellOutput . generated_before ( ended_at ) . unconsumed_at ( ended_at ) . count
141158 else
142- CellOutput . generated_between ( started_at , ended_at ) . count +
143- yesterday_daily_statistic . live_cells_count . to_i - dead_cells_count_today
159+ CellOutput . generated_between ( started_at , ended_at ) . count + yesterday_daily_statistic . live_cells_count . to_i - dead_cells_count_today
144160 end
145161 end
146162
@@ -188,19 +204,27 @@ def liquidity
188204 end
189205 end
190206
191- ranges . each_with_index . map do |range , index |
207+ counts = ranges . each_with_index . map do |range , index |
192208 begin_value = range [ 0 ] * ( 10 **8 )
193209 end_value = range [ 1 ] * ( 10 **8 )
194210 if index == max_n - 1
195211 addresses_count = Address . visible . where ( "balance > ?" , begin_value ) . count
196- total_addresses_count = Address . visible . where ( "balance > 0" ) . count
197212 else
198213 addresses_count = Address . visible . where ( "balance > ? and balance <= ?" , begin_value , end_value ) . count
199- total_addresses_count = Address . visible . where ( "balance > 0 and balance <= ?" , end_value ) . count
200214 end
201215
202- [ range [ 1 ] , addresses_count , total_addresses_count ]
216+ [ range [ 1 ] , addresses_count ]
203217 end
218+
219+ total_addresses_count = 0
220+
221+ results = [ ]
222+ counts . each do |c |
223+ total_addresses_count += c [ 1 ]
224+ results << [ c [ 0 ] , c [ 1 ] , total_addresses_count ]
225+ end
226+
227+ results
204228 end
205229
206230 define_logic :total_tx_fee do
@@ -480,22 +504,40 @@ def phase1_dao_interests
480504 @phase1_dao_interests ||=
481505 begin
482506 total = 0
483- DaoEvent . processed . withdraw_from_dao .
484- created_before ( ended_at ) . unconsumed_at ( ended_at ) . find_each do |dao_event |
485- nervos_dao_withdrawing_cell = CellOutput . find_by ( ckb_transaction_id : dao_event . ckb_transaction_id , cell_index : dao_event . cell_index )
507+ phase1_cells . each do |nervos_dao_withdrawing_cell |
486508 total += CkbUtils . dao_interest ( nervos_dao_withdrawing_cell )
487509 end
488510 total
489511 end
490512 end
491513
514+ def phase1_cells
515+ return @phase1_cells if @phase1_cells
516+ events = DaoEvent . processed . withdraw_from_dao . created_before ( ended_at ) . unconsumed_at ( ended_at ) . select ( :ckb_transaction_id , :cell_index ) . all
517+ if events . blank?
518+ return @phase1_cells = [ ]
519+ end
520+
521+ pairs = events . map { |e | [ e . ckb_transaction_id , e . cell_index ] }
522+ conditions = pairs . map do |tx_id , cell_idx |
523+ [ "ckb_transaction_id = #{ tx_id } AND cell_index = #{ cell_idx } " ]
524+ end
525+ query = conditions . map do |cond |
526+ "(#{ cond [ 0 ] } )"
527+ end . join ( " OR " )
528+ @phase1_cells = CellOutput . where ( query ) . to_a
529+ end
530+
531+ def unmaed_cells
532+ @unmaed_cells ||= DaoEvent . includes ( :cell_output ) . processed . deposit_to_dao . created_before ( ended_at ) . unconsumed_at ( ended_at ) . to_a
533+ end
534+
492535 def unmade_dao_interests
493536 @unmade_dao_interests ||=
494537 begin
495538 tip_dao = current_tip_block . dao
496539 total = 0
497- DaoEvent . includes ( :cell_output ) . processed . deposit_to_dao .
498- created_before ( ended_at ) . unconsumed_at ( ended_at ) . find_each do |dao_event |
540+ unmaed_cells . each do |dao_event |
499541 total += DaoCompensationCalculator . new ( dao_event . cell_output , tip_dao ) . call
500542 end
501543 total
0 commit comments