diff --git a/app/controllers/api/v2/scripts_controller.rb b/app/controllers/api/v2/scripts_controller.rb index df57f2c86..e43a872d6 100644 --- a/app/controllers/api/v2/scripts_controller.rb +++ b/app/controllers/api/v2/scripts_controller.rb @@ -1,4 +1,5 @@ require "jbuilder" + module Api module V2 class ScriptsController < BaseController @@ -8,16 +9,16 @@ class ScriptsController < BaseController def general_info head :not_found and return if @script.blank? - render json: { - data: get_script_content(@script) - } + expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds + render json: { data: get_script_content } end def ckb_transactions head :not_found and return if @script.blank? + expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds scope = CellDependency.where(contract_id: @contract.id).order(ckb_transaction_id: :desc) - tx_ids = scope.page(params[:page]).pluck(:ckb_transaction_id) + tx_ids = scope.page(@page).per(@page_size).pluck(:ckb_transaction_id) @ckb_transactions = CkbTransaction.find(tx_ids) @total = scope.count end @@ -25,31 +26,34 @@ def ckb_transactions def deployed_cells head :not_found and return if @script.blank? || @script.contract.blank? + expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds @deployed_cells = @contract.deployed_cells.page(@page).per(@page_size).fast_page end def referring_cells head :not_found and return if @script.blank? + expires_in 15.seconds, public: true, must_revalidate: true, stale_while_revalidate: 5.seconds @referring_cells = @contract.referring_cells.page(@page).per(@page_size).fast_page end private - def get_script_content(script) - column_name = script.instance_of?(TypeScript) ? "type_script_id" : "lock_script_id" - @my_referring_cells = CellOutput.live.where(column_name => script.id) - @deployed_cells = @contract&.deployed_cell_outputs&.live + def get_script_content + referring_cells = @contract&.referring_cell_outputs + deployed_cells = @contract&.deployed_cell_outputs&.live + transactions = @contract&.cell_dependencies + { - id: script.id, - code_hash: script.code_hash, - hash_type: script.hash_type, - script_type: script.class.to_s, - capacity_of_deployed_cells: @deployed_cells&.sum(:capacity), - capacity_of_referring_cells: @my_referring_cells.sum(:capacity), - count_of_transactions: @contract&.ckb_transactions&.count.to_i, - count_of_deployed_cells: @deployed_cells&.count.to_i, - count_of_referring_cells: @my_referring_cells.size.to_i + id: @script.id, + code_hash: @script.code_hash, + hash_type: @script.hash_type, + script_type: @script.class.to_s, + capacity_of_deployed_cells: deployed_cells&.sum(:capacity), + capacity_of_referring_cells: referring_cells&.sum(:capacity), + count_of_transactions: transactions&.count.to_i, + count_of_deployed_cells: deployed_cells&.count.to_i, + count_of_referring_cells: referring_cells&.count.to_i } end diff --git a/app/models/contract.rb b/app/models/contract.rb index e5da24f83..8065e8376 100644 --- a/app/models/contract.rb +++ b/app/models/contract.rb @@ -3,6 +3,7 @@ class Contract < ApplicationRecord has_many :deployed_cells has_many :deployed_cell_outputs, through: :deployed_cells, source: :cell_output has_many :referring_cells + has_many :referring_cell_outputs, through: :deployed_cells, source: :cell_output has_many :cell_dependencies has_many :ckb_transactions, through: :cell_dependencies diff --git a/app/services/das_indexer_service.rb b/app/services/das_indexer_service.rb index 043be8cee..2bee240c5 100644 --- a/app/services/das_indexer_service.rb +++ b/app/services/das_indexer_service.rb @@ -19,15 +19,15 @@ def reverse_record(ckb_address) "key": "0x#{manager_addr}" } }) - data = JSON.parse res.to_s - case data["errno"] + data = JSON.parse(res.to_s) + + case data["err_no"] when 0 + return data["data"]["account"] when 20007 return "" else - raise data["errmsg"] + raise data["err_msg"] || "Unknown Error" end - - data["data"]["account"] end end diff --git a/app/views/api/v2/scripts/ckb_transactions.json.jbuilder b/app/views/api/v2/scripts/ckb_transactions.json.jbuilder index 9c92de90d..3ee41a87d 100644 --- a/app/views/api/v2/scripts/ckb_transactions.json.jbuilder +++ b/app/views/api/v2/scripts/ckb_transactions.json.jbuilder @@ -23,7 +23,7 @@ json.data do json.display_outputs tx.display_outputs end json.meta do - json.total @total || @contract.ckb_transactions.count + json.total @total json.page_size @page_size.to_i end end diff --git a/app/views/api/v2/scripts/deployed_cells.json.jbuilder b/app/views/api/v2/scripts/deployed_cells.json.jbuilder index 293b1497d..7f84e408f 100644 --- a/app/views/api/v2/scripts/deployed_cells.json.jbuilder +++ b/app/views/api/v2/scripts/deployed_cells.json.jbuilder @@ -24,7 +24,7 @@ json.data do json.type_script_id cell_output.type_script_id end json.meta do - json.total @deployed_cells.count + json.total @deployed_cells.total_count json.page_size @page_size.to_i end end diff --git a/lib/tasks/migration/generate_referring_cells.rake b/lib/tasks/migration/generate_referring_cells.rake index 7033e1e1c..db1a5f4c5 100644 --- a/lib/tasks/migration/generate_referring_cells.rake +++ b/lib/tasks/migration/generate_referring_cells.rake @@ -8,7 +8,7 @@ namespace :migration do outputs.each do |output| progress_bar.increment - contract = output.lock_script.contract + contract = output.lock_script&.contract contract ||= output.type_script&.contract next unless contract