Skip to content

Commit

Permalink
feat: add contract referring capacities
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitz committed Jan 30, 2024
1 parent 15df613 commit ab676eb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
13 changes: 10 additions & 3 deletions app/controllers/api/v2/scripts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
7 changes: 4 additions & 3 deletions config/routes/v2.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -61,6 +61,7 @@
get :deployed_cells
get :referring_cells
get :general_info
get :referring_capacities
end
end

Expand Down
12 changes: 10 additions & 2 deletions test/controllers/api/v2/scripts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions test/factories/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ab676eb

Please sign in to comment.