Skip to content

Commit

Permalink
chore: enhance 'created_at' return in ckb transactions serializer (#1537
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rabbitz authored Dec 19, 2023
1 parent 3b88a44 commit 40e4c19
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def show
address = Address.find_address!(params[:id])
raise Api::V1::Exceptions::AddressNotFoundError if address.is_a?(NullAddress)

ckb_dao_transactions = address.ckb_dao_transactions.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at).recent.page(@page).per(@page_size).fast_page
ckb_dao_transactions = address.ckb_dao_transactions.select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at).recent.page(@page).per(@page_size).fast_page
json =
Rails.cache.realize(ckb_dao_transactions.cache_key, version: ckb_dao_transactions.cache_version) do
records_counter = RecordCounters::AddressDaoTransactions.new(address)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/address_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def show
ckb_transaction_ids = tx_ids.map(&:ckb_transaction_id)
ckb_transactions = CkbTransaction.where(id: ckb_transaction_ids).
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp,
:is_cellbase, :updated_at, :capacity_involved).
:is_cellbase, :updated_at, :capacity_involved, :created_at).
order(order_by => asc_or_desc)

options = FastJsonapi::PaginationMetaGenerator.new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def show
udt = Udt.find_by(type_hash: params[:type_hash], published: true)
raise Api::V1::Exceptions::UdtNotFoundError if udt.blank?

ckb_dao_transactions = address.ckb_udt_transactions(udt.id).select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at).recent.page(@page).per(@page_size).fast_page
ckb_dao_transactions = address.ckb_udt_transactions(udt.id).
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at).
recent.page(@page).per(@page_size).fast_page
json =
Rails.cache.realize(ckb_dao_transactions.cache_key, version: ckb_dao_transactions.cache_version) do
records_counter = RecordCounters::AddressUdtTransactions.new(address, udt.id)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/block_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class BlockTransactionsController < ApplicationController
def show
block = Block.find_by!(block_hash: params[:id])
ckb_transactions = block.ckb_transactions.
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at).
select(:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at).
order(is_cellbase: :desc, id: :asc)

if params[:tx_hash].present?
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/api/v1/ckb_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CkbTransactionsController < ApplicationController
def index
if from_home_page?
ckb_transactions = CkbTransaction.tx_committed.recent.normal.select(
:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved, :updated_at
:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved, :updated_at, :created_at
).limit((Settings.homepage_transactions_records_count || 15).to_i)
json =
Rails.cache.realize(ckb_transactions.cache_key,
Expand All @@ -18,7 +18,7 @@ def index
render json: json
else
ckb_transactions = CkbTransaction.tx_committed.normal.select(
:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved, :updated_at
:id, :tx_hash, :block_number, :block_timestamp, :live_cell_changes, :capacity_involved, :updated_at, :created_at
)

params[:sort] ||= "id.desc"
Expand Down Expand Up @@ -84,7 +84,7 @@ def query
CkbTransaction.recent.normal.page(@page).per(@page_size).fast_page
end
ckb_transactions = ckb_transactions.select(:id, :tx_hash, :block_id,
:block_number, :block_timestamp, :is_cellbase, :updated_at)
:block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at)
json =
Rails.cache.realize(ckb_transactions.cache_key,
version: ckb_transactions.cache_version, race_condition_ttl: 1.minute) do
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/contract_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def show
expires_in 10.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds

ckb_transactions = dao_contract.ckb_transactions.includes(:cell_inputs, :cell_outputs).tx_committed.select(
:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at
:id, :tx_hash, :block_id, :block_number, :block_timestamp, :is_cellbase, :updated_at, :created_at
).order("ckb_transactions.block_timestamp desc nulls last, ckb_transactions.id desc")

if params[:tx_hash].present?
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/udt_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def show

ckb_transactions = udt.ckb_transactions.tx_committed.
select(:id, :tx_hash, :block_id, :block_number,
:block_timestamp, :is_cellbase, :updated_at).
:block_timestamp, :is_cellbase, :updated_at, :created_at).
order("ckb_transactions.block_timestamp desc nulls last, ckb_transactions.id desc")

if params[:tx_hash].present?
Expand Down
8 changes: 8 additions & 0 deletions app/serializers/ckb_transactions_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,12 @@ class CkbTransactionsSerializer
object.income(params[:address])
end
end

attribute :created_at do |object|
object.created_at.to_s
end

attribute :create_timestamp do |object|
(object.created_at.to_f * 1000).to_s
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class AddressDaoTransactionsControllerTest < ActionDispatch::IntegrationTest

response_tx_transaction = json["data"].first

assert_equal %w(block_number block_timestamp display_inputs display_inputs_count display_outputs display_outputs_count income is_cellbase transaction_hash).sort, response_tx_transaction["attributes"].keys.sort
assert_equal %w(block_number block_timestamp display_inputs display_inputs_count display_outputs display_outputs_count income is_cellbase transaction_hash created_at create_timestamp).sort, response_tx_transaction["attributes"].keys.sort
end

test "should return error object when no records found by id" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ class AddressPendingTransactionsControllerTest < ActionDispatch::IntegrationTest
income
is_cellbase
transaction_hash
created_at
create_timestamp
).sort, response_tx_transaction["attributes"].keys.sort
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class AddressTransactionsControllerTest < ActionDispatch::IntegrationTest
income
is_cellbase
transaction_hash
created_at
create_timestamp
).sort, response_tx_transaction["attributes"].keys.sort
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class AddressUdtTransactionsControllerTest < ActionDispatch::IntegrationTest

response_tx_transaction = json["data"].first

assert_equal %w(block_number block_timestamp display_inputs display_inputs_count display_outputs display_outputs_count income is_cellbase transaction_hash).sort, response_tx_transaction["attributes"].keys.sort
assert_equal %w(block_number block_timestamp display_inputs display_inputs_count display_outputs display_outputs_count income is_cellbase transaction_hash created_at create_timestamp).sort, response_tx_transaction["attributes"].keys.sort
end

test "should return error object when no records found by id" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class BlockTransactionsControllerTest < ActionDispatch::IntegrationTest
assert_equal %w(
block_number block_timestamp display_inputs
display_inputs_count display_outputs display_outputs_count
income is_cellbase transaction_hash
income is_cellbase transaction_hash created_at create_timestamp
).sort,
response_tx_transaction["attributes"].keys.sort
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ContractTransactionsControllerTest < ActionDispatch::IntegrationTest

response_tx_transaction = json["data"].first

assert_equal %w(block_number block_timestamp display_inputs display_inputs_count display_outputs display_outputs_count income is_cellbase transaction_hash).sort, response_tx_transaction["attributes"].keys.sort
assert_equal %w(block_number block_timestamp display_inputs display_inputs_count display_outputs display_outputs_count income is_cellbase transaction_hash created_at create_timestamp).sort, response_tx_transaction["attributes"].keys.sort
end

test "should return error object when no records found by give contract name" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class UdtTransactionsControllerTest < ActionDispatch::IntegrationTest
response_tx_transaction = json["data"].first

assert_equal %w(
block_number block_timestamp display_inputs display_inputs_count
block_number block_timestamp display_inputs display_inputs_count created_at create_timestamp
display_outputs display_outputs_count income is_cellbase transaction_hash
).sort,
response_tx_transaction["attributes"].keys.sort
Expand Down

0 comments on commit 40e4c19

Please sign in to comment.