Skip to content

Commit 503f533

Browse files
authored
Merge pull request #2883 from nervosnetwork/develop
Deploy to testnet
2 parents cbbba47 + 94bc04d commit 503f533

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

app/controllers/api/v2/ckb_transactions_controller.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,18 @@ def display_outputs
6464
def rgb_digest
6565
expires_in 10.seconds, public: true, must_revalidate: true
6666

67-
@ckb_transaction = CkbTransaction.includes(:cell_inputs => [:previous_cell_output], :cell_outputs => {}).find_by(tx_hash: params[:id])
67+
includes = {
68+
cell_outputs: [:lock_script, address: [bitcoin_vout: [:bitcoin_address]]],
69+
input_cells: [:lock_script, address: [bitcoin_vout: [:bitcoin_address]]]}
70+
71+
@ckb_transaction = CkbTransaction.includes(includes).find_by(tx_hash: params[:id])
6872
return head :not_found unless @ckb_transaction
6973

7074
transfers = [].tap do |res|
71-
combine_transfers(@ckb_transaction).each do |address_id, transfers|
72-
vout = BitcoinVout.includes(:bitcoin_address).find_by(address_id:)
73-
next unless vout
75+
combine_transfers(@ckb_transaction).each do |address, transfers|
76+
next unless address.bitcoin_vout
7477

75-
res << { address: vout&.bitcoin_address&.address_hash, transfers: }
78+
res << { address: address.bitcoin_vout&.bitcoin_address&.address_hash, transfers: }
7679
end
7780
end
7881

app/controllers/concerns/cell_data_comparator.rb

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

app/models/address.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Address < ApplicationRecord
1616
has_many :ckb_dao_transactions, -> { distinct }, through: :dao_events, source: :ckb_transaction
1717
has_one :bitcoin_address_mapping, foreign_key: "ckb_address_id"
1818
has_one :bitcoin_address, through: :bitcoin_address_mapping
19+
has_one :bitcoin_vout
1920

2021
validates :balance, :ckb_transactions_count, :interest, :dao_deposit,
2122
numericality: { greater_than_or_equal_to: 0 }, allow_nil: true

0 commit comments

Comments
 (0)