From 2cf82b2f15a053c17b351fccf2b4149b88787d1a Mon Sep 17 00:00:00 2001 From: Rabbit Date: Tue, 30 Jan 2024 19:23:53 +0800 Subject: [PATCH] feat: add contract referring capacities (#1612) --- app/controllers/api/v2/scripts_controller.rb | 13 ++++++++++--- config/routes/v2.rb | 7 ++++--- test/controllers/api/v2/scripts_controller_test.rb | 12 ++++++++++-- test/factories/contract.rb | 2 ++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/v2/scripts_controller.rb b/app/controllers/api/v2/scripts_controller.rb index c9835741e..4d20ab7b2 100644 --- a/app/controllers/api/v2/scripts_controller.rb +++ b/app/controllers/api/v2/scripts_controller.rb @@ -3,8 +3,8 @@ module Api module V2 class ScriptsController < BaseController - before_action :set_page_and_page_size - before_action :find_script + before_action :set_page_and_page_size, except: :referring_capacities + before_action :find_script, except: :referring_capacities def general_info head :not_found and return if @script.blank? || @contract.blank? @@ -49,6 +49,13 @@ def referring_cells end end + def referring_capacities + expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds + data = Contract.all.map { { _1.code_hash => _1.total_referring_cells_capacity.to_s } } + + render json: { data: } + end + private def get_script_content @@ -69,7 +76,7 @@ def get_script_content capacity_of_referring_cells: @contract.total_referring_cells_capacity, count_of_transactions: @contract.ckb_transactions_count, count_of_deployed_cells: @contract.deployed_cells_count, - count_of_referring_cells: @contract.referring_cells_count + count_of_referring_cells: @contract.referring_cells_count, } end diff --git a/config/routes/v2.rb b/config/routes/v2.rb index c48e88196..96dad4698 100644 --- a/config/routes/v2.rb +++ b/config/routes/v2.rb @@ -1,7 +1,7 @@ namespace :api do namespace :v2 do post "/das_accounts" => "das_accounts#query", as: :das_accounts - resources :ckb_transactions, only: [:index, :show] do + resources :ckb_transactions, only: %i[index show] do member do get :details get :display_inputs @@ -27,7 +27,7 @@ resources :holders, only: :index resources :transfers, only: :index resources :items do - resources :transfers, only: [:index, :show] + resources :transfers, only: %i[index show] end end namespace :cota do @@ -47,7 +47,7 @@ end end resources :items, only: :index - resources :transfers, only: [:index, :show] do + resources :transfers, only: %i[index show] do collection do get :download_csv end @@ -61,6 +61,7 @@ get :deployed_cells get :referring_cells get :general_info + get :referring_capacities end end diff --git a/test/controllers/api/v2/scripts_controller_test.rb b/test/controllers/api/v2/scripts_controller_test.rb index 81d6aa090..1423bd797 100644 --- a/test/controllers/api/v2/scripts_controller_test.rb +++ b/test/controllers/api/v2/scripts_controller_test.rb @@ -4,8 +4,8 @@ module Api module V2 class ScriptsControllerTest < ActionDispatch::IntegrationTest setup do - @code_hash = '0x00000000000000000000000000000000000000000000000000545950455f4944' - @hash_type = 'type' + @code_hash = "0x00000000000000000000000000000000000000000000000000545950455f4944" + @hash_type = "type" @block = create :block @contract = create :contract, code_hash: @code_hash, hash_type: @hash_type @script = create :script, contract_id: @contract.id @@ -35,6 +35,14 @@ class ScriptsControllerTest < ActionDispatch::IntegrationTest valid_get referring_cells_api_v2_scripts_url(code_hash: @code_hash, hash_type: @hash_type) assert_response :success end + + test "should get referring_capacities" do + create_list(:contract, 10) + referring_capacities = Contract.all.map { { _1.code_hash => _1.total_referring_cells_capacity.to_s } } + + valid_get referring_capacities_api_v2_scripts_url + assert_equal ({ "data" => referring_capacities }), json + end end end end diff --git a/test/factories/contract.rb b/test/factories/contract.rb index a4286f2ed..03c190886 100644 --- a/test/factories/contract.rb +++ b/test/factories/contract.rb @@ -9,6 +9,8 @@ description { "SECP256K1/multisig (Source Code) is a script which allows a group of users to sign a single transaction." } verified { false } deprecated { false } + total_referring_cells_capacity { SecureRandom.random_number(10**10) } + after(:create) do |contract, _eval| tx = create :ckb_transaction, :with_single_output co = tx.cell_outputs.first