diff --git a/app/controllers/api/v2/bitcoin_addresses_controller.rb b/app/controllers/api/v2/bitcoin_addresses_controller.rb index 09ca71309..237357ada 100644 --- a/app/controllers/api/v2/bitcoin_addresses_controller.rb +++ b/app/controllers/api/v2/bitcoin_addresses_controller.rb @@ -1,6 +1,7 @@ module Api module V2 class BitcoinAddressesController < BaseController + before_action :set_pagination_params, only: %i[rgb_cells] def show expires_in 1.minute, public: true, must_revalidate: true, stale_while_revalidate: 10.seconds @@ -17,6 +18,37 @@ def show render json: { unbound_live_cells_count:, bound_live_cells_count: } end + + def rgb_cells + expires_in 1.minute, public: true, must_revalidate: true, stale_while_revalidate: 10.seconds + + address = Addresses::Explore.run!(key: params[:id]) + address_ids = address.map(&:id) + + bitcoin_vouts = BitcoinVout.joins(:cell_output). + where(cell_outputs: { status: "live" }, bitcoin_vouts: { address_id: address_ids }). + select(:bitcoin_transaction_id, :index). + group(:bitcoin_transaction_id, :index). + page(@page).per(@page_size) + + transaction_ids = bitcoin_vouts.map(&:bitcoin_transaction_id).uniq + transactions = BitcoinTransaction.where(id: transaction_ids).index_by(&:id) + + cells = bitcoin_vouts.each_with_object({}) do |vout, hash| + tx = transactions[vout.bitcoin_transaction_id] + vouts = BitcoinVout.where(bitcoin_transaction_id: vout.bitcoin_transaction_id, index: vout.index).includes(:cell_output) + hash[[tx.tx_hash, vout.index]] = vouts.map { |v| CellOutputSerializer.new(v.cell_output).serializable_hash } + end + + render json: { data: { rgb_cells: cells }, meta: { total: bitcoin_vouts.total_count, page_size: @page_size } } + end + + private + + def set_pagination_params + @page = params.fetch(:page, 1) + @page_size = params.fetch(:page_size, CellOutput.default_per_page) + end end end end diff --git a/app/interactions/addresses/ckb_transactions.rb b/app/interactions/addresses/ckb_transactions.rb index e8b1a57aa..2b21ceba0 100644 --- a/app/interactions/addresses/ckb_transactions.rb +++ b/app/interactions/addresses/ckb_transactions.rb @@ -15,11 +15,9 @@ def execute address_id = address.map(&:id) order_by, asc_or_desc = account_books_ordering - records = CkbTransaction.includes(:account_books).where( + records = CkbTransaction.tx_committed.joins(:account_books).where( account_books: { address_id: }, - ckb_transactions: { tx_status: "committed" }, - ).order(order_by => asc_or_desc). - page(page).per(page_size) + ).order(order_by => asc_or_desc).distinct.page(page).per(page_size) options = paginate_options(records, address_id) options.merge!(params: { previews: true, address: }) diff --git a/config/routes/v2.rb b/config/routes/v2.rb index 7a62f83a1..f983bf018 100644 --- a/config/routes/v2.rb +++ b/config/routes/v2.rb @@ -95,6 +95,8 @@ resources :rgb_transactions, only: :index resources :bitcoin_statistics, only: :index - resources :bitcoin_addresses, only: :show + resources :bitcoin_addresses, only: :show do + get :rgb_cells, on: :member + end end end