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: generate h24_ckb_transactions_count of contract #1831

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 23 additions & 18 deletions app/controllers/api/v2/statistics_controller.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
module Api::V2
class StatisticsController < BaseController
def transaction_fees
expires_in 15.seconds, public: true
stats_info = StatisticInfo.default
if stale?(stats_info)
expires_in 15.seconds, public: true

render json: {
transaction_fee_rates: stats_info.transaction_fee_rates,
pending_transaction_fee_rates: stats_info.pending_transaction_fee_rates,
last_n_days_transaction_fee_rates: stats_info.last_n_days_transaction_fee_rates,
}
render json: {
transaction_fee_rates: stats_info.transaction_fee_rates,
pending_transaction_fee_rates: stats_info.pending_transaction_fee_rates,
last_n_days_transaction_fee_rates: stats_info.last_n_days_transaction_fee_rates,
}
end
end

def contract_resource_distributed
expires_in 30.minutes, public: true
contracts = Contract.filter_nil_hash_type
if stale?(contracts)
expires_in 30.minutes, public: true
json = contracts.map do |contract|
{
name: contract.name,
code_hash: contract.code_hash,
hash_type: contract.hash_type,
tx_count: contract.ckb_transactions_count,
h24_tx_count: contract.h24_ckb_transactions_count,
ckb_amount: (contract.total_referring_cells_capacity / 10**8).truncate(8),
address_count: contract.addresses_count,
}
end

json = Contract.filter_nil_hash_type.map do |contract|
{
name: contract.name,
code_hash: contract.code_hash,
hash_type: contract.hash_type,
tx_count: contract.ckb_transactions_count,
ckb_amount: (contract.total_referring_cells_capacity / 10**8).truncate(8),
address_count: contract.addresses_count,
}
render json:
end

render json:
end
end
end
1 change: 1 addition & 0 deletions app/models/ckb_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class CkbTransaction < ApplicationRecord
created_after(start_block_timestamp).created_before(end_block_timestamp)
}
scope :inner_block, ->(block_id) { where("block_id = ?", block_id) }
scope :h24, -> { where("block_timestamp >= ?", 24.hours.ago.to_i * 1000) }

after_commit :flush_cache
before_destroy :recover_dead_cell
Expand Down
1 change: 1 addition & 0 deletions app/models/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def self.create_initial_data
# total_deployed_cells_capacity :decimal(30, ) default(0)
# total_referring_cells_capacity :decimal(30, ) default(0)
# addresses_count :integer
# h24_ckb_transactions_count :integer
#
# Indexes
#
Expand Down
2 changes: 2 additions & 0 deletions app/workers/contract_statistic_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
sidekiq_options queue: "critical"

def perform
h24_tx_ids = CkbTransaction.h24.pluck(:id)

Check warning on line 6 in app/workers/contract_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/contract_statistic_worker.rb#L6

Added line #L6 was not covered by tests
Contract.find_each do |contract|
contract.update(
ckb_transactions_count: contract.cell_dependencies.count,
h24_ckb_transactions_count: contract.cell_dependencies.where(ckb_transaction_id: h24_tx_ids).count,

Check warning on line 10 in app/workers/contract_statistic_worker.rb

View check run for this annotation

Codecov / codecov/patch

app/workers/contract_statistic_worker.rb#L10

Added line #L10 was not covered by tests
deployed_cells_count: contract.deployed_cell_outputs&.live&.size,
referring_cells_count: contract.referring_cell_outputs&.live&.size,
total_deployed_cells_capacity: contract.deployed_cell_outputs&.live&.sum(:capacity),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddH24CkbTransactionsCountToContract < ActiveRecord::Migration[7.0]
def change
add_column :contracts, :h24_ckb_transactions_count, :integer
end
end
61 changes: 4 additions & 57 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -624,39 +624,6 @@ CREATE SEQUENCE public.bitcoin_statistics_id_seq
ALTER SEQUENCE public.bitcoin_statistics_id_seq OWNED BY public.bitcoin_statistics.id;


--
-- Name: bitcoin_time_locks; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.bitcoin_time_locks (
id bigint NOT NULL,
bitcoin_transaction_id bigint,
ckb_transaction_id bigint,
cell_output_id bigint,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);


--
-- Name: bitcoin_time_locks_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.bitcoin_time_locks_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: bitcoin_time_locks_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.bitcoin_time_locks_id_seq OWNED BY public.bitcoin_time_locks.id;


--
-- Name: bitcoin_transactions; Type: TABLE; Schema: public; Owner: -
--
Expand Down Expand Up @@ -1302,7 +1269,8 @@ CREATE TABLE public.contracts (
referring_cells_count numeric(30,0) DEFAULT 0.0,
total_deployed_cells_capacity numeric(30,0) DEFAULT 0.0,
total_referring_cells_capacity numeric(30,0) DEFAULT 0.0,
addresses_count integer
addresses_count integer,
h24_ckb_transactions_count integer
);


Expand Down Expand Up @@ -2703,13 +2671,6 @@ ALTER TABLE ONLY public.bitcoin_addresses ALTER COLUMN id SET DEFAULT nextval('p
ALTER TABLE ONLY public.bitcoin_statistics ALTER COLUMN id SET DEFAULT nextval('public.bitcoin_statistics_id_seq'::regclass);


--
-- Name: bitcoin_time_locks id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.bitcoin_time_locks ALTER COLUMN id SET DEFAULT nextval('public.bitcoin_time_locks_id_seq'::regclass);


--
-- Name: bitcoin_transactions id; Type: DEFAULT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -3102,14 +3063,6 @@ ALTER TABLE ONLY public.bitcoin_statistics
ADD CONSTRAINT bitcoin_statistics_pkey PRIMARY KEY (id);


--
-- Name: bitcoin_time_locks bitcoin_time_locks_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.bitcoin_time_locks
ADD CONSTRAINT bitcoin_time_locks_pkey PRIMARY KEY (id);


--
-- Name: bitcoin_transactions bitcoin_transactions_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -3885,13 +3838,6 @@ CREATE UNIQUE INDEX index_bitcoin_addresses_on_mapping ON public.bitcoin_address
CREATE UNIQUE INDEX index_bitcoin_statistics_on_timestamp ON public.bitcoin_statistics USING btree ("timestamp");


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

CREATE UNIQUE INDEX index_bitcoin_time_locks_on_cell ON public.bitcoin_time_locks USING btree (bitcoin_transaction_id, cell_output_id);


--
-- Name: index_bitcoin_transactions_on_txid; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -5157,6 +5103,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20240408082159'),
('20240415080556'),
('20240428085020'),
('20240429102325');
('20240429102325'),
('20240507041552');


Loading