Skip to content

Commit 46bec62

Browse files
committed
feat: add template related types
block_template, cellbase_template, transaction_template and uncle_template
1 parent 97a826c commit 46bec62

File tree

5 files changed

+228
-0
lines changed

5 files changed

+228
-0
lines changed

lib/ckb/types/block_template.rb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# frozen_string_literal: true
2+
3+
module CKB
4+
module Types
5+
class BlockTemplate
6+
attr_accessor :version, :difficulty, :current_time, :number, :epoch, :parent_hash, :cycles_limit,
7+
:bytes_limit, :uncles_count_limit, :uncles, :transactions, :proposals, :cellbase, :work_id, :dao
8+
9+
# @param version [String | Integer] integer or hex number
10+
# @param difficulty [String | Integer] integer or hex number
11+
# @param current_time [String | Integer] integer or hex number
12+
# @param number [String | Integer] integer or hex number
13+
# @param epoch [String | Integer] integer or hex number
14+
# @param parent_hash [String] 0x...
15+
# @param cycles_limit [String | Integer] integer or hex number
16+
# @param bytes_limit [String | Integer] integer or hex number
17+
# @param uncles_count_limit [String | Integer] integer or hex number
18+
# @param uncles [CKB::Types::UncleTemplate[]]
19+
# @param transactions [CKB::Types::TransactionTemplate[]]
20+
# @param proposals [String[]] 0x...
21+
# @param cellbase [CellbaseTemplate]
22+
# @param work_id [String | Integer] integer or hex number
23+
# @param dao [String] 0x...
24+
def initialize(
25+
version:,
26+
difficulty:,
27+
current_time:,
28+
number:,
29+
epoch:,
30+
parent_hash:,
31+
cycles_limit:,
32+
bytes_limit:,
33+
uncles_count_limit:,
34+
uncles:,
35+
transactions:,
36+
proposals:,
37+
cellbase:,
38+
work_id:,
39+
dao:
40+
)
41+
@version = Utils.to_int(version)
42+
@difficulty = Utils.to_int(difficulty)
43+
@current_time = Utils.to_int(current_time)
44+
@number = Utils.to_int(number)
45+
@epoch = Utils.to_int(epoch)
46+
@parent_hash = parent_hash
47+
@cycles_limit = Utils.to_int(cycles_limit)
48+
@bytes_limit = Utils.to_int(bytes_limit)
49+
@uncles_count_limit = Utils.to_int(uncles_count_limit)
50+
@uncles = uncles
51+
@transactions = transactions
52+
@proposals = proposals
53+
@cellbase = cellbase
54+
@work_id = Utils.to_int(work_id)
55+
@dao = dao
56+
end
57+
58+
def to_h
59+
{
60+
version: Utils.to_hex(version),
61+
difficulty: Utils.to_hex(difficulty),
62+
current_time: Utils.to_hex(current_time),
63+
number: Utils.to_hex(number),
64+
epoch: Utils.to_hex(epoch),
65+
parent_hash: parent_hash,
66+
cycles_limit: Utils.to_hex(cycles_limit),
67+
bytes_limit: Utils.to_hex(bytes_limit),
68+
uncles_count_limit: Utils.to_hex(uncles_count_limit),
69+
uncles: uncles.map(&:to_h),
70+
transactions: transactions.map(&:to_h),
71+
proposals: proposals,
72+
cellbase: cellbase.to_h,
73+
work_id: work_id,
74+
dao: dao
75+
}
76+
end
77+
78+
def self.from_h(hash)
79+
return if hash.nil?
80+
81+
new(
82+
version: hash[:version],
83+
difficulty: hash[:difficulty],
84+
current_time: hash[:current_time],
85+
number: hash[:number],
86+
epoch: hash[:epoch],
87+
parent_hash: hash[:parent_hash],
88+
cycles_limit: hash[:cycles_limit],
89+
bytes_limit: hash[:bytes_limit],
90+
uncles_count_limit: hash[:uncles_count_limit],
91+
uncles: hash[:uncles],
92+
transactions: hash[:transactions],
93+
proposals: hash[:proposals],
94+
cellbase: hash[:cellbase],
95+
work_id: hash[:work_id],
96+
dao: hash[:dao]
97+
)
98+
end
99+
end
100+
end
101+
end

lib/ckb/types/cellbase_template.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module CKB
4+
module Types
5+
class CellbaseTemplate
6+
attr_accessor :hash, :cycles, :data
7+
8+
# @param hash [String] 0x..
9+
# @param cycles [String | Integer] integer or hex number
10+
# @param data [CKB::Type::Transaction]
11+
def initialize(hash:, cycles:, data:)
12+
@hash = hash
13+
@cycles = Utils.to_int(cycles)
14+
@data = data
15+
end
16+
17+
def to_h
18+
{
19+
hash: hash,
20+
cycles: Utils.to_hex(cycles),
21+
data: data.to_h
22+
}
23+
end
24+
25+
def self.from_h(hash)
26+
return if hash.nil?
27+
28+
new(
29+
hash: hash[:hash],
30+
cycles: hash[:cycles],
31+
data: Transaction.from_h(hash[:data]),
32+
)
33+
end
34+
end
35+
end
36+
end
37+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
module CKB
4+
module Types
5+
class TransactionTemplate
6+
attr_accessor :hash, :required, :cycles, :depends, :data
7+
8+
# @param hash [String] 0x..
9+
# @param required [Boolean]
10+
# @param cycles [String | Integer] integer or hex number
11+
# @param depends [String[] | Integer[]] integer[] or hex number[]
12+
# @param data [CKB::Type::Transaction]
13+
def initialize(hash:, required:, cycles:, depends:, data:)
14+
@hash = hash
15+
@required = required
16+
@cycles = Utils.to_int(cycles)
17+
@depends = depends.map { |depend| Utils.to_int(depend) }
18+
@data = data
19+
end
20+
21+
def to_h
22+
{
23+
hash: hash,
24+
required: required,
25+
cycles: Utils.to_hex(cycles),
26+
depends: depends.map { |depend| Utils.to_hex(depend) },
27+
data: data.to_h
28+
}
29+
end
30+
31+
def self.from_h(hash)
32+
return if hash.nil?
33+
34+
new(
35+
hash: hash[:hash],
36+
required: hash[:required],
37+
cycles: hash[:cycles],
38+
depends: hash[:depends],
39+
data: Transaction.from_h(hash[:data]),
40+
)
41+
end
42+
end
43+
end
44+
end
45+

lib/ckb/types/types.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
require_relative "cell_dep"
3131
require_relative "cell_data"
3232
require_relative "cell_info"
33+
require_relative "uncle_template"
34+
require_relative "transaction_template"
35+
require_relative "cellbase_template"
36+
require_relative "block_template"
3337

3438
module CKB
3539
module Types

lib/ckb/types/uncle_template.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
module CKB
4+
module Types
5+
class UncleTemplate
6+
attr_accessor :hash, :required, :proposals, :header
7+
8+
# @param hash [String] 0x..
9+
# @param required [Boolean]
10+
# @param proposals [String[]] 0x..
11+
# @param header [CKB::Type::BlockHeader]
12+
def initialize(hash:, required:, proposals:, header:)
13+
@hash = hash
14+
@required = required
15+
@proposals = proposals
16+
@header = header
17+
end
18+
19+
def to_h
20+
{
21+
hash: hash,
22+
required: required,
23+
proposals: proposals,
24+
header: header.to_h
25+
}
26+
end
27+
28+
def self.from_h(hash)
29+
return if hash.nil?
30+
31+
new(
32+
hash: hash[:hash],
33+
required: hash[:required],
34+
proposals: hash[:proposals],
35+
header: BlockHeader.from_h(hash[:header])
36+
)
37+
end
38+
end
39+
end
40+
end
41+

0 commit comments

Comments
 (0)