@@ -27,10 +27,11 @@ def import
2727 cell_deps_attrs = [ ]
2828 witnesses_attrs = [ ]
2929 header_deps_attrs = [ ]
30+ input_account_books_attrs = [ ]
31+ output_account_books_attrs = [ ]
3032 lock_script_attrs = Set . new
3133 addresses_attrs = Set . new
3234 type_script_attrs = Set . new
33- account_books_attrs = Set . new
3435
3536 group_parsers . each do |parser |
3637 tx_attrs << parser . tx_attr
@@ -40,10 +41,10 @@ def import
4041 cell_deps_attrs . concat ( parser . cell_deps_attrs )
4142 header_deps_attrs . concat ( parser . header_deps_attrs )
4243 cell_inputs_attrs . concat ( parser . cell_inputs_attrs )
44+ output_account_books_attrs . concat ( parser . output_account_books_attrs )
4345 lock_script_attrs . merge ( parser . lock_script_attrs )
4446 addresses_attrs . merge ( parser . addresses_attrs )
4547 type_script_attrs . merge ( parser . type_script_attrs )
46- account_books_attrs . merge ( parser . account_books_attrs )
4748 end
4849 ApplicationRecord . transaction do
4950 tx_returnings = CkbTransaction . upsert_all ( tx_attrs , unique_by : %i[ tx_status tx_hash ] , returning : %i[ id tx_hash ] )
@@ -61,11 +62,7 @@ def import
6162 type_script_returnings = TypeScript . upsert_all ( type_script_attrs . to_a , unique_by : :script_hash , returning : %i[ id script_hash ] )
6263 type_script_mappings = type_script_returnings . rows . to_h { |id , script_hash | [ script_hash , id ] }
6364 end
64- new_account_books_attrs =
65- account_books_attrs . to_a . map do |attr |
66- { ckb_transaction_id : tx_mappings [ attr [ :tx_hash ] ] , address_id : address_mappings [ attr [ :lock_script_hash ] ] }
67- end
68- AccountBook . upsert_all ( new_account_books_attrs , unique_by : %i[ address_id ckb_transaction_id ] )
65+
6966 new_cell_outputs_attrs =
7067 cell_outputs_attrs . map do |attr |
7168 attr . merge ( { ckb_transaction_id : tx_mappings [ attr [ :tx_hash ] ] , lock_script_id : lock_script_mappings [ attr [ :lock_script_hash ] ] ,
@@ -100,25 +97,45 @@ def import
10097 input_conditions = cell_inputs_attrs . filter do |input |
10198 input [ :previous_tx_hash ] != CellOutput ::SYSTEM_TX_HASH
10299 end . map { |input | { tx_hash : input [ :previous_tx_hash ] , cell_index : input [ :previous_index ] } }
103- input_returnings = batch_query_outputs ( input_conditions , %i[ id cell_type tx_hash cell_index capacity ] )
104- input_mappings = input_returnings . to_h { |id , cell_type , tx_hash , cell_index | [ "#{ tx_hash } -#{ cell_index } " , " #{ cell_type } - #{ id } " ] }
100+ input_returnings = batch_query_outputs ( input_conditions , %i[ id cell_type tx_hash cell_index address_id capacity ] )
101+ input_mappings = input_returnings . to_h { |id , cell_type , tx_hash , cell_index , address_id , capacity | [ "#{ tx_hash } -#{ cell_index } " , { id : , cell_type : , address_id : , capacity : } ] }
105102 new_cell_inputs_attrs =
106103 cell_inputs_attrs . map do |attr |
107104 attr [ :ckb_transaction_id ] = tx_mappings [ attr [ :tx_hash ] ]
108105 if attr [ :previous_tx_hash ] != CellOutput ::SYSTEM_TX_HASH && input_mappings [ "#{ attr [ :previous_tx_hash ] } -#{ attr [ :previous_index ] } " ] . present?
109- cell_type , previous_cell_output_id = input_mappings [ "#{ attr [ :previous_tx_hash ] } -#{ attr [ :previous_index ] } " ] . split ( "-" )
110- attr [ :previous_cell_output_id ] = previous_cell_output_id
111- attr [ :cell_type ] = cell_type
106+ input_hash = input_mappings [ "#{ attr [ :previous_tx_hash ] } -#{ attr [ :previous_index ] } " ]
107+ attr [ :previous_cell_output_id ] = input_hash [ :id ]
108+ attr [ :cell_type ] = input_hash [ :cell_type ]
109+ input_account_books_attrs << { ckb_transaction_id : attr [ :ckb_transaction_id ] , address_id : input_hash [ :address_id ] , capacity : input_hash [ :capacity ] }
112110 end
113111 attr . except ( :tx_hash )
114112 end
115113 CellInput . upsert_all ( new_cell_inputs_attrs , unique_by : %i[ ckb_transaction_id index ] )
114+ new_output_account_books_attrs =
115+ output_account_books_attrs . map do |attr |
116+ { ckb_transaction_id : tx_mappings [ attr [ :tx_hash ] ] , address_id : address_mappings [ attr [ :lock_script_hash ] ] , capacity : attr [ :capacity ] }
117+ end
118+ account_books_attrs = calculate_income ( new_output_account_books_attrs , input_account_books_attrs )
119+ AccountBook . upsert_all ( account_books_attrs , unique_by : %i[ address_id ckb_transaction_id ] )
116120 end
117121 end
118122 end
119123
120124 private
121125
126+ def calculate_income ( outputs , inputs )
127+ grouped_outputs = outputs . group_by { |o | [ o [ :ckb_transaction_id ] , o [ :address_id ] ] }
128+ grouped_inputs = inputs . group_by { |i | [ i [ :ckb_transaction_id ] , i [ :address_id ] ] }
129+
130+ all_keys = ( grouped_outputs . keys + grouped_inputs . keys ) . uniq
131+
132+ all_keys . map do |key |
133+ out_cap = grouped_outputs [ key ] &.sum { |o | o [ :capacity ] } || 0
134+ in_cap = grouped_inputs [ key ] &.sum { |i | i [ :capacity ] } || 0
135+ { address_id : key [ 1 ] , ckb_transaction_id : key [ 0 ] , income : out_cap - in_cap }
136+ end
137+ end
138+
122139 def batch_query_outputs ( conditions , returnings = %i[ id cell_type tx_hash cell_index ] )
123140 relation = CellOutput . none
124141
0 commit comments