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

feat: export dao transactions #1864

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions app/controllers/api/v1/contract_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def show
end
end

def download_csv
args = params.permit(:start_date, :end_date, :start_number, :end_number)
file = CsvExportable::ExportContractTransactionsJob.perform_now(args.to_h)

send_data file, type: "text/csv; charset=utf-8; header=present",
disposition: "attachment;filename=contract_transactions.csv"
end

private

def pagination_params
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/api/v1/dao_depositors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ def index

render json: DaoDepositorSerializer.new(addresses)
end

def download_csv
args = params.permit(:start_date, :end_date, :start_number, :end_number)
file = CsvExportable::ExportDaoDepositorsJob.perform_now(args.to_h)

send_data file, type: "text/csv; charset=utf-8; header=present",
disposition: "attachment;filename=dao_depositors.csv"
end
end
end
end
77 changes: 77 additions & 0 deletions app/jobs/csv_exportable/export_contract_transactions_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

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

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

if args[:end_date].present?
end_date = BigDecimal(args[:end_date])
ckb_transactions = ckb_transactions.where("ckb_transactions.block_timestamp <= ?", end_date)
end

if args[:start_number].present?
ckb_transactions = ckb_transactions.where("ckb_transactions.block_number >= ?", args[:start_number])
end

if args[:end_number].present?
ckb_transactions = ckb_transactions.where("ckb_transactions.block_number <= ?", args[:end_number])
end

ckb_transactions = ckb_transactions.order("ckb_transactions.block_timestamp desc nulls last, ckb_transactions.id desc").limit(5000)

rows = []
ckb_transactions.find_in_batches(batch_size: 1000) do |transactions|
transactions.each do |transaction|
row = generate_row(transaction)
next if row.blank?

rows += row
end
end

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

generate_csv(header, rows)
end

def generate_row(transaction)
dao_events = transaction.dao_events

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]

rows << [
transaction.tx_hash,
dao_event.address.address_hash,
transaction.block_number,
transaction.block_timestamp,
method,
amount,
"CKB",
fee,
datetime,
]
end

rows
end
end
end
50 changes: 50 additions & 0 deletions app/jobs/csv_exportable/export_dao_depositors_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module CsvExportable
class ExportDaoDepositorsJob < BaseExporter
def perform(args)
sql = ""

if args[:start_date].present?
sql << "ckb_transactions.block_timestamp >= #{BigDecimal(args[:start_date])}"
end

if args[:end_date].present?
sql << " AND ckb_transactions.block_timestamp <= #{BigDecimal(args[:start_date])}"
end

if args[:start_number].present?
sql << "ckb_transactions.block_number >= #{args[:start_number]}"
end

if args[:end_number].present?
sql << " AND ckb_transactions.block_number <= #{args[:end_number]}"
end

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|
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|
cells.each do |cell|
cell_input = cell.ckb_transaction.cell_inputs.nervos_dao_deposit.first
previous_cell_output = cell_input.previous_cell_output
previous_tx_hash = previous_cell_output.tx_hash
amount = CkbUtils.shannon_to_byte(BigDecimal(previous_cell_output.capacity))
datetime = datetime_utc(previous_cell_output.block_timestamp)
rows << [previous_cell_output.address_hash, amount, cell.tx_hash, previous_tx_hash, previous_cell_output.block_timestamp, datetime]
end
end

header = ["Address", "Capacity", "Txn hash", "Previous Txn hash", "UnixTimestamp", "date(UTC)"]
generate_csv(header, rows)
end
end
end
39 changes: 15 additions & 24 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@
resources :stats, only: :show
end
resources :blocks, only: %i(index show) do
collection do
get :download_csv
end
get :download_csv, on: :collection
end
resources :address_dao_transactions, only: :show
resources :block_transactions, only: :show
resources :addresses, only: :show
get "/transactions/:id", to: "ckb_transactions#show",
as: "ckb_transaction"
get "/transactions/:id", to: "ckb_transactions#show", as: "ckb_transaction"
get "/transactions", to: "ckb_transactions#index", as: "ckb_transactions"
post "/transactions/query", to: "ckb_transactions#query",
as: "query_ckb_transactions"
post "/transactions/query", to: "ckb_transactions#query", as: "query_ckb_transactions"
resources :cell_input_lock_scripts, only: :show
resources :cell_input_type_scripts, only: :show
resources :cell_input_data, only: :show
Expand All @@ -41,34 +37,30 @@
resources :statistics, only: %i(index show)
resources :nets, only: %i(index show)
resources :statistic_info_charts, only: :index
resources :contract_transactions, only: :show
resources :contract_transactions, only: :show do
get :download_csv, on: :collection
end
resources :contracts, only: :show
resources :dao_contract_transactions, only: :show
resources :address_transactions, only: :show do
collection do
get :download_csv
end
get :download_csv, on: :collection
end
resources :dao_depositors, only: :index do
get :download_csv, on: :collection
end
resources :dao_depositors, only: :index
resources :daily_statistics, only: :show
resources :block_statistics, only: :show ## TODO: unused route
resources :epoch_statistics, only: :show
resources :market_data, only: %i[index show]
resources :udts, only: %i(index show update) do
collection do
get :download_csv
end
get :download_csv, on: :collection
end
resources :xudts, only: %i(index show) do
collection do
get :download_csv
get :snapshot
end
get :download_csv, on: :collection
get :snapshot, on: :collection
end
resources :omiga_inscriptions, only: %i(index show) do
collection do
get :download_csv
end
get :download_csv, on: :collection
end
resources :udt_transactions, only: :show
resources :address_udt_transactions, only: :show
Expand All @@ -80,6 +72,5 @@
end
end
draw "v2"
match "/:anything" => "errors#routing_error", via: :all,
constraints: { anything: /.*/ }
match "/:anything" => "errors#routing_error", via: :all, constraints: { anything: /.*/ }
end
Loading