Skip to content

Commit 4eb6f3e

Browse files
committed
current balance (#2907)
1 parent 133ed72 commit 4eb6f3e

22 files changed

+39
-113
lines changed

app/interactions/addresses/ckb_transactions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def execute
3232
select(:ckb_transaction_id).
3333
page(page).per(page_size)
3434

35-
total_count = Address.find(address_id).ckb_transactions_count
35+
total_count = Address.find(address_id).current_ckb_transactions_count
3636
end
3737

3838
ckb_transaction_ids = account_books.map(&:ckb_transaction_id)

app/models/address.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Address < ApplicationRecord
1818
has_one :bitcoin_address, through: :bitcoin_address_mapping
1919
has_one :bitcoin_vout
2020

21-
validates :balance, :ckb_transactions_count, :interest, :dao_deposit,
21+
validates :balance, :interest, :dao_deposit,
2222
numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
2323
validates :lock_hash, presence: true, uniqueness: true, on: :create
2424

@@ -30,10 +30,6 @@ class Address < ApplicationRecord
3030

3131
attr_accessor :query_address
3232

33-
def custom_ckb_transactions
34-
ckb_transactions
35-
end
36-
3733
def ckb_udt_transactions(udt)
3834
udt = Udt.find_by_id(udt) unless udt.is_a?(Udt)
3935
udt&.ckb_transactions || []
@@ -160,6 +156,22 @@ def recalc_revalidate_balance!
160156
save!
161157
end
162158

159+
def current_balance
160+
self.cell_outputs.live.sum(:capacity)
161+
end
162+
163+
def ckb_transactions_count
164+
AccountBook.where(address_id: self.id).count
165+
end
166+
167+
def current_live_cells_count
168+
self.cell_outputs.live.count
169+
end
170+
171+
def current_balance_occupied
172+
self.cell_outputs.live.occupied.sum(:capacity)
173+
end
174+
163175
def cal_balance
164176
total = cell_outputs.live.sum(:capacity)
165177
occupied = cell_outputs.live.occupied.sum(:capacity)

app/models/cell_output.rb

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -228,65 +228,6 @@ def create_token
228228
end
229229
end
230230

231-
# Because the balance of address equals to the total capacity of all live cells in this address,
232-
# So we can directly aggregate balance by address from database.
233-
def self.refresh_address_balances
234-
puts "refreshing all balances"
235-
# fix balance and live cell count for all addresses
236-
connection.execute <<-SQL
237-
UPDATE addresses SET balance=sq.balance, live_cells_count=c
238-
FROM (
239-
SELECT address_id, SUM(capacity) as balance, COUNT(*) as c
240-
FROM cell_outputs
241-
WHERE status = 0
242-
GROUP BY address_id
243-
) AS sq
244-
WHERE addresses.id=sq.address_id;
245-
SQL
246-
# fix occupied balances for all addresses
247-
puts "refreshing all occupied balances"
248-
connection.execute <<-SQL
249-
UPDATE addresses SET balance_occupied=sq.balance
250-
FROM (
251-
SELECT address_id, SUM(capacity) as balance
252-
FROM cell_outputs
253-
WHERE status = 0
254-
AND "cell_outputs"."type_hash" IS NOT NULL AND "cell_outputs"."data" != '\x3078'
255-
GROUP BY address_id
256-
) AS sq
257-
WHERE addresses.id=sq.address_id;
258-
SQL
259-
puts "refreshing dao deposits"
260-
connection.execute <<-SQL
261-
UPDATE addresses SET dao_deposit=sq.sum, is_depositor = sq.sum > 0
262-
FROM (
263-
SELECT address_id, SUM(capacity) as sum
264-
FROM cell_outputs
265-
WHERE status = 0
266-
AND cell_type = 1
267-
GROUP BY address_id
268-
) AS sq
269-
WHERE addresses.id=sq.address_id;
270-
SQL
271-
end
272-
273-
# update the history data, which cell_type should be "cota_registry" or "cota_regular"
274-
def self.update_cell_types_for_cota
275-
TypeScript.where(code_hash: CkbSync::Api.instance.cota_registry_code_hash).each do |type_script|
276-
CellOutput.where(type_script_id: type_script.id).each do |cell_output|
277-
cell_output.cell_type = "cota_registry"
278-
cell_output.save!
279-
end
280-
end
281-
282-
TypeScript.where(code_hash: CkbSync::Api.instance.cota_regular_code_hash).each do |type_script|
283-
CellOutput.where(type_script_id: type_script.id).each do |cell_output|
284-
cell_output.cell_type = "cota_regular"
285-
cell_output.save!
286-
end
287-
end
288-
end
289-
290231
def self.dead_reltuples_count
291232
sql = "SELECT reltuples FROM pg_class WHERE relname = 'cell_outputs_dead'"
292233
result = ActiveRecord::Base.connection.execute(sql)

app/models/null_address.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ def balance
1313
0
1414
end
1515

16+
def current_balance
17+
0
18+
end
19+
20+
def current_balance_occupied
21+
0
22+
end
23+
1624
def ckb_transactions_count
1725
0
1826
end

app/serializers/address_serializer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class AddressSerializer
77
object.query_address || object.address_hash
88
end
99
attribute :balance do |object|
10-
object.balance.to_s
10+
object.current_balance.to_s
1111
end
1212
attribute :transactions_count do |object|
1313
object.ckb_transactions_count.to_s
@@ -144,7 +144,7 @@ class AddressSerializer
144144
(object.interest.to_i + object.unclaimed_compensation.to_i).to_s
145145
end
146146
attribute :balance_occupied do |object|
147-
object.balance_occupied.to_s
147+
object.current_balance_occupied.to_s
148148
end
149149
attribute :bitcoin_address_hash do |object|
150150
object.bitcoin_address&.address_hash

app/workers/calculate_address_info_worker.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ def perform()
2727
id: addr.id,
2828
balance: balance,
2929
balance_occupied: balance_occupied,
30-
ckb_transactions_count: AccountBook.where(address_id: addr.id).count,
31-
live_cells_count: addr.cell_outputs.live.count,
3230
updated_at: Time.current
3331
}
3432

app/workers/update_address_info_worker.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ def perform(block_number)
1313
id: addr.id,
1414
balance: balance,
1515
balance_occupied: balance_occupied,
16-
ckb_transactions_count: AccountBook.where(address_id: addr.id).count,
17-
live_cells_count: addr.cell_outputs.live.count,
1816
updated_at: Time.current
1917
}
2018
end

lib/tasks/migration/check_address_info.rake

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ namespace :migration do
77
if address.last_updated_block_number.nil?
88
local_tip_block = Block.recent.first
99
address.last_updated_block_number = local_tip_block.number
10-
address.live_cells_count = address.cell_outputs.live.where("block_timestamp <= ?", local_tip_block.timestamp).count
11-
address.ckb_transactions_count = AccountBook.where(address_id: address.id).where("block_number <= ?", local_tip_block.number).count
1210
address.cal_balance!
1311
address.save!
1412
end

lib/tasks/migration/fill_nrc_721_token.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace :migration do
4040
# update addresses transaction page cache
4141
CkbTransaction.where(id: tx_ids).find_each do |ckb_tx|
4242
Address.where(id: ckb_tx.contained_address_ids).find_each do |address|
43-
ckb_transactions = address.custom_ckb_transactions.select(:id, :tx_hash, :block_id, :block_number,
43+
ckb_transactions = address.ckb_transactions.select(:id, :tx_hash, :block_id, :block_number,
4444
:block_timestamp, :is_cellbase, :updated_at).recent.page(1).per(CkbTransaction.default_per_page)
4545
Rails.cache.delete("#{ckb_transactions.cache_key}/#{address.query_address}")
4646
end

lib/tasks/migration/fix_address_tx_count.rake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ namespace :migration do
77
puts address.id
88
local_tip_block = Block.recent.first
99
address.update(
10-
ckb_transactions_count: AccountBook.where(address_id: address.id).where("block_number <= ?", local_tip_block.number).count,
1110
last_updated_block_number: local_tip_block.number,
1211
)
1312
AddressBlockSnapshot.where(address_id: address.id).delete_all

0 commit comments

Comments
 (0)