Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy to testnet #2059

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 45 additions & 28 deletions app/jobs/csv_exportable/export_contract_transactions_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,49 @@
module CsvExportable
class ExportContractTransactionsJob < BaseExporter
def perform(args)
dao_contract = DaoContract.default_contract
ckb_transactions = dao_contract.ckb_transactions.includes(dao_events: [:address]).tx_committed
start_date, end_date = extract_dates(args)
transaction_rows = fetch_transaction_rows(start_date, end_date)
header = [
"Txn hash", "Address", "Blockno", "UnixTimestamp", "Method",
"Amount", "Token", "TxnFee(CKB)", "date(UTC)"
]
generate_csv(header, transaction_rows)
end

if args[:start_date].present?
start_date = BigDecimal(args[:start_date])
ckb_transactions = ckb_transactions.where("ckb_transactions.block_timestamp >= ?", start_date)
end
private

if args[:end_date].present?
end_date = BigDecimal(args[:end_date])
ckb_transactions = ckb_transactions.where("ckb_transactions.block_timestamp <= ?", end_date)
end
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?
ckb_transactions = ckb_transactions.where("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?
ckb_transactions = ckb_transactions.where("ckb_transactions.block_number <= ?", args[:end_number])
if end_number.present?
end_date = Block.find_by(number: end_number)&.timestamp
end

ckb_transactions = ckb_transactions.order("ckb_transactions.block_timestamp desc nulls last, ckb_transactions.id desc").limit(5000)
[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_transaction_rows(start_date, end_date)
sql = build_sql_query(start_date, end_date)
rows = []
ckb_transactions.find_in_batches(batch_size: 1000) do |transactions|

dao_contract = DaoContract.default_contract
dao_contract.ckb_transactions.includes(dao_events: [:address]).tx_committed.where(sql).
order("block_timestamp desc nulls last, id desc").limit(5000).find_in_batches(batch_size: 500) do |transactions|
transactions.each do |transaction|
row = generate_row(transaction)
next if row.blank?
Expand All @@ -36,27 +54,18 @@ def perform(args)
end
end

header = [
"Txn hash", "Address", "Blockno", "UnixTimestamp", "Method",
"Amount", "Token", "TxnFee(CKB)", "date(UTC)"
]

generate_csv(header, rows)
rows
end

def generate_row(transaction)
dao_events = transaction.dao_events
dao_events = transaction.dao_events.where(event_type: ["deposit_to_dao", "withdraw_from_dao", "issue_interest"])

rows = []
dao_events.each do |dao_event|
datetime = datetime_utc(transaction.block_timestamp)
fee = parse_transaction_fee(transaction.transaction_fee)
amount = CkbUtils.shannon_to_byte(BigDecimal(dao_event.value))
method = {
"deposit_to_dao" => "Deposit",
"withdraw_from_dao" => "Withdraw Request",
"issue_interest" => "Withdraw Finalization",
}[dao_event.event_type]
method = map_event_type(dao_event.event_type)

rows << [
transaction.tx_hash,
Expand All @@ -73,5 +82,13 @@ def generate_row(transaction)

rows
end

def map_event_type(event_type)
{
"deposit_to_dao" => "Deposit",
"withdraw_from_dao" => "Withdraw Request",
"issue_interest" => "Withdraw Finalization",
}[event_type]
end
end
end
Loading