Skip to content

Commit

Permalink
feat: return holder count of daily statistic (#2033)
Browse files Browse the repository at this point in the history
Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid authored Jul 16, 2024
1 parent 6a65b3b commit 51c08f9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
9 changes: 8 additions & 1 deletion app/models/daily_statistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class DailyStatistic < ApplicationRecord
transactions_count addresses_count total_dao_deposit live_cells_count dead_cells_count avg_hash_rate avg_difficulty uncle_rate
total_depositors_count address_balance_distribution total_tx_fee occupied_capacity daily_dao_deposit daily_dao_depositors_count
circulation_ratio daily_dao_withdraw nodes_count circulating_supply burnt locked_capacity treasury_amount mining_reward
deposit_compensation liquidity created_at_unixtimestamp ckb_hodl_wave
deposit_compensation liquidity created_at_unixtimestamp ckb_hodl_wave holder_count
).freeze
MILLISECONDS_IN_DAY = BigDecimal(24 * 60 * 60 * 1000)
GENESIS_TIMESTAMP = 1573852190812
Expand Down Expand Up @@ -398,6 +398,12 @@ def liquidity
base_attrs.merge(total_supply: total_live_capacities).transform_values { |value| (value.to_f / 10**8).truncate(8) }
end

define_logic :holder_count do
live_address_ids = CellOutput.live.generated_before(to_be_counted_date.to_i * 1000 - 1).pluck(:address_id)
dead_address_ids = CellOutput.dead.generated_before(to_be_counted_date.to_i * 1000 - 1).consumed_after(to_be_counted_date.to_i * 1000).pluck(:address_id)
(live_address_ids + dead_address_ids).uniq.count
end

private

def to_be_counted_date
Expand Down Expand Up @@ -558,6 +564,7 @@ def aggron_first_day?
# nodes_count :integer
# locked_capacity :decimal(30, )
# ckb_hodl_wave :jsonb
# holder_count :integer
#
# Indexes
#
Expand Down
6 changes: 6 additions & 0 deletions app/serializers/daily_statistic_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,10 @@ class DailyStatisticSerializer
attribute :ckb_hodl_wave, if: Proc.new { |_record, params|
params.present? && params[:indicator].include?("ckb_hodl_wave")
}

attribute :holder_count, if: Proc.new { |_record, params|
params.present? && params[:indicator].include?("holder_count")
} do |object|
object.holder_count.to_s
end
end
2 changes: 1 addition & 1 deletion app/services/charts/daily_statistic_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def updated_attrs
treasury_amount estimated_apc live_cells_count dead_cells_count avg_hash_rate
avg_difficulty uncle_rate address_balance_distribution
total_tx_fee occupied_capacity daily_dao_deposit total_supply block_time_distribution
epoch_time_distribution epoch_length_distribution locked_capacity ckb_hodl_wave
epoch_time_distribution epoch_length_distribution locked_capacity ckb_hodl_wave holder_count
}

established_order + others
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddHolderCountToDailyStatistic < ActiveRecord::Migration[7.0]
def change
add_column :daily_statistics, :holder_count, :integer
end
end
4 changes: 3 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,8 @@ CREATE TABLE public.daily_statistics (
nodes_distribution jsonb,
nodes_count integer,
locked_capacity numeric(30,0),
ckb_hodl_wave jsonb
ckb_hodl_wave jsonb,
holder_count integer
);


Expand Down Expand Up @@ -6093,6 +6094,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20240513055849'),
('20240620083123'),
('20240625032839'),
('20240704092919'),
('20240709131020'),
('20240709131132'),
('20240709131713'),
Expand Down
14 changes: 14 additions & 0 deletions test/services/charts/daily_statistic_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -468,5 +468,19 @@ class DailyStatisticGeneratorTest < ActiveSupport::TestCase
locked_capacity = Charts::DailyStatisticGenerator.new(@datetime).call.locked_capacity
assert_equal locked_capacity_temp, locked_capacity
end

test "it should get holder_count" do
address1 = create(:address)
address2 = create(:address)
consumed_tx = create(:ckb_transaction)

create(:cell_output, :with_full_transaction,
block_timestamp: @datetime.beginning_of_day.to_i * 1000 - 10000, block: @block, address_id: address1.id)
create(:cell_output, :with_full_transaction,
block_timestamp: @datetime.beginning_of_day.to_i * 1000 - 10000, block: @block, address_id: address2.id, status: :dead, consumed_block_timestamp: @datetime.beginning_of_day.to_i * 1000 + 10000, consumed_by_id: consumed_tx.id)

holder_count = Charts::DailyStatisticGenerator.new(@datetime).call.holder_count
assert_equal 2, holder_count
end
end
end

0 comments on commit 51c08f9

Please sign in to comment.