Skip to content

Commit

Permalink
refactor: move display_inputs and display_outputs to api/v2 (#1553)
Browse files Browse the repository at this point in the history
* refactor: move display_inputs and display_outputs to api/v2

* chore: add display_cells option
  • Loading branch information
rabbitz authored Jan 4, 2024
1 parent 928b869 commit 33ccdac
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 48 deletions.
49 changes: 6 additions & 43 deletions app/controllers/api/v1/ckb_transactions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
module Api
module V1
class CkbTransactionsController < ApplicationController
before_action :validate_query_params,
only: %i[show display_inputs display_outputs]
before_action :find_transaction,
only: %i[show display_inputs display_outputs]
before_action :validate_pagination_params, :pagination_params,
only: %i[index display_inputs display_outputs]
before_action :validate_query_params, only: %i[show]
before_action :find_transaction,only: %i[show]
before_action :validate_pagination_params, :pagination_params, only: %i[index]

def index
if from_home_page?
Expand Down Expand Up @@ -109,43 +106,9 @@ def query
def show
expires_in 10.seconds, public: true, must_revalidate: true

render json: CkbTransactionSerializer.new(@ckb_transaction)
end

def display_inputs
expires_in 1.hour, public: true, must_revalidate: true

if @ckb_transaction.is_cellbase
cell_inputs = @ckb_transaction.cellbase_display_inputs
total_count = cell_inputs.count
else
cell_inputs = @ckb_transaction.cell_inputs.order(id: :asc).
page(@page).per(@page_size).fast_page
total_count = cell_inputs.total_count
cell_inputs = @ckb_transaction.normal_tx_display_inputs(cell_inputs)
end

render json: { data: cell_inputs,
meta: { total: total_count,
page_size: @page_size.to_i } }
end

def display_outputs
expires_in 1.hour, public: true, must_revalidate: true

if @ckb_transaction.is_cellbase
cell_outputs = @ckb_transaction.cellbase_display_outputs
total_count = cell_outputs.count
else
cell_outputs = @ckb_transaction.outputs.order(id: :asc).
page(@page).per(@page_size).fast_page
total_count = cell_outputs.total_count
cell_outputs = @ckb_transaction.normal_tx_display_outputs(cell_outputs)
end

render json: { data: cell_outputs,
meta: { total: total_count,
page_size: @page_size.to_i } }
render json: CkbTransactionSerializer.new(@ckb_transaction, {
params: { display_cells: params.fetch(:display_cells, true)
}})
end

private
Expand Down
57 changes: 56 additions & 1 deletion app/controllers/api/v2/ckb_transactions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Api
module V2
class CkbTransactionsController < BaseController
# transaction lite info
before_action :set_page_and_page_size, only: %i[display_inputs display_outputs]

def details
ckb_transaction = CkbTransaction.where(tx_hash: params[:id]).order(tx_status: :desc).first
head :not_found and return if ckb_transaction.blank?
Expand All @@ -15,6 +16,55 @@ def details
render json: { data: transfers }
end

def display_inputs
expires_in 1.hour, public: true, must_revalidate: true

ckb_transaction = CkbTransaction.where(tx_hash: params[:id]).order(tx_status: :desc).first
head :not_found and return if ckb_transaction.blank?

if ckb_transaction.is_cellbase
cell_inputs = ckb_transaction.cellbase_display_inputs
total_count = cell_inputs.count
else
cell_inputs = ckb_transaction.cell_inputs.order(id: :asc).
page(@page).per(@page_size).fast_page
total_count = cell_inputs.total_count
cell_inputs = ckb_transaction.normal_tx_display_inputs(cell_inputs)
end

render json: {
data: cell_inputs,
meta: {
total: total_count,
page_size: @page_size.to_i,
},
}
end

def display_outputs
expires_in 1.hour, public: true, must_revalidate: true

ckb_transaction = CkbTransaction.where(tx_hash: params[:id]).order(tx_status: :desc).first
head :not_found and return if ckb_transaction.blank?

if ckb_transaction.is_cellbase
cell_outputs = ckb_transaction.cellbase_display_outputs
total_count = cell_outputs.count
else
cell_outputs = ckb_transaction.outputs.order(id: :asc).
page(@page).per(@page_size).fast_page
total_count = cell_outputs.total_count
cell_outputs = ckb_transaction.normal_tx_display_outputs(cell_outputs)
end

render json: {
data: cell_outputs,
meta: {
total: total_count, page_size: @page_size.to_i
},
}
end

private

def build_cell_capacities(outputs)
Expand Down Expand Up @@ -92,6 +142,11 @@ def token_unit(cell)
"CKB"
end
end

def set_page_and_page_size
@page = params.fetch(:page, 1)
@page_size = params.fetch(:page_size, CkbTransaction.default_per_page)
end
end
end
end
6 changes: 6 additions & 0 deletions app/serializers/ckb_transaction_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class CkbTransactionSerializer
end

attribute :display_inputs do |object, params|
display_cells = ActiveModel::Type::Boolean.new.cast(params[:display_cells])
next [] unless display_cells

cache_key = "display_inputs_previews_#{params[:previews].present?}_#{object.id}_#{object.inputs.cache_version}"
Rails.cache.fetch(cache_key, expires_in: 1.day) do
if params && params[:previews]
Expand All @@ -56,6 +59,9 @@ class CkbTransactionSerializer
end

attribute :display_outputs do |object, params|
display_cells = ActiveModel::Type::Boolean.new.cast(params[:display_cells])
next [] unless display_cells

cache_key = "display_outputs_previews_#{params[:previews].present?}_#{object.id}_#{object.outputs.cache_version}"
Rails.cache.fetch(cache_key, expires_in: 1.day) do
if params && params[:previews]
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/rack_attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ class Rack::Attack
# If you want to return 503 so that the attacker might be fooled into
# believing that they've successfully broken your app (or you just want to
# customize the response), then uncomment these lines.
# self.throttled_response = lambda do |env|
# self.throttled_responder = lambda do |env|
# [ 503, # status
# {}, # headers
# ['']] # body
# end
#
self.throttled_response =
self.throttled_responder =
lambda do |env|
match_data = env["rack.attack.match_data"]
now = match_data[:epoch_time]
Expand Down
2 changes: 0 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
resources :block_transactions, only: :show
resources :addresses, only: :show
get "/transactions/:id", to: "ckb_transactions#show", as: "ckb_transaction"
get "/transactions/:id/display_inputs", to: "ckb_transactions#display_inputs"
get "/transactions/:id/display_outputs", to: "ckb_transactions#display_outputs"
get "/transactions", to: "ckb_transactions#index", as: "ckb_transactions"
post "/transactions/query", to: "ckb_transactions#query", as: "query_ckb_transactions"
resources :cell_input_lock_scripts, only: :show
Expand Down
2 changes: 2 additions & 0 deletions config/routes/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
resources :ckb_transactions, only: [:index, :show] do
member do
get :details
get :display_inputs
get :display_outputs
end
end
resources :transactions do
Expand Down

0 comments on commit 33ccdac

Please sign in to comment.