Skip to content

Commit

Permalink
refactor: export dao depositors (#2037)
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitz authored Jul 8, 2024
1 parent 0fffb95 commit e92fb43
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 239 deletions.
58 changes: 41 additions & 17 deletions app/jobs/csv_exportable/export_dao_depositors_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,61 @@
module CsvExportable
class ExportDaoDepositorsJob < BaseExporter
def perform(args)
sql = "".dup
start_date, end_date = extract_dates(args)

if args[:start_date].present?
sql << "ckb_transactions.block_timestamp >= #{BigDecimal(args[:start_date])}"
end
rows = fetch_deposit_rows(start_date, end_date) + fetch_withdrawing_rows(start_date, end_date)

if args[:end_date].present?
sql << " AND ckb_transactions.block_timestamp <= #{BigDecimal(args[:end_date])}"
end
header = ["Address", "Capacity", "Txn hash", "Previous Txn hash", "UnixTimestamp", "date(UTC)"]
generate_csv(header, rows)
end

private

def extract_dates(args)
start_date = args[:start_date].present? ? BigDecimal(args[:start_date]) : nil
end_date = args[:end_date].present? ? BigDecimal(args[:end_date]) : nil
start_number = args[:start_number].presence
end_number = args[:end_number].presence

if args[:start_number].present?
sql << "ckb_transactions.block_number >= #{args[:start_number]}"
if start_number.present?
start_date = Block.find_by(number: start_number)&.timestamp
end

if args[:end_number].present?
sql << " AND ckb_transactions.block_number <= #{args[:end_number]}"
if end_number.present?
end_date = Block.find_by(number: end_number)&.timestamp
end

[start_date, end_date]
end

def build_sql_query(start_date, end_date)
sql = "".dup
sql << "block_timestamp >= #{start_date}" if start_date.present?
sql << " AND " if start_date.present? && end_date.present?
sql << "block_timestamp <= #{end_date}" if end_date.present?
sql
end

def fetch_deposit_rows(start_date, end_date)
sql = build_sql_query(start_date, end_date)
rows = []
CellOutput.left_joins(:ckb_transaction, :address).live.nervos_dao_deposit.where(sql).
select("cell_outputs.*, ckb_transactions.block_number, ckb_transactions.block_timestamp").find_in_batches(batch_size: 1000) do |cells|

CellOutput.includes(:address).live.nervos_dao_deposit.where(sql).find_in_batches(batch_size: 500) do |cells|
cells.each do |cell|
amount = CkbUtils.shannon_to_byte(BigDecimal(cell.capacity))
datetime = datetime_utc(cell.block_timestamp)
rows << [cell.address_hash, amount, cell.tx_hash, nil, cell.block_timestamp, datetime]
end
end

CellOutput.left_joins(:ckb_transaction, :address).live.nervos_dao_withdrawing.where(sql).
select("cell_outputs.*, ckb_transactions.block_number, ckb_transactions.block_timestamp").find_in_batches(batch_size: 1000) do |cells|
rows
end

def fetch_withdrawing_rows(start_date, end_date)
sql = build_sql_query(start_date, end_date)
rows = []

CellOutput.includes(:address).live.nervos_dao_withdrawing.where(sql).find_in_batches(batch_size: 500) do |cells|
cells.each do |cell|
cell_input = cell.ckb_transaction.cell_inputs.nervos_dao_deposit.first
previous_cell_output = cell_input.previous_cell_output
Expand All @@ -43,8 +68,7 @@ def perform(args)
end
end

header = ["Address", "Capacity", "Txn hash", "Previous Txn hash", "UnixTimestamp", "date(UTC)"]
generate_csv(header, rows)
rows
end
end
end
60 changes: 0 additions & 60 deletions app/jobs/import_btc_time_cell_job.rb

This file was deleted.

159 changes: 0 additions & 159 deletions app/jobs/import_rgbpp_cell_job.rb

This file was deleted.

3 changes: 1 addition & 2 deletions app/workers/generate_udt_holder_allocation_worker.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class GenerateUdtHolderAllocationWorker
include Sidekiq::Worker
sidekiq_options retry: 3
include Sidekiq::Job

def perform(type_hashes = nil)
type_hashes ||= $redis.smembers("udt_holder_allocation")
Expand Down
2 changes: 1 addition & 1 deletion lib/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def call_worker(clz)
call_worker TokenCollectionTagWorker
end

s.every "1h", overlap: false do
s.every "10m", overlap: false do
call_worker GenerateUdtHolderAllocationWorker
end

Expand Down

0 comments on commit e92fb43

Please sign in to comment.