-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add udt holder allocation * chore: update db structure * chore:adjust holder allocation filtering criteria * fix: iteration variable for allocation_data * chore: remove ckb_holder_count
- Loading branch information
Showing
11 changed files
with
186 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class UdtHolderAllocation < ApplicationRecord | ||
belongs_to :udt | ||
belongs_to :contract, optional: true | ||
end | ||
|
||
# == Schema Information | ||
# | ||
# Table name: udt_holder_allocations | ||
# | ||
# id :bigint not null, primary key | ||
# udt_id :bigint not null | ||
# contract_id :bigint | ||
# ckb_holder_count :integer default(0), not null | ||
# btc_holder_count :integer default(0), not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_udt_holder_allocations_on_udt_id (udt_id) | ||
# |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
class GenerateUdtHolderAllocationWorker | ||
include Sidekiq::Worker | ||
sidekiq_options retry: 3 | ||
|
||
def perform(type_hash) | ||
udt = Udt.find_by(type_hash:) | ||
return unless udt | ||
|
||
update_udt_holder_allocation(udt) | ||
update_contract_holder_allocation(udt) | ||
end | ||
|
||
def update_udt_holder_allocation(udt) | ||
type_script = TypeScript.find_by(udt.type_script) | ||
return unless type_script | ||
|
||
holder_allocation = UdtHolderAllocation.find_or_initialize_by(udt:, contract_id: nil) | ||
ckb_address_ids = CellOutput.live.where(type_script:).distinct.pluck(:address_id) | ||
btc_address_ids = [] | ||
ckb_address_ids.each_slice(1000) do |address_ids| | ||
ids = BitcoinAddressMapping.where(ckb_address_id: address_ids).pluck(:bitcoin_address_id) | ||
btc_address_ids.concat(ids).uniq! | ||
end | ||
|
||
holder_allocation.update!(btc_holder_count: btc_address_ids.count) | ||
end | ||
|
||
def update_contract_holder_allocation(udt) | ||
type_script = TypeScript.find_by(udt.type_script) | ||
return unless type_script | ||
|
||
unique_ckb_address_ids = [] | ||
CellOutput.live.where(type_script:).find_in_batches(batch_size: 1000) do |batch| | ||
batch_ckb_address_ids = batch.pluck(:address_id) | ||
excluded_ckb_address_ids = BitcoinAddressMapping.where(ckb_address_id: batch_ckb_address_ids).pluck(:ckb_address_id) | ||
filtered_ckb_address_ids = batch_ckb_address_ids - excluded_ckb_address_ids | ||
unique_ckb_address_ids.concat(filtered_ckb_address_ids).uniq! | ||
end | ||
|
||
allocation_data = {} | ||
unique_ckb_address_ids.each_slice(1000) do |batch_address_ids| | ||
holder_count = CellOutput.joins(:lock_script). | ||
where(address_id: batch_address_ids). | ||
group("lock_scripts.code_hash"). | ||
count("DISTINCT cell_outputs.address_id") | ||
|
||
holder_count.each do |code_hash, count| | ||
allocation_data[code_hash] ||= 0 | ||
allocation_data[code_hash] += count | ||
end | ||
end | ||
|
||
allocation_data.each do |code_hash, count| | ||
contract = Contract.find_by(code_hash:, role: ["LockScript", "lock_script"]) | ||
next unless contract | ||
|
||
allocation = UdtHolderAllocation.find_or_initialize_by(udt:, contract:) | ||
allocation.update!(ckb_holder_count: count) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
db/migrate/20240625032839_create_udt_holder_allocations.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateUdtHolderAllocations < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :udt_holder_allocations do |t| | ||
t.bigint :udt_id, null: false, index: true | ||
t.bigint :contract_id | ||
t.integer :ckb_holder_count, null: false, default: 0 | ||
t.integer :btc_holder_count, null: false, default: 0 | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FactoryBot.define do | ||
factory :udt_holder_allocation do | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "test_helper" | ||
|
||
class UdtHolderAllocationTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |