Skip to content

Commit 6311c5b

Browse files
authored
Issue 845 (#2507)
* feat: add column to contracts Signed-off-by: Miles Zhang <mingchang555@hotmail.com> * feat: add scripts list api Signed-off-by: Miles Zhang <mingchang555@hotmail.com> * feat: use task update contract Signed-off-by: Miles Zhang <mingchang555@hotmail.com> * feat: add paginate to scripts list api Signed-off-by: Miles Zhang <mingchang555@hotmail.com> * feat: add deployed_block_timestamp to contracts Signed-off-by: Miles Zhang <mingchang555@hotmail.com> * feat: adjust script api Signed-off-by: Miles Zhang <mingchang555@hotmail.com> * test: fix test Signed-off-by: Miles Zhang <mingchang555@hotmail.com> * feat: add contract_cell_id to point to dep_group's cell Signed-off-by: Miles Zhang <mingchang555@hotmail.com> * feat: return contract name of script Signed-off-by: Miles Zhang <mingchang555@hotmail.com> * feat: returns verified column to scripts api * feat: cell_dependencies returns script info Signed-off-by: Miles Zhang <mingchang555@hotmail.com> * feat: modify association of cell_dependency Signed-off-by: Miles Zhang <mingchang555@hotmail.com> * feat: add is_primary for dep_group cell_dependency Signed-off-by: Miles Zhang <mingchang555@hotmail.com> --------- Signed-off-by: Miles Zhang <mingchang555@hotmail.com>
1 parent 858d328 commit 6311c5b

29 files changed

+674
-833
lines changed

.rubocop.yml

Lines changed: 215 additions & 616 deletions
Large diffs are not rendered by default.

Gemfile.lock

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,9 +495,7 @@ GEM
495495
zeitwerk (2.6.13)
496496

497497
PLATFORMS
498-
arm64-darwin-21
499498
ruby
500-
x86_64-linux
501499

502500
DEPENDENCIES
503501
active_interaction (~> 5.3)

app/controllers/api/v2/scripts_controller.rb

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,22 @@ module Api
44
module V2
55
class ScriptsController < BaseController
66
before_action :set_page_and_page_size
7-
before_action :set_contracts
7+
before_action :set_contracts, excepts: [:index]
8+
9+
def index
10+
scope = Contract.where(verified: true)
11+
if params[:script_type].present?
12+
script_types = params[:script_type].split(",").map(&:strip)
13+
if script_types.include?("lock")
14+
scope = scope.where(is_lock_script: true)
15+
end
16+
if script_types.include?("type")
17+
scope = scope.where(is_type_script: true)
18+
end
19+
end
20+
21+
@contracts = sort_scripts(scope).page(@page).per(@page_size)
22+
end
823

924
def general_info
1025
head :not_found and return if @contracts.blank?
@@ -72,27 +87,28 @@ def referring_cells
7287
private
7388

7489
def get_script_content
75-
sum_hash =
76-
@contracts.inject({
77-
capacity_of_deployed_cells: 0,
78-
capacity_of_referring_cells: 0,
79-
count_of_transactions: 0,
80-
count_of_deployed_cells: 0,
81-
count_of_referring_cells: 0,
82-
}) do |sum, contract|
83-
sum[:capacity_of_deployed_cells] += contract.total_deployed_cells_capacity
84-
sum[:capacity_of_referring_cells] += contract.total_referring_cells_capacity
85-
sum[:count_of_transactions] += contract.ckb_transactions_count
86-
sum[:count_of_deployed_cells] += 1
87-
sum[:count_of_referring_cells] += contract.referring_cells_count
88-
sum
89-
end
90-
{
91-
id: @contracts.first.type_hash,
92-
code_hash: params[:code_hash],
93-
hash_type: params[:hash_type],
94-
script_type: @contracts.first.is_lock_script ? "LockScript" : "TypeScript",
95-
}.merge(sum_hash)
90+
@contracts.map do |contract|
91+
{
92+
name: contract.name,
93+
type_hash: contract.type_hash,
94+
data_hash: contract.data_hash,
95+
hash_type: contract.hash_type,
96+
is_lock_script: contract.is_lock_script,
97+
is_type_script: contract.is_type_script,
98+
rfc: contract.rfc,
99+
website: contract.website,
100+
description: contract.description,
101+
deprecated: contract.deprecated,
102+
verified: contract.verified,
103+
source_url: contract.source_url,
104+
capacity_of_deployed_cells: contract.deployed_cell_output.capacity.to_s,
105+
capacity_of_referring_cells: contract.total_referring_cells_capacity.to_s,
106+
count_of_transactions: contract.ckb_transactions_count,
107+
count_of_referring_cells: contract.referring_cells_count,
108+
script_out_point: "#{contract.contract_cell&.tx_hash}-#{contract.contract_cell&.cell_index}",
109+
dep_type: contract.dep_type,
110+
}
111+
end
96112
end
97113

98114
def set_page_and_page_size
@@ -104,19 +120,22 @@ def set_contracts
104120
@contracts =
105121
case params[:hash_type]
106122
when "data", "data1", "data2"
107-
Contract.where(data_hash: params[:code_hash])
123+
Contract.includes(:deployed_cell_output, :contract_cell).where(data_hash: params[:code_hash])
108124
when "type"
109-
Contract.where(type_hash: params[:code_hash])
125+
Contract.includes(:deployed_cell_output, :contract_cell).where(type_hash: params[:code_hash])
110126
end
111127
end
112128

113-
def sort_referring_cells(records)
114-
sort, order = params.fetch(:sort, "block_timestamp.desc").split(".", 2)
129+
def sort_scripts(records)
130+
sort, order = params.fetch(:sort, "deployed_block_timestamp.asc").split(".", 2)
115131
sort =
116132
case sort
117-
when "created_time" then "block_timestamp"
118-
else "block_timestamp"
133+
when "capacity" then "total_referring_cells_capacity"
134+
when "timestamp" then "deployed_block_timestamp"
135+
else
136+
sort
119137
end
138+
120139
order = "asc" unless order&.match?(/^(asc|desc)$/i)
121140
records.order("#{sort} #{order}")
122141
end

app/controllers/api/v2/statistics_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def contract_resource_distributed
2424
json = contracts.map do |contract|
2525
{
2626
name: contract.name,
27-
code_hash: contract.code_hash,
28-
hash_type: contract.hash_type,
27+
code_hash: contract.code_hash_hash_type[0],
28+
hash_type: contract.code_hash_hash_type[1],
2929
tx_count: contract.ckb_transactions_count,
3030
h24_tx_count: contract.h24_ckb_transactions_count,
3131
ckb_amount: (contract.total_referring_cells_capacity / 10**8).truncate(8),

app/models/cell_dependency.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
class CellDependency < ApplicationRecord
22
belongs_to :ckb_transaction
33
belongs_to :cell_output, foreign_key: "contract_cell_id", class_name: "CellOutput"
4-
belongs_to :cell_deps_out_point, foreign_key: :contract_cell_id, primary_key: :contract_cell_id, optional: true
4+
has_many :cell_deps_out_points, foreign_key: :contract_cell_id, primary_key: :contract_cell_id, class_name: "CellDepsOutPoint"
5+
has_many :contracts, foreign_key: :contract_cell_id, primary_key: :contract_cell_id, class_name: "Contract"
56

67
enum :dep_type, %i[code dep_group]
78

89
def to_raw
10+
code_hash, hash_type =
11+
if contracts.primary.first
12+
contracts.primary.first.code_hash_hash_type
13+
else
14+
[nil, nil]
15+
end
916
{
1017
out_point: {
1118
tx_hash: cell_output.tx_hash,
1219
index: cell_output.cell_index,
1320
},
1421
dep_type:,
22+
script: {
23+
name: contracts.primary.first&.name,
24+
code_hash: code_hash,
25+
hash_type: hash_type,
26+
},
1527
}
1628
end
1729
end

app/models/contract.rb

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
class Contract < ApplicationRecord
22
has_many :cell_deps_out_points, foreign_key: :deployed_cell_output_id, primary_key: :deployed_cell_output_id
33
has_many :cell_dependencies, through: :cell_deps_out_points
4-
belongs_to :deployed_cell_output, class_name: "CellOutput", optional: true
4+
belongs_to :deployed_cell_output, class_name: "CellOutput"
5+
belongs_to :contract_cell, class_name: "CellOutput", optional: true
56

67
scope :active, -> { where("addresses_count != 0 and total_referring_cells_capacity != 0 and ckb_transactions_count != 0") }
8+
scope :verified, -> { where(verified: true) }
9+
scope :primary, -> { where(is_primary: true) }
10+
11+
enum dep_type: { code: 0, dep_group: 1 }
712

813
def self.referring_cells_query(contracts)
914
lock_script_hashes = []
@@ -25,28 +30,31 @@ def self.referring_cells_query(contracts)
2530
end
2631
scope
2732
end
33+
34+
def code_hash_hash_type
35+
if type_hash
36+
[type_hash, "type"]
37+
else
38+
[data_hash, hash_type]
39+
end
40+
end
2841
end
2942

3043
# == Schema Information
3144
#
3245
# Table name: contracts
3346
#
3447
# id :bigint not null, primary key
35-
# code_hash :binary
3648
# hash_type :string
3749
# deployed_args :string
38-
# role :string default("type_script")
3950
# name :string
40-
# symbol :string
4151
# description :string
4252
# verified :boolean default(FALSE)
4353
# created_at :datetime not null
4454
# updated_at :datetime not null
4555
# deprecated :boolean
4656
# ckb_transactions_count :decimal(30, ) default(0)
47-
# deployed_cells_count :decimal(30, ) default(0)
4857
# referring_cells_count :decimal(30, ) default(0)
49-
# total_deployed_cells_capacity :decimal(30, ) default(0)
5058
# total_referring_cells_capacity :decimal(30, ) default(0)
5159
# addresses_count :integer
5260
# h24_ckb_transactions_count :integer
@@ -55,15 +63,19 @@ def self.referring_cells_query(contracts)
5563
# deployed_cell_output_id :bigint
5664
# is_type_script :boolean
5765
# is_lock_script :boolean
66+
# rfc :string
67+
# source_url :string
68+
# dep_type :integer
69+
# website :string
70+
# deployed_block_timestamp :bigint
71+
# contract_cell_id :bigint
72+
# is_primary :boolean
5873
#
5974
# Indexes
6075
#
61-
# index_contracts_on_code_hash (code_hash)
6276
# index_contracts_on_deployed_cell_output_id (deployed_cell_output_id) UNIQUE
6377
# index_contracts_on_deprecated (deprecated)
6478
# index_contracts_on_hash_type (hash_type)
6579
# index_contracts_on_name (name)
66-
# index_contracts_on_role (role)
67-
# index_contracts_on_symbol (symbol)
6880
# index_contracts_on_verified (verified)
6981
#

app/models/lock_script.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ def calculate_bytesize
9999
bytesize
100100
end
101101

102+
def verified_script
103+
if hash_type == "type"
104+
Contract.where(verified: true, type_hash: code_hash)&.first
105+
else
106+
Contract.where(verified: true, data_hash: code_hash)&.first
107+
end
108+
end
109+
102110
private
103111

104112
def set_since_epoch_number_and_index(since_value)
@@ -115,8 +123,8 @@ def set_since_epoch_number_and_index(since_value)
115123

116124
def lock_info_status(since_value, tip_epoch)
117125
after_lock_epoch_number = tip_epoch.number > since_value.number
118-
at_lock_epoch_number_but_exceeded_index = (tip_epoch.number == since_value.number &&
119-
tip_epoch.index * since_value.length > since_value.index * tip_epoch.length)
126+
at_lock_epoch_number_but_exceeded_index = tip_epoch.number == since_value.number &&
127+
tip_epoch.index * since_value.length > since_value.index * tip_epoch.length
120128

121129
after_lock_epoch_number || at_lock_epoch_number_but_exceeded_index ? "unlocked" : "locked"
122130
end

app/models/type_script.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ def calculate_bytesize
6464

6565
bytesize
6666
end
67+
68+
def verified_script
69+
if hash_type == "type"
70+
Contract.where(verified: true, type_hash: code_hash)&.first
71+
else
72+
Contract.where(verified: true, data_hash: code_hash)&.first
73+
end
74+
end
6775
end
6876

6977
# == Schema Information

app/serializers/ckb_transaction_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CkbTransactionSerializer
1111
end
1212

1313
attribute :cell_deps do |o|
14-
o.cell_dependencies.order("id asc").includes(:cell_output).to_a.map(&:to_raw)
14+
o.cell_dependencies.order("id asc").includes(:cell_output, :contracts).to_a.map(&:to_raw)
1515
end
1616

1717
attribute :header_deps do |o|

app/serializers/lock_script_serializer.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ class LockScriptSerializer
22
include FastJsonapi::ObjectSerializer
33

44
attributes :args, :code_hash, :hash_type
5+
6+
attribute :verified_script_name do |object|
7+
object.verified_script&.name
8+
end
59
end

0 commit comments

Comments
 (0)