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

refactor: pending transactions query #1503

Merged
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
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