-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1430 from nervosnetwork/develop
Deploy to testnet
- Loading branch information
Showing
11 changed files
with
181 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,73 @@ | ||
require "jbuilder" | ||
module Api::V2 | ||
class ScriptsController < BaseController | ||
before_action :set_page_and_page_size | ||
before_action :find_script | ||
module Api | ||
module V2 | ||
class ScriptsController < BaseController | ||
before_action :set_page_and_page_size | ||
before_action :find_script | ||
|
||
def general_info | ||
head :not_found and return if @script.blank? | ||
def general_info | ||
head :not_found and return if @script.blank? | ||
|
||
render json: { | ||
data: get_script_content(@script) | ||
} | ||
end | ||
render json: { | ||
data: get_script_content(@script) | ||
} | ||
end | ||
|
||
def ckb_transactions | ||
head :not_found and return if @script.blank? | ||
def ckb_transactions | ||
head :not_found and return if @script.blank? | ||
|
||
scope = CellDependency.where(contract_id: @contract.id).order(ckb_transaction_id: :desc) | ||
tx_ids = scope.page(params[:page]).pluck(:ckb_transaction_id) | ||
@ckb_transactions = CkbTransaction.find(tx_ids) | ||
@total = scope.count | ||
end | ||
scope = CellDependency.where(contract_id: @contract.id).order(ckb_transaction_id: :desc) | ||
tx_ids = scope.page(params[:page]).pluck(:ckb_transaction_id) | ||
@ckb_transactions = CkbTransaction.find(tx_ids) | ||
@total = scope.count | ||
end | ||
|
||
def deployed_cells | ||
head :not_found and return if @script.blank? || @script.contract.blank? | ||
def deployed_cells | ||
head :not_found and return if @script.blank? || @script.contract.blank? | ||
|
||
@deployed_cells = @contract.deployed_cells.page(@page).per(@page_size).fast_page | ||
end | ||
@deployed_cells = @contract.deployed_cells.page(@page).per(@page_size).fast_page | ||
end | ||
|
||
private | ||
def referring_cells | ||
head :not_found and return if @script.blank? | ||
|
||
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 | ||
{ | ||
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 | ||
} | ||
end | ||
@referring_cells = @contract.referring_cells.page(@page).per(@page_size).fast_page | ||
end | ||
|
||
def set_page_and_page_size | ||
@page = params[:page] || 1 | ||
@page_size = params[:page_size] || 10 | ||
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 | ||
{ | ||
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 | ||
} | ||
end | ||
|
||
def find_script | ||
@script = TypeScript.find_by(code_hash: params[:code_hash], | ||
hash_type: params[:hash_type]) | ||
if @script.blank? | ||
@script = LockScript.find_by(code_hash: params[:code_hash], | ||
def set_page_and_page_size | ||
@page = params[:page] || 1 | ||
@page_size = params[:page_size] || 10 | ||
end | ||
|
||
def find_script | ||
@script = TypeScript.find_by(code_hash: params[:code_hash], | ||
hash_type: params[:hash_type]) | ||
if @script.blank? | ||
@script = LockScript.find_by(code_hash: params[:code_hash], | ||
hash_type: params[:hash_type]) | ||
end | ||
@contract = Contract.find_by(code_hash: params[:code_hash], | ||
hash_type: params[:hash_type]) | ||
end | ||
@contract = Contract.find_by(code_hash: params[:code_hash], | ||
hash_type: params[:hash_type]) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
json.data do | ||
json.referring_cells @referring_cells do |referring_cell| | ||
cell_output = referring_cell.cell_output | ||
json.id cell_output.id | ||
json.capacity cell_output.capacity | ||
json.ckb_transaction_id cell_output.ckb_transaction_id | ||
json.created_at cell_output.created_at | ||
json.updated_at cell_output.updated_at | ||
json.status cell_output.status | ||
json.address_id cell_output.address_id | ||
json.block_id cell_output.block_id | ||
json.tx_hash cell_output.tx_hash | ||
json.cell_index cell_output.cell_index | ||
json.consumed_by_id cell_output.consumed_by_id | ||
json.cell_type cell_output.cell_type | ||
json.data_size cell_output.data_size | ||
json.occupied_capacity cell_output.occupied_capacity | ||
json.block_timestamp cell_output.block_timestamp | ||
json.consumed_block_timestamp cell_output.consumed_block_timestamp | ||
json.type_hash cell_output.type_hash | ||
json.udt_amount cell_output.udt_amount | ||
json.dao cell_output.dao | ||
json.lock_script_id cell_output.lock_script_id | ||
json.type_script_id cell_output.type_script_id | ||
end | ||
json.meta do | ||
json.total @referring_cells.count | ||
json.page_size @page_size.to_i | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddIndexesToReferringCells < ActiveRecord::Migration[7.0] | ||
def change | ||
add_index :referring_cells, [:contract_id, :cell_output_id], unique: true | ||
add_index :referring_cells, :cell_output_id, unique: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace :migration do | ||
desc "Usage: RAILS_ENV=production bundle exec rake migration:generate_referring_cells" | ||
task generate_referring_cells: :environment do | ||
live_cells = CellOutput.live.left_joins(:referring_cell).where(referring_cells: { id: nil }) | ||
progress_bar = ProgressBar.create({ total: live_cells.count, format: "%e %B %p%% %c/%C" }) | ||
|
||
live_cells.find_in_batches do |outputs| | ||
outputs.each do |output| | ||
progress_bar.increment | ||
|
||
contract = output.lock_script.contract | ||
contract ||= output.type_script&.contract | ||
|
||
next unless contract | ||
|
||
ReferringCell.create_or_find_by( | ||
cell_output_id: output.id, | ||
ckb_transaction_id: output.ckb_transaction_id, | ||
contract_id: contract.id | ||
) | ||
end | ||
end | ||
|
||
puts "done" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters