@@ -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
@@ -480,22 +496,41 @@ def phase1_dao_interests
480496 @phase1_dao_interests ||=
481497 begin
482498 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 )
499+ phase1_cells . each do |nervos_dao_withdrawing_cell |
500+ puts nervos_dao_withdrawing_cell . attributes
486501 total += CkbUtils . dao_interest ( nervos_dao_withdrawing_cell )
487502 end
488503 total
489504 end
490505 end
491506
507+ def phase1_cells
508+ return @phase1_cells if @phase1_cells
509+ events = DaoEvent . processed . withdraw_from_dao . created_before ( ended_at ) . unconsumed_at ( ended_at ) . select ( :ckb_transaction_id , :cell_index ) . all
510+ if events . blank?
511+ return @phase1_cells = [ ]
512+ end
513+
514+ pairs = events . map { |e | [ e . ckb_transaction_id , e . cell_index ] }
515+ conditions = pairs . map do |tx_id , cell_idx |
516+ [ "ckb_transaction_id = #{ tx_id } AND cell_index = #{ cell_idx } " ]
517+ end
518+ query = conditions . map do |cond |
519+ "(#{ cond [ 0 ] } )"
520+ end . join ( " OR " )
521+ @phase1_cells = CellOutput . where ( query ) . to_a
522+ end
523+
524+ def unmaed_cells
525+ @unmaed_cells ||= DaoEvent . includes ( :cell_output ) . processed . deposit_to_dao . created_before ( ended_at ) . unconsumed_at ( ended_at ) . to_a
526+ end
527+
492528 def unmade_dao_interests
493529 @unmade_dao_interests ||=
494530 begin
495531 tip_dao = current_tip_block . dao
496532 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 |
533+ unmaed_cells . each do |dao_event |
499534 total += DaoCompensationCalculator . new ( dao_event . cell_output , tip_dao ) . call
500535 end
501536 total
0 commit comments