-
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.
Merge pull request #1614 from nervosnetwork/testnet
Deploy to mainnet
- Loading branch information
Showing
26 changed files
with
475 additions
and
153 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
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,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
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
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,64 @@ | ||
module Users | ||
class CkbTransactions < ActiveInteraction::Base | ||
include Api::V2::Exceptions | ||
|
||
object :user | ||
object :request, class: ActionDispatch::Request | ||
string :address_hash, default: nil | ||
string :tx_hash, default: nil | ||
string :sort, default: "ckb_transaction_id.desc" | ||
integer :page, default: 1 | ||
integer :page_size, default: CkbTransaction.default_per_page | ||
|
||
def execute | ||
account_books = sort_account_books(filter_account_books).page(page).per(page_size).fast_page | ||
transactions = CkbTransaction.where(id: account_books.map(&:ckb_transaction_id)). | ||
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, | ||
:is_cellbase, :updated_at, :capacity_involved). | ||
order(id: :desc) | ||
|
||
options = FastJsonapi::PaginationMetaGenerator.new( | ||
records: transactions, | ||
records_counter: account_books, | ||
request:, | ||
page:, | ||
page_size:, | ||
).call | ||
options[:params] = { previews: true, address: user.addresses } | ||
|
||
transactions_serializer = CkbTransactionsSerializer.new(transactions, options) | ||
transactions_serializer.serialized_json | ||
end | ||
|
||
private | ||
|
||
def filter_account_books | ||
address_ids = user.address_ids | ||
if address_hash.present? | ||
address = Address.find_address!(address_hash) | ||
address_ids = Array[address.id] | ||
end | ||
|
||
scope = AccountBook.joins(:ckb_transaction).where( | ||
account_books: { address_id: address_ids }, | ||
ckb_transactions: { tx_status: "committed" }, | ||
) | ||
scope = scope.where(ckb_transactions: { tx_hash: }) if tx_hash.present? | ||
|
||
scope | ||
rescue StandardError | ||
raise AddressNotFoundError.new | ||
end | ||
|
||
def sort_account_books(records) | ||
sorting, ordering = sort.split(".", 2) | ||
sorting = "ckb_transactions.block_timestamp" if sorting == "time" | ||
|
||
if ordering.nil? || !ordering.match?(/^(asc|desc)$/i) | ||
ordering = "asc" | ||
end | ||
|
||
records.order("#{sorting} #{ordering}") | ||
end | ||
end | ||
end |
Oops, something went wrong.