Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: if udt is omiga inscription, no need to validate published true #1660

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/controllers/api/v1/address_udt_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ def show
raise Api::V1::Exceptions::AddressNotFoundError if address.is_a?(NullAddress)
raise Api::V1::Exceptions::TypeHashInvalidError if params[:type_hash].blank?

udt = Udt.find_by(type_hash: params[:type_hash], published: true)
raise Api::V1::Exceptions::UdtNotFoundError if udt.blank?
udt = Udt.find_by(type_hash: params[:type_hash])
raise Api::V1::Exceptions::UdtNotFoundError if udt.blank? || (udt.udt_type != "omiga_inscription" && !udt.published)

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)
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_dao_transactions, page: @page, page_size: @page_size, records_counter: records_counter).call
options = FastJsonapi::PaginationMetaGenerator.new(request:, records: ckb_dao_transactions, page: @page, page_size: @page_size, records_counter:).call
CkbTransactionsSerializer.new(ckb_dao_transactions, options.merge(params: { previews: true })).serialized_json
end

render json: json
render json:
end

private
Expand All @@ -34,7 +34,7 @@ def validate_query_params
errors = validator.error_object[:errors]
status = validator.error_object[:status]

render json: errors, status: status
render json: errors, status:
end
end

Expand Down
12 changes: 6 additions & 6 deletions app/controllers/api/v1/udt_transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class UdtTransactionsController < ApplicationController
before_action :validate_query_params, :validate_pagination_params, :pagination_params

def show
udt = Udt.find_by(type_hash: params[:id], published: true)
raise Api::V1::Exceptions::UdtNotFoundError if udt.blank?
udt = Udt.find_by(type_hash: params[:id])
raise Api::V1::Exceptions::UdtNotFoundError if udt.blank? || (udt.udt_type != "omiga_inscription" && !udt.published)

ckb_transactions = udt.ckb_transactions.tx_committed.
select(:id, :tx_hash, :block_id, :block_number,
Expand All @@ -29,16 +29,16 @@ def show

ckb_transactions = ckb_transactions.page(@page).per(@page_size).fast_page
options = FastJsonapi::PaginationMetaGenerator.new(
request: request,
request:,
records: ckb_transactions,
page: @page,
page_size: @page_size
page_size: @page_size,
).call
json = CkbTransactionsSerializer.new(
ckb_transactions, options.merge(params: { previews: true })
).serialized_json

render json: json
render json:
end
end

Expand All @@ -51,7 +51,7 @@ def validate_query_params
errors = validator.error_object[:errors]
status = validator.error_object[:status]

render json: errors, status: status
render json: errors, status:
end
end

Expand Down
52 changes: 31 additions & 21 deletions test/controllers/api/v1/address_udt_transactions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,26 @@ class AddressUdtTransactionsControllerTest < ActionDispatch::IntegrationTest
page = 1
page_size = 10
udt = create(:udt, published: true)
address = create(:address, :with_udt_transactions, udt: udt)
address = create(:address, :with_udt_transactions, udt:)
ckb_udt_transactions = address.ckb_udt_transactions(udt.id).recent.page(page).per(page_size)

valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash)

options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: ckb_udt_transactions, page: page, page_size: page_size).call
options = FastJsonapi::PaginationMetaGenerator.new(request:, records: ckb_udt_transactions, page:, page_size:).call

assert_equal CkbTransactionsSerializer.new(ckb_udt_transactions, options.merge(params: { previews: true })).serialized_json, response.body
end

test "should contain right keys in the serialized object when call show" do
udt = create(:udt, published: true)
address = create(:address, :with_udt_transactions, udt: udt)
address = create(:address, :with_udt_transactions, udt:)

valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash)

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 created_at create_timestamp).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 All @@ -120,7 +121,7 @@ class AddressUdtTransactionsControllerTest < ActionDispatch::IntegrationTest

test "should return error object when page param is invalid" do
udt = create(:udt, published: true)
address = create(:address, :with_udt_transactions, udt: udt)
address = create(:address, :with_udt_transactions, udt:)
error_object = Api::V1::Exceptions::PageParamError.new
response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json

Expand All @@ -131,7 +132,7 @@ class AddressUdtTransactionsControllerTest < ActionDispatch::IntegrationTest

test "should return error object when page size param is invalid" do
udt = create(:udt, published: true)
address = create(:address, :with_udt_transactions, udt: udt)
address = create(:address, :with_udt_transactions, udt:)
error_object = Api::V1::Exceptions::PageSizeParamError.new
response_json = RequestErrorSerializer.new([error_object], message: error_object.title).serialized_json

Expand All @@ -143,7 +144,7 @@ class AddressUdtTransactionsControllerTest < ActionDispatch::IntegrationTest
test "should return error object when page and page size param are invalid" do
errors = []
udt = create(:udt, published: true)
address = create(:address, :with_udt_transactions, udt: udt)
address = create(:address, :with_udt_transactions, udt:)
errors << Api::V1::Exceptions::PageParamError.new
errors << Api::V1::Exceptions::PageSizeParamError.new
response_json = RequestErrorSerializer.new(errors, message: errors.first.title).serialized_json
Expand All @@ -155,7 +156,7 @@ class AddressUdtTransactionsControllerTest < ActionDispatch::IntegrationTest

test "should return 10 records when page and page_size are not set" do
udt = create(:udt, published: true)
address = create(:address, :with_udt_transactions, transactions_count: 15, udt: udt)
address = create(:address, :with_udt_transactions, transactions_count: 15, udt:)

valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash)

Expand All @@ -166,13 +167,13 @@ class AddressUdtTransactionsControllerTest < ActionDispatch::IntegrationTest
page = 2
page_size = 10
udt = create(:udt, published: true)
address = create(:address, :with_udt_transactions, transactions_count: 30, udt: udt)
address = create(:address, :with_udt_transactions, transactions_count: 30, udt:)

address_udt_transactions = address.ckb_udt_transactions(udt.id).recent.recent.page(page).per(page_size)

valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash), params: { page: page }
valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash), params: { page: }

options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: address_udt_transactions, page: page, page_size: page_size).call
options = FastJsonapi::PaginationMetaGenerator.new(request:, records: address_udt_transactions, page:, page_size:).call
response_transaction = CkbTransactionsSerializer.new(address_udt_transactions, options.merge(params: { previews: true })).serialized_json

assert_equal response_transaction, response.body
Expand All @@ -183,12 +184,12 @@ class AddressUdtTransactionsControllerTest < ActionDispatch::IntegrationTest
page = 1
page_size = 12
udt = create(:udt, published: true)
address = create(:address, :with_udt_transactions, transactions_count: 15, udt: udt)
address = create(:address, :with_udt_transactions, transactions_count: 15, udt:)
address_udt_transactions = address.ckb_udt_transactions(udt.id).recent.page(page).per(page_size)

valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash), params: { page_size: page_size }
valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash), params: { page_size: }

options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: address_udt_transactions, page: page, page_size: page_size).call
options = FastJsonapi::PaginationMetaGenerator.new(request:, records: address_udt_transactions, page:, page_size:).call
response_transaction = CkbTransactionsSerializer.new(address_udt_transactions, options.merge(params: { previews: true })).serialized_json

assert_equal response_transaction, response.body
Expand All @@ -199,11 +200,11 @@ class AddressUdtTransactionsControllerTest < ActionDispatch::IntegrationTest
page = 2
page_size = 5
udt = create(:udt, published: true)
address = create(:address, :with_udt_transactions, transactions_count: 30, udt: udt)
address = create(:address, :with_udt_transactions, transactions_count: 30, udt:)
address_udt_transactions = address.ckb_udt_transactions(udt.id).order("block_timestamp desc nulls last, id desc").page(page).per(page_size)

valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash), params: { page: page, page_size: page_size }
options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: address_udt_transactions, page: page, page_size: page_size).call
valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash), params: { page:, page_size: }
options = FastJsonapi::PaginationMetaGenerator.new(request:, records: address_udt_transactions, page:, page_size:).call
response_transaction = CkbTransactionsSerializer.new(address_udt_transactions, options.merge(params: { previews: true })).serialized_json

assert_equal response_transaction, response.body
Expand All @@ -213,13 +214,13 @@ class AddressUdtTransactionsControllerTest < ActionDispatch::IntegrationTest
page = 20
page_size = 5
udt = create(:udt, published: true)
address = create(:address, :with_udt_transactions, udt: udt)
address = create(:address, :with_udt_transactions, udt:)

address_udt_transactions = address.ckb_udt_transactions(udt.id).recent.page(page).per(page_size)

valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash), params: { page: page, page_size: page_size }
valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash), params: { page:, page_size: }

options = FastJsonapi::PaginationMetaGenerator.new(request: request, records: address_udt_transactions, page: page, page_size: page_size).call
options = FastJsonapi::PaginationMetaGenerator.new(request:, records: address_udt_transactions, page:, page_size:).call
response_transaction = CkbTransactionsSerializer.new(address_udt_transactions, options.merge(params: { previews: true })).serialized_json

assert_equal [], json["data"]
Expand All @@ -228,7 +229,16 @@ class AddressUdtTransactionsControllerTest < ActionDispatch::IntegrationTest

test "should return meta that contained total in response body" do
udt = create(:udt, published: true)
address = create(:address, :with_udt_transactions, transactions_count: 3, udt: udt)
address = create(:address, :with_udt_transactions, transactions_count: 3, udt:)

valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash)

assert_equal 6, json.dig("meta", "total")
end

test "should return meta if udt is omiga_inscription" do
udt = create(:udt, udt_type: :omiga_inscription, published: false)
address = create(:address, :with_udt_transactions, transactions_count: 3, udt:)

valid_get api_v1_address_udt_transaction_url(address.address_hash, type_hash: udt.type_hash)

Expand Down
Loading