Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/rgbpp top holders #2384

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Api
module V2
class RgbppAssetsStatisticsController < BaseController
class RgbAssetsStatisticsController < BaseController

Check warning on line 3 in app/controllers/api/v2/rgb_assets_statistics_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/rgb_assets_statistics_controller.rb#L3

Added line #L3 was not covered by tests
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

Check warning on line 5 in app/controllers/api/v2/rgb_top_holders_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/rgb_top_holders_controller.rb#L1-L5

Added lines #L1 - L5 were not covered by tests

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

Check warning on line 8 in app/controllers/api/v2/rgb_top_holders_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/rgb_top_holders_controller.rb#L7-L8

Added lines #L7 - L8 were not covered by tests

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

Check warning on line 11 in app/controllers/api/v2/rgb_top_holders_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/rgb_top_holders_controller.rb#L10-L11

Added lines #L10 - L11 were not covered by tests

render json: { data: top10 }
end

Check warning on line 14 in app/controllers/api/v2/rgb_top_holders_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/rgb_top_holders_controller.rb#L13-L14

Added lines #L13 - L14 were not covered by tests

private

Check warning on line 16 in app/controllers/api/v2/rgb_top_holders_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/rgb_top_holders_controller.rb#L16

Added line #L16 was not covered by tests

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 }).
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are accounts with zero balance in this table? If so, append a filter amount > 0 could speed up this query

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are accounts with zero balance in this table? If so, append a filter amount > 0 could speed up this query

Added filter condition: 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)

Check warning on line 24 in app/controllers/api/v2/rgb_top_holders_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/rgb_top_holders_controller.rb#L18-L24

Added lines #L18 - L24 were not covered by tests

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

Check warning on line 31 in app/controllers/api/v2/rgb_top_holders_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/rgb_top_holders_controller.rb#L26-L31

Added lines #L26 - L31 were not covered by tests

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

Check warning on line 45 in app/controllers/api/v2/rgb_top_holders_controller.rb

View check run for this annotation

Codecov / codecov/patch

app/controllers/api/v2/rgb_top_holders_controller.rb#L33-L45

Added lines #L33 - L45 were not covered by tests
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
Loading