Skip to content

Commit

Permalink
refactor: pending transactions query (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitz authored Nov 17, 2023
1 parent 7c06249 commit 321a7fc
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions app/controllers/api/v2/pending_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,32 @@ class PendingTransactionsController < BaseController
before_action :set_page_and_page_size

def index
pending_transactions = CkbTransaction.tx_pending.includes(:cell_inputs).
where.not(cell_inputs: { previous_cell_output_id: nil, from_cell_base: false })
expires_in 10.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds

if stale?(pending_transactions)
expires_in 10.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds
tx_ids = CkbTransaction.tx_pending.ids
unique_tx_ids = CellInput.where(ckb_transaction_id: tx_ids).
where.not(previous_cell_output_id: nil, from_cell_base: false).
pluck(:ckb_transaction_id).uniq
pending_transactions = CkbTransaction.where(id: unique_tx_ids).
order(transactions_ordering).page(@page).per(@page_size)

total_count = pending_transactions.count
pending_transactions = sort_transactions(pending_transactions).
page(@page).per(@page_size)

json = {
data: pending_transactions.map do |tx|
{
transaction_hash: tx.tx_hash,
capacity_involved: tx.capacity_involved,
transaction_fee: tx.transaction_fee,
created_at: tx.created_at,
create_timestamp: (tx.created_at.to_f * 1000).to_i
}
end,
meta: {
total: total_count,
page_size: @page_size.to_i
json = {
data: pending_transactions.map do |tx|
{
transaction_hash: tx.tx_hash,
capacity_involved: tx.capacity_involved,
transaction_fee: tx.transaction_fee,
created_at: tx.created_at,
create_timestamp: (tx.created_at.to_f * 1000).to_i
}
end,
meta: {
total: pending_transactions.total_count,
page_size: @page_size.to_i
}
}

render json: json
end
render json: json
end

def count
Expand All @@ -47,7 +45,7 @@ def set_page_and_page_size
@page_size = params[:page_size] || CkbTransaction.default_per_page
end

def sort_transactions(records)
def transactions_ordering
sort, order = params.fetch(:sort, "id.desc").split(".", 2)
sort =
case sort
Expand All @@ -61,7 +59,7 @@ def sort_transactions(records)
order = "asc"
end

records.order("ckb_transactions.#{sort} #{order} NULLS LAST")
"#{sort} #{order} NULLS LAST"
end
end
end
Expand Down

0 comments on commit 321a7fc

Please sign in to comment.