Skip to content

Commit

Permalink
feat: add btc address udt accounts (#2114)
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitz committed Aug 29, 2024
1 parent 476fd28 commit 855ce89
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/controllers/api/v2/bitcoin_addresses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,34 @@ def rgb_cells
render json: { data: { rgb_cells: cells }, meta: { total: bitcoin_vouts.total_count, page_size: @page_size } }
end

def udt_accounts
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)

cell_types = %w(udt xudt xudt_compatible)
cell_outputs = CellOutput.live.includes(:bitcoin_vout).where(cell_outputs: { address_id: address_ids, cell_type: cell_types }).
where.not(bitcoin_vouts: { status: "unbound" }).group(:cell_type, :type_hash).sum(:udt_amount)

udt_accounts = cell_outputs.map do |k, v|
udt = Udt.find_by(type_hash: k[1], published: true)
next unless udt

{
symbol: udt.symbol,
decimal: udt.decimal,
amount: v,
type_hash: k[1],
udt_icon_file: udt.icon_file,
udt_type: udt.udt_type,
udt_type_script: udt.type_script,
}
end.compact

render json: { data: { udt_accounts: } }
end

private

def set_pagination_params
Expand Down
1 change: 1 addition & 0 deletions config/routes/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
resources :bitcoin_statistics, only: :index
resources :bitcoin_addresses, only: :show do
get :rgb_cells, on: :member
get :udt_accounts, on: :member
end
resources :rgb_live_cells, only: :index
end
Expand Down

0 comments on commit 855ce89

Please sign in to comment.