-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: address live cells contoller (#1610)
Signed-off-by: Miles Zhang <[email protected]>
- Loading branch information
1 parent
06d070d
commit 15df613
Showing
8 changed files
with
249 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Api | ||
module V1 | ||
class AddressLiveCellsController < ApplicationController | ||
before_action :validate_pagination_params, :pagination_params | ||
|
||
def show | ||
address = Address.find_address!(params[:id]) | ||
raise Api::V1::Exceptions::AddressNotFoundError if address.is_a?(NullAddress) | ||
|
||
order_by, asc_or_desc = live_cells_ordering | ||
@addresses = address.cell_outputs.live.order(order_by => asc_or_desc).page(@page).per(@page_size).fast_page | ||
options = FastJsonapi::PaginationMetaGenerator.new( | ||
request:, | ||
records: @addresses, | ||
page: @page, | ||
page_size: @page_size, | ||
).call | ||
render json: CellOutputSerializer.new(@addresses, options).serialized_json | ||
end | ||
|
||
private | ||
|
||
def pagination_params | ||
@page = params[:page] || 1 | ||
@page_size = params[:page_size] || CellOutput.default_per_page | ||
end | ||
|
||
def live_cells_ordering | ||
sort, order = params.fetch(:sort, "block_timestamp.desc").split(".", 2) | ||
if order.nil? || !order.match?(/^(asc|desc)$/i) | ||
order = "asc" | ||
end | ||
|
||
[sort, order] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class CellOutputSerializer | ||
include FastJsonapi::ObjectSerializer | ||
|
||
attributes :cell_type, :tx_hash, :cell_index, :type_hash, :data | ||
|
||
attribute :capacity do |object| | ||
object.capacity.to_s | ||
end | ||
|
||
attribute :occupied_capacity do |object| | ||
object.occupied_capacity.to_s | ||
end | ||
|
||
attribute :block_timestamp do |object| | ||
object.block_timestamp.to_s | ||
end | ||
|
||
attribute :type_script do |object| | ||
object&.type_script&.to_node | ||
end | ||
|
||
attribute :lock_script do |object| | ||
object.lock_script.to_node | ||
end | ||
|
||
attribute :extra_info do |object| | ||
case object.cell_type | ||
when "udt" | ||
object.udt_info | ||
when "cota_registry" | ||
object.cota_registry_info | ||
when "cota_regular" | ||
object.cota_regular_info | ||
when "m_nft_issuer", "m_nft_class", "m_nft_token" | ||
object.m_nft_info | ||
when "nrc_721_token", "nrc_721_factory" | ||
object.nrc_721_nft_info | ||
when "omiga_inscription_info", "omiga_inscription" | ||
object.omiga_inscription_info | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.