Skip to content

Commit

Permalink
feat: add address pending transactions (#1514)
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitz authored Nov 28, 2023
1 parent 162a8bf commit f9d423b
Show file tree
Hide file tree
Showing 6 changed files with 446 additions and 14 deletions.
82 changes: 82 additions & 0 deletions app/controllers/api/v1/address_pending_transactions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module Api
module V1
class AddressPendingTransactionsController < ApplicationController
before_action :validate_query_params
before_action :validate_pagination_params, :pagination_params
before_action :find_address

def show
expires_in 10.seconds, public: true, must_revalidate: true, stale_while_revalidate: 10.seconds

ckb_transactions = @address.ckb_transactions.tx_pending
ckb_transactions_ids = CellInput.where(ckb_transaction_id: ckb_transactions.ids).
where.not(previous_cell_output_id: nil, from_cell_base: false).
distinct.pluck(:ckb_transaction_id)
@ckb_transactions = CkbTransaction.where(id: ckb_transactions_ids).
order(transactions_ordering).page(@page).per(@page_size)

render json: serialized_ckb_transactions
end

private

def validate_query_params
validator = Validations::Address.new(params)

if validator.invalid?
errors = validator.error_object[:errors]
status = validator.error_object[:status]

render json: errors, status: status
end
end

def pagination_params
@page = params[:page] || 1
@page_size = params[:page_size] || CkbTransaction.default_per_page
end

def find_address
@address = Address.find_address!(params[:id])
raise Api::V1::Exceptions::AddressNotFoundError if @address.is_a?(NullAddress)
end

def transactions_ordering
sort, order = params.fetch(:sort, "id.desc").split(".", 2)
sort = case sort
when "time" then "block_timestamp"
else "id"
end

order = order.match?(/^(asc|desc)$/i) ? order : "asc"

"#{sort} #{order} NULLS LAST"
end

def serialized_ckb_transactions
options = FastJsonapi::PaginationMetaGenerator.new(
request: request,
records: @ckb_transactions,
page: @page,
page_size: @page_size
).call
ckb_transaction_serializer = CkbTransactionsSerializer.new(
@ckb_transactions,
options.merge(params: { previews: true, address: @address })
)

if QueryKeyUtils.valid_address?(params[:id])
if @address.address_hash == @address.query_address
ckb_transaction_serializer.serialized_json
else
ckb_transaction_serializer.serialized_json.gsub(
@address.address_hash, @address.query_address
)
end
else
ckb_transaction_serializer.serialized_json
end
end
end
end
end
6 changes: 0 additions & 6 deletions app/models/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,6 @@ def self.find_address!(query_key)
end

def self.cached_find(query_key)
cache_key = query_key

unless QueryKeyUtils.valid_hex?(query_key)
cache_key = CkbUtils.parse_address(query_key).script.compute_hash
end

address =
if QueryKeyUtils.valid_hex?(query_key)
find_by(lock_hash: query_key)
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
resources :distribution_data, only: :show
resources :monetary_data, only: :show
resources :udt_verifications, only: :update
resources :address_pending_transactions, only: :show
end
end
draw "v2"
Expand Down
2 changes: 0 additions & 2 deletions lib/tasks/migration/fix_address_balance_occupied.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ namespace :migration do
pr_merged_datetime = DateTime.new(2022,7,23,0,0,0)
addresses = Address.where("updated_at > ?", pr_merged_datetime).order(id: :asc)
addresses.find_each do |address|
puts "Address ID: #{address.id}"

occupied = address.cell_outputs.live.occupied.sum(:capacity)
address.update(balance_occupied: occupied)
end
Expand Down
Loading

0 comments on commit f9d423b

Please sign in to comment.