Skip to content

Commit

Permalink
feat: query bitcoin address rgb cells
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitz committed Jun 4, 2024
1 parent 4962060 commit 29b361d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
32 changes: 32 additions & 0 deletions app/controllers/api/v2/bitcoin_addresses_controller.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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(1).per(10)

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).serialized_json }
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
6 changes: 5 additions & 1 deletion config/routes/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@

resources :rgb_transactions, only: :index
resources :bitcoin_statistics, only: :index
resources :bitcoin_addresses, only: :show
resources :bitcoin_addresses, only: :show do
member do
get :rgb_cells
end
end
end
end

0 comments on commit 29b361d

Please sign in to comment.