|
| 1 | +module Api |
| 2 | + module V2 |
| 3 | + class RgbTopHoldersController < BaseController |
| 4 | + def show |
| 5 | + expires_in 15.minutes, public: true, stale_while_revalidate: 5.minutes, stale_if_error: 5.minutes |
| 6 | + |
| 7 | + udt = Udt.find_by(udt_type: %i[xudt xudt_compatible], type_hash: params[:id]) |
| 8 | + return head :not_found unless udt |
| 9 | + |
| 10 | + merged_array = btc_top_holders(udt) + ckb_top_holders(udt) |
| 11 | + top10 = merged_array.sort_by { |item| -item[:amount].to_f }.take(10) |
| 12 | + |
| 13 | + render json: { data: top10 } |
| 14 | + end |
| 15 | + |
| 16 | + private |
| 17 | + |
| 18 | + def btc_top_holders(udt) |
| 19 | + result = BitcoinAddressMapping. |
| 20 | + joins("LEFT OUTER JOIN udt_accounts ON udt_accounts.address_id = bitcoin_address_mappings.ckb_address_id"). |
| 21 | + where(udt_accounts: { udt_id: udt.id }).where("udt_accounts.amount > 0"). |
| 22 | + group("bitcoin_address_mappings.bitcoin_address_id"). |
| 23 | + select("bitcoin_address_mappings.bitcoin_address_id, SUM(udt_accounts.amount) AS total_amount"). |
| 24 | + order("total_amount DESC").limit(10) |
| 25 | + |
| 26 | + result.map do |record| |
| 27 | + address_hash = BitcoinAddress.find_by(id: record.bitcoin_address_id).address_hash |
| 28 | + position_ratio = udt.total_amount.zero? ? 0 : format("%.5f", record.total_amount.to_f / udt.total_amount) |
| 29 | + { address_hash:, amount: record.total_amount.to_s, position_ratio: position_ratio.to_s, network: "btc" } |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + def ckb_top_holders(udt) |
| 34 | + UdtAccount.joins("LEFT OUTER JOIN bitcoin_address_mappings ON udt_accounts.address_id = bitcoin_address_mappings.ckb_address_id"). |
| 35 | + where(udt_accounts: { udt_id: udt.id}, bitcoin_address_mappings: { bitcoin_address_id: nil }). |
| 36 | + where("udt_accounts.amount > 0"). |
| 37 | + order("udt_accounts.amount desc").limit(10).map do |udt_account| |
| 38 | + address_hash = udt_account.address.address_hash |
| 39 | + position_ratio = udt.total_amount.zero? ? 0 : format("%.5f", udt_account.amount.to_f / udt.total_amount) |
| 40 | + { address_hash:, amount: udt_account.amount.to_s, position_ratio: position_ratio.to_s, network: "ckb" } |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + end |
| 45 | +end |
0 commit comments