Skip to content

Commit

Permalink
feat: return contract resource distributed
Browse files Browse the repository at this point in the history
Signed-off-by: Miles Zhang <[email protected]>
  • Loading branch information
zmcNotafraid committed Mar 12, 2024
1 parent e38725a commit 70cf559
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 17 deletions.
18 changes: 17 additions & 1 deletion app/controllers/api/v2/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions app/models/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
1 change: 0 additions & 1 deletion app/models/referring_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
3 changes: 2 additions & 1 deletion app/workers/contract_statistic_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/routes/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
resources :statistics, only: [] do
collection do
get :transaction_fees
get :contract_resource_distributed
end
end

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240312050057_add_addresses_count_to_contract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAddressesCountToContract < ActiveRecord::Migration[7.0]
def change
add_column :contracts, :addresses_count, :integer
end
end
13 changes: 4 additions & 9 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);


Expand Down Expand Up @@ -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: -
--
Expand Down Expand Up @@ -5190,6 +5184,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20240228102716'),
('20240301025505'),
('20240305100337'),
('20240311143030');
('20240311143030'),
('20240312050057');


21 changes: 16 additions & 5 deletions test/controllers/api/v2/statistics_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
2 changes: 2 additions & 0 deletions test/factories/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 70cf559

Please sign in to comment.