From 70cf55947519fb50a7e94ce6a93263c41d027d64 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Tue, 12 Mar 2024 13:30:59 +0800 Subject: [PATCH] feat: return contract resource distributed Signed-off-by: Miles Zhang --- .../api/v2/statistics_controller.rb | 18 +++++++++++++++- app/models/contract.rb | 1 + app/models/referring_cell.rb | 1 - app/workers/contract_statistic_worker.rb | 3 ++- config/routes/v2.rb | 1 + ...2050057_add_addresses_count_to_contract.rb | 5 +++++ db/structure.sql | 13 ++++-------- .../api/v2/statistics_controller_test.rb | 21 ++++++++++++++----- test/factories/contract.rb | 2 ++ 9 files changed, 48 insertions(+), 17 deletions(-) create mode 100644 db/migrate/20240312050057_add_addresses_count_to_contract.rb diff --git a/app/controllers/api/v2/statistics_controller.rb b/app/controllers/api/v2/statistics_controller.rb index 8794e353b6..a06028299e 100644 --- a/app/controllers/api/v2/statistics_controller.rb +++ b/app/controllers/api/v2/statistics_controller.rb @@ -7,8 +7,24 @@ def transaction_fees 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 + last_n_days_transaction_fee_rates: stats_info.last_n_days_transaction_fee_rates, } end + + def contract_resource_distributed + expires_in 10.minutes, public: true + + json = Contract.all.map do |contract| + { + name: contract.name, + code_hash: contract.code_hash, + tx_count: contract.ckb_transactions_count, + capacity_amount: contract.total_referring_cells_capacity, + address_count: contract.addresses_count, + } + end + + render json: + end end end diff --git a/app/models/contract.rb b/app/models/contract.rb index f6aeee8123..d92c5078ec 100644 --- a/app/models/contract.rb +++ b/app/models/contract.rb @@ -42,6 +42,7 @@ def self.create_initial_data # referring_cells_count :decimal(30, ) default(0) # total_deployed_cells_capacity :decimal(30, ) default(0) # total_referring_cells_capacity :decimal(30, ) default(0) +# addresses_count :integer # # Indexes # diff --git a/app/models/referring_cell.rb b/app/models/referring_cell.rb index 2deaba7d15..9ae25ebf22 100644 --- a/app/models/referring_cell.rb +++ b/app/models/referring_cell.rb @@ -56,6 +56,5 @@ def self.create_initial_data_for_ckb_transaction(ckb_transaction) # # Indexes # -# index_referring_cells_on_cell_output_id (cell_output_id) UNIQUE # index_referring_cells_on_contract_id_and_cell_output_id (contract_id,cell_output_id) UNIQUE # diff --git a/app/workers/contract_statistic_worker.rb b/app/workers/contract_statistic_worker.rb index 1324ee8253..8ad6968a4a 100644 --- a/app/workers/contract_statistic_worker.rb +++ b/app/workers/contract_statistic_worker.rb @@ -9,7 +9,8 @@ def perform 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), - total_referring_cells_capacity: contract.referring_cell_outputs&.live&.sum(:capacity) + total_referring_cells_capacity: contract.referring_cell_outputs&.live&.sum(:capacity), + addresses_count: contract.referring_cell_outputs&.live&.select(:address_id)&.distinct&.count, ) end end diff --git a/config/routes/v2.rb b/config/routes/v2.rb index d0810fb005..4fd87260cf 100644 --- a/config/routes/v2.rb +++ b/config/routes/v2.rb @@ -74,6 +74,7 @@ resources :statistics, only: [] do collection do get :transaction_fees + get :contract_resource_distributed end end diff --git a/db/migrate/20240312050057_add_addresses_count_to_contract.rb b/db/migrate/20240312050057_add_addresses_count_to_contract.rb new file mode 100644 index 0000000000..303e8c21cd --- /dev/null +++ b/db/migrate/20240312050057_add_addresses_count_to_contract.rb @@ -0,0 +1,5 @@ +class AddAddressesCountToContract < ActiveRecord::Migration[7.0] + def change + add_column :contracts, :addresses_count, :integer + end +end diff --git a/db/structure.sql b/db/structure.sql index 2f4218c289..5b0aaca49d 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1191,7 +1191,8 @@ CREATE TABLE public.contracts ( deployed_cells_count numeric(30,0) DEFAULT 0.0, 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 + total_referring_cells_capacity numeric(30,0) DEFAULT 0.0, + addresses_count integer ); @@ -4371,13 +4372,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: - -- @@ -5190,6 +5184,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20240228102716'), ('20240301025505'), ('20240305100337'), -('20240311143030'); +('20240311143030'), +('20240312050057'); diff --git a/test/controllers/api/v2/statistics_controller_test.rb b/test/controllers/api/v2/statistics_controller_test.rb index 42c7746c6c..67fb636a4b 100644 --- a/test/controllers/api/v2/statistics_controller_test.rb +++ b/test/controllers/api/v2/statistics_controller_test.rb @@ -14,9 +14,9 @@ class StatisticsControllerTest < ActionDispatch::IntegrationTest tx_hash1 = "0x497277029e6335c6d5f916574dc4475ee229f3c1cce3658e7dad017a8ed580d4" tx_hash2 = "0xe9772bae467924e0feee85e9b7087993d38713bd8c19c954c4b68da69b4f4644" create :ckb_transaction, created_at: Time.at(tx_created_at), - transaction_fee: 30000, bytes: 20, confirmation_time: confirmation_time, block: block, tx_hash: tx_hash1 + transaction_fee: 30000, bytes: 20, confirmation_time:, block:, tx_hash: tx_hash1 create :ckb_transaction, created_at: Time.at(tx_created_at), - transaction_fee: 30000, bytes: 20, confirmation_time: confirmation_time, block: block, tx_hash: tx_hash2 + transaction_fee: 30000, bytes: 20, confirmation_time:, block:, tx_hash: tx_hash2 create :pending_transaction, transaction_fee: 30000, bytes: 20, tx_hash: tx_hash1 create :pending_transaction, transaction_fee: 13000, bytes: 15, @@ -29,7 +29,8 @@ class StatisticsControllerTest < ActionDispatch::IntegrationTest get transaction_fees_api_v2_statistics_url, headers: { "Content-Type": "application/vnd.api+json", - "Accept": "application/json" } + "Accept": "application/json", + } data = JSON.parse(response.body) assert_equal CkbTransaction.tx_pending.count, data["transaction_fee_rates"].size @@ -45,7 +46,8 @@ class StatisticsControllerTest < ActionDispatch::IntegrationTest get transaction_fees_api_v2_statistics_url, headers: { "Content-Type": "application/vnd.api+json", - "Accept": "application/json" } + "Accept": "application/json", + } data = JSON.parse(response.body) assert_equal CkbTransaction.tx_pending.count, data["transaction_fee_rates"].size @@ -76,7 +78,8 @@ class StatisticsControllerTest < ActionDispatch::IntegrationTest get transaction_fees_api_v2_statistics_url, headers: { "Content-Type": "application/vnd.api+json", - "Accept": "application/json" } + "Accept": "application/json", + } data = JSON.parse(response.body) assert_equal 3, data["last_n_days_transaction_fee_rates"].size @@ -86,6 +89,14 @@ class StatisticsControllerTest < ActionDispatch::IntegrationTest assert_response :success end end + + test "return contracts resource distributed" do + create_list(:contract, 3) + get contract_resource_distributed_api_v2_statistics_url + data = JSON.parse(response.body) + + assert_equal 3, data.size + end end end end diff --git a/test/factories/contract.rb b/test/factories/contract.rb index 03c1908868..8e9114029a 100644 --- a/test/factories/contract.rb +++ b/test/factories/contract.rb @@ -10,6 +10,8 @@ verified { false } deprecated { false } total_referring_cells_capacity { SecureRandom.random_number(10**10) } + ckb_transactions_count { SecureRandom.random_number(10**10) } + addresses_count { SecureRandom.random_number(100_000_000) } after(:create) do |contract, _eval| tx = create :ckb_transaction, :with_single_output