Skip to content

Commit

Permalink
Issue 424 3 (#1622)
Browse files Browse the repository at this point in the history
* feat: remove ckb_hodl_waves column from statistic_infos

Signed-off-by: Miles Zhang <[email protected]>

* feat: add ckb_hodl_waves to daily_statistic

Signed-off-by: Miles Zhang <[email protected]>

* feat: calculate ckb_hodl_wave and save to daily_statistics

Signed-off-by: Miles Zhang <[email protected]>

* fix: fix typo

Signed-off-by: Miles Zhang <[email protected]>

---------

Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid authored Feb 6, 2024
1 parent 7d65d7a commit 11621ff
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 120 deletions.
112 changes: 103 additions & 9 deletions app/models/daily_statistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ 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
deposit_compensation liquidity created_at_unixtimestamp ckb_hodl_wave
).freeze
MILLISECONDS_IN_DAY = BigDecimal(24 * 60 * 60 * 1000)
GENESIS_TIMESTAMP = 1573852190812
TESTNET_GENESIS_TIMESTAMP = 1589276230000

attr_accessor :from_scratch

Expand Down Expand Up @@ -260,9 +261,9 @@ def liquidity
millisecond_start = range[0] * 1000
millisecond_end = range[1] * 1000
block_count = Block.where(
number: start_block_number..tip_block_number
number: start_block_number..tip_block_number,
).where(
block_time: (millisecond_start + 1)..millisecond_end
block_time: (millisecond_start + 1)..millisecond_end,
).count
[range[1], block_count]
end
Expand All @@ -271,7 +272,7 @@ def liquidity
define_logic :epoch_time_distribution do
max_n = 119
ranges = [[0, 180]] + (180..(180 + max_n)).map { |n| [n, n + 1] }
ranges.each_with_index.map { |range, index|
ranges.each_with_index.map do |range, index|
milliseconds_start = range[0] * 60 * 1000
milliseconds_end = range[1] * 60 * 1000
condition =
Expand All @@ -285,7 +286,7 @@ def liquidity
epoch_count = ::EpochStatistic.where(epoch_time: condition).count

[range[1], epoch_count]
}.compact
end.compact
end

define_logic :epoch_length_distribution do
Expand All @@ -295,15 +296,15 @@ def liquidity
interval = 499
start_epoch_number = [0, tip_epoch_number - interval].max

ranges.each_with_index.map { |range, _index|
ranges.each_with_index.map do |range, _index|
epoch_count = ::EpochStatistic.where(
epoch_number: start_epoch_number..tip_epoch_number
epoch_number: start_epoch_number..tip_epoch_number,
).where(
epoch_length: (range[0] + 1)..range[1]
epoch_length: (range[0] + 1)..range[1],
).count

[range[1], epoch_count]
}.compact
end.compact
end

define_logic :locked_capacity do
Expand All @@ -316,6 +317,90 @@ def liquidity
market_data.bug_bounty_locked
end

define_logic :ckb_hodl_wave do
over_three_years =
if time_range_exceeded?(3.years)
CellOutput.live.generated_before(to_be_counted_date.years_ago(3).to_i * 1000).sum(:capacity) +
CellOutput.dead.generated_before(to_be_counted_date.years_ago(3).to_i * 1000).consumed_after(to_be_counted_date.years_ago(3).to_i * 1000).sum(:capacity)
else
0
end

one_year_to_three_years =
if time_range_exceeded?(1.year)
CellOutput.live.generated_between(
to_be_counted_date.years_ago(3).to_i * 1000, to_be_counted_date.years_ago(1).to_i * 1000
).sum(:capacity) + CellOutput.dead.generated_between(
to_be_counted_date.years_ago(3).to_i * 1000, to_be_counted_date.years_ago(1).to_i * 1000
).consumed_after(to_be_counted_date.years_ago(1).to_i * 1000).sum(:capacity)
else
0
end

six_months_to_one_year =
if time_range_exceeded?(6.months)
CellOutput.live.generated_between(
to_be_counted_date.years_ago(1).to_i * 1000, to_be_counted_date.months_ago(6).to_i * 1000
).sum(:capacity) + CellOutput.dead.generated_between(
to_be_counted_date.years_ago(1).to_i * 1000, to_be_counted_date.months_ago(6).to_i * 1000
).consumed_after(to_be_counted_date.months_ago(6).to_i * 1000).sum(:capacity)
else
0
end

three_months_to_six_months =
if time_range_exceeded?(3.months)
CellOutput.live.generated_between(
to_be_counted_date.months_ago(6).to_i * 1000, to_be_counted_date.months_ago(3).to_i * 1000
).sum(:capacity) + CellOutput.dead.generated_between(
to_be_counted_date.months_ago(6).to_i * 1000, to_be_counted_date.months_ago(3).to_i * 1000
).consumed_after(to_be_counted_date.months_ago(3).to_i * 1000).sum(:capacity)
else
0
end

one_month_to_three_months =
if time_range_exceeded?(1.month)
CellOutput.live.generated_between(
to_be_counted_date.months_ago(3).to_i * 1000, to_be_counted_date.months_ago(1).to_i * 1000
).sum(:capacity) + CellOutput.dead.generated_between(
to_be_counted_date.months_ago(3).to_i * 1000, to_be_counted_date.months_ago(1).to_i * 1000
).consumed_after(to_be_counted_date.months_ago(1).to_i * 1000).sum(:capacity)
else
0
end

one_week_to_one_month = CellOutput.live.generated_between(
to_be_counted_date.months_ago(1).to_i * 1000, to_be_counted_date.weeks_ago(1).to_i * 1000
).sum(:capacity) + CellOutput.dead.generated_between(
to_be_counted_date.months_ago(1).to_i * 1000, to_be_counted_date.weeks_ago(1).to_i * 1000
).consumed_after(to_be_counted_date.weeks_ago(1).to_i * 1000).sum(:capacity)

day_to_one_week = CellOutput.live.generated_between(
to_be_counted_date.weeks_ago(1).to_i * 1000, to_be_counted_date.days_ago(1).to_i * 1000
).sum(:capacity) + CellOutput.dead.generated_between(
to_be_counted_date.weeks_ago(1).to_i * 1000, to_be_counted_date.days_ago(1).to_i * 1000
).consumed_after(to_be_counted_date.days_ago(1).to_i * 1000).sum(:capacity)

latest_day = CellOutput.live.generated_between(
to_be_counted_date.days_ago(1).to_i * 1000, to_be_counted_date.to_i * 1000
).sum(:capacity) + CellOutput.dead.generated_between(
to_be_counted_date.days_ago(1).to_i * 1000, to_be_counted_date.to_i * 1000
).consumed_after(to_be_counted_date.to_i * 1000).sum(:capacity)

{
over_three_years:,
one_year_to_three_years:,
six_months_to_one_year:,
three_months_to_six_months:,
one_month_to_three_months:,
one_week_to_one_month:,
day_to_one_week:,
latest_day:,
total_supply:,
}.transform_values { |value| (value / 10**8).truncate(8) }
end

private

def to_be_counted_date
Expand All @@ -339,6 +424,14 @@ def current_tip_block
end
end

def time_range_exceeded?(time_range)
if CkbSync::Api.instance.mode == CKB::MODE::MAINNET
GENESIS_TIMESTAMP + time_range.to_i * 1000 <= started_at
else
TESTNET_GENESIS_TIMESTAMP + time_range.to_i * 1000 <= started_at
end
end

def phase1_dao_interests
@phase1_dao_interests ||=
begin
Expand Down Expand Up @@ -467,6 +560,7 @@ def aggron_first_day?
# nodes_distribution :jsonb
# nodes_count :integer
# locked_capacity :decimal(30, )
# ckb_hodl_wave :jsonb
#
# Indexes
#
Expand Down
1 change: 0 additions & 1 deletion app/models/statistic_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,4 @@ def tip_block
# updated_at :datetime not null
# pending_transaction_fee_rates :jsonb
# transaction_fee_rates :jsonb
# ckb_hodl_waves :jsonb
#
4 changes: 4 additions & 0 deletions app/serializers/daily_statistic_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,8 @@ class DailyStatisticSerializer
} do |object|
object.liquidity.to_s
end

attribute :ckb_hodl_wave, if: Proc.new { |_record, params|
params.present? && params[:indicator].include?("ckb_hodl_wave")
}
end
4 changes: 0 additions & 4 deletions app/serializers/statistic_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,4 @@ class StatisticSerializer
attribute :maintenance_info, if: Proc.new { |_record, params|
params && params[:info_name] == "maintenance_info"
}

attribute :ckb_hodl_waves, if: Proc.new { |_record, params|
params && params[:info_name] == "ckb_hodl_waves"
}
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
epoch_time_distribution epoch_length_distribution locked_capacity ckb_hodl_wave
}

established_order + others
Expand Down
49 changes: 0 additions & 49 deletions app/workers/charts/ckb_hodl_waves_statistic.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveCkbHodlWavesToStatisticInfo < ActiveRecord::Migration[7.0]
def change
remove_columns :statistic_infos, :ckb_hodl_waves
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCkbHodlWavesToDailyStatistic < ActiveRecord::Migration[7.0]
def change
add_column :daily_statistics, :ckb_hodl_wave, :jsonb
end
end
17 changes: 6 additions & 11 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,8 @@ CREATE TABLE public.daily_statistics (
average_block_time jsonb,
nodes_distribution jsonb,
nodes_count integer,
locked_capacity numeric(30,0)
locked_capacity numeric(30,0),
ckb_hodl_wave jsonb
);


Expand Down Expand Up @@ -1936,8 +1937,7 @@ CREATE TABLE public.statistic_infos (
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL,
pending_transaction_fee_rates jsonb,
transaction_fee_rates jsonb,
ckb_hodl_waves jsonb
transaction_fee_rates jsonb
);


Expand Down Expand Up @@ -4131,13 +4131,6 @@ CREATE INDEX index_pool_transaction_entries_on_tx_status ON public.pool_transact
CREATE UNIQUE INDEX index_portfolios_on_user_id_and_address_id ON public.portfolios USING btree (user_id, address_id);


--
-- Name: index_referring_cells_on_cell_output_id; Type: INDEX; Schema: public; Owner: -
--

CREATE UNIQUE INDEX index_referring_cells_on_cell_output_id ON public.referring_cells USING btree (cell_output_id);


--
-- Name: index_referring_cells_on_contract_id_and_cell_output_id; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -4943,6 +4936,8 @@ INSERT INTO "schema_migrations" (version) VALUES
('20231218082938'),
('20240107100346'),
('20240118103947'),
('20240119131328');
('20240119131328'),
('20240205023511'),
('20240205024238');


4 changes: 0 additions & 4 deletions lib/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def call_worker(clz)
call_worker Charts::DailyStatistic
end

s.cron "10 8 * * *" do
call_worker Charts::CkbHodlWavesStatistic
end

s.every "10m", overlap: false do
call_worker Charts::BlockStatistic
end
Expand Down
17 changes: 16 additions & 1 deletion test/controllers/api/v1/daily_statistics_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class DailyStatisticsControllerTest < ActionDispatch::IntegrationTest

assert_equal DailyStatisticSerializer.new(
::DailyStatistic.order(created_at_unixtimestamp: :asc).valid_indicators,
params: { indicator: "total_dao_deposit" }
params: { indicator: "total_dao_deposit" },
).serialized_json, response.body
end

Expand Down Expand Up @@ -131,6 +131,21 @@ class DailyStatisticsControllerTest < ActionDispatch::IntegrationTest
response.body
assert_equal 100, json.dig("data").size
end

test "should return ckb_hodl_wave" do
ckb_hodl_wave = { "over_three_years" => 19531171649.691193,
"one_year_to_three_years" => 23338346194.19826,
"six_months_to_one_year" => 19609620799.532352,
"three_months_to_six_months" => 2236264635.3570275,
"one_month_to_three_months" => 814754775.4523662,
"one_week_to_one_month" => 456541010.49045384,
"day_to_one_week" => 104631888.5063308,
"latest_day" => 22211617.27774267,
"total_supply" => 40845092357.49983 }
create(:daily_statistic, created_at_unixtimestamp: 1.day.ago.to_i, ckb_hodl_wave:)
valid_get api_v1_daily_statistic_url("ckb_hodl_wave")
assert_equal 1, json.dig("data").size
end
end
end
end
19 changes: 0 additions & 19 deletions test/controllers/api/v1/statistics_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,6 @@ class StatisticsControllerTest < ActionDispatch::IntegrationTest

assert_equal response_json, response.body
end

test "should return current ckb_hodl_waves when param is ckb_hodl_waves" do
ckb_hodl_waves = { "over_three_years" => 19531171649.691193,
"one_year_to_three_years" => 23338346194.19826,
"six_months_to_one_year" => 19609620799.532352,
"three_months_to_six_months" => 2236264635.3570275,
"one_month_to_three_months" => 814754775.4523662,
"one_week_to_one_month" => 456541010.49045384,
"day_to_one_week" => 104631888.5063308,
"latest_day" => 22211617.27774267,
"total_supply" => 40845092357.49983,
"updated_at" => 1702895323 }
create(:statistic_info, ckb_hodl_waves:)

valid_get api_v1_statistic_url("ckb_hodl_waves")

assert_equal ckb_hodl_waves,
json.dig("data", "attributes", "ckb_hodl_waves")
end
end
end
end
1 change: 0 additions & 1 deletion test/factories/statistic_infos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
miner_ranking { "" }
blockchain_info { "MyString" }
last_n_days_transaction_fee_rates { "" }
ckb_hodl_waves { "" }
end
end
Loading

0 comments on commit 11621ff

Please sign in to comment.