Skip to content

Commit

Permalink
Merge pull request #2403 from nervosnetwork/testnet
Browse files Browse the repository at this point in the history
Deploy to mainnet
  • Loading branch information
rabbitz authored Jan 13, 2025
2 parents 7c4459b + 004298c commit 6e3023c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Api
module V2
class RgbppAssetsStatisticsController < BaseController
class RgbAssetsStatisticsController < BaseController
def index
expires_in 15.minutes, public: true, stale_while_revalidate: 5.minutes, stale_if_error: 5.minutes

Expand Down
45 changes: 45 additions & 0 deletions app/controllers/api/v2/rgb_top_holders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Api
module V2
class RgbTopHoldersController < BaseController
def show
expires_in 15.minutes, public: true, stale_while_revalidate: 5.minutes, stale_if_error: 5.minutes

udt = Udt.find_by(udt_type: %i[xudt xudt_compatible], type_hash: params[:id])
return head :not_found unless udt

merged_array = btc_top_holders(udt) + ckb_top_holders(udt)
top10 = merged_array.sort_by { |item| -item[:amount].to_f }.take(10)

render json: { data: top10 }
end

private

def btc_top_holders(udt)
result = BitcoinAddressMapping.
joins("LEFT OUTER JOIN udt_accounts ON udt_accounts.address_id = bitcoin_address_mappings.ckb_address_id").
where(udt_accounts: { udt_id: udt.id }).where("udt_accounts.amount > 0").
group("bitcoin_address_mappings.bitcoin_address_id").
select("bitcoin_address_mappings.bitcoin_address_id, SUM(udt_accounts.amount) AS total_amount").
order("total_amount DESC").limit(10)

result.map do |record|
address_hash = BitcoinAddress.find_by(id: record.bitcoin_address_id).address_hash
position_ratio = udt.total_amount.zero? ? 0 : format("%.5f", record.total_amount.to_f / udt.total_amount)
{ address_hash:, amount: record.total_amount.to_s, position_ratio: position_ratio.to_s, network: "btc" }
end
end

def ckb_top_holders(udt)
UdtAccount.joins("LEFT OUTER JOIN bitcoin_address_mappings ON udt_accounts.address_id = bitcoin_address_mappings.ckb_address_id").
where(udt_accounts: { udt_id: udt.id}, bitcoin_address_mappings: { bitcoin_address_id: nil }).
where("udt_accounts.amount > 0").
order("udt_accounts.amount desc").limit(10).map do |udt_account|
address_hash = udt_account.address.address_hash
position_ratio = udt.total_amount.zero? ? 0 : format("%.5f", udt_account.amount.to_f / udt.total_amount)
{ address_hash:, amount: udt_account.amount.to_s, position_ratio: position_ratio.to_s, network: "ckb" }
end
end
end
end
end
2 changes: 0 additions & 2 deletions app/services/charts/daily_statistic_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ def call
daily_statistic.from_scratch = from_scratch
daily_statistic.reset!(updated_attrs)
daily_statistic
rescue StandardError => e
Rails.logger.error "Error occurred during DailyStatisticGenerator error: #{e.message}"
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/workers/charts/daily_statistic.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Charts
class DailyStatistic
include Sidekiq::Worker
sidekiq_options queue: "critical", backtrace: 20
sidekiq_options queue: "critical"

# iterate from the creation timestamp of last daily statistic record to now day by day
# and generate daily statistic record for each day
Expand Down
3 changes: 2 additions & 1 deletion config/routes/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
resources :graph_channels, only: :index
end
resources :udt_hourly_statistics, only: :show
resources :rgbpp_assets_statistics, only: :index
resources :rgb_assets_statistics, only: :index
resources :rgb_top_holders, only: :show
end
end

0 comments on commit 6e3023c

Please sign in to comment.