Skip to content

Commit

Permalink
feat: add CellOutputWithOutPoint type and add block_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Sep 23, 2019
1 parent d3a4488 commit 2ab7217
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ def get_tip_block_number
# @param from [String | Integer]
# @param to [String | Integer]
#
# @return [CKB::Types::Output[]]
# @return [CKB::Types::CellOutputWithOutPoint[]]
def get_cells_by_lock_hash(hash, from, to)
outputs = rpc.get_cells_by_lock_hash(hash, from, to)
outputs.map { |output| Types::Output.from_h(output) }
outputs.map { |output| Types::CellOutputWithOutPoint.from_h(output) }
end

# @param tx_hash [String]
Expand Down
45 changes: 45 additions & 0 deletions lib/ckb/types/cell_output_with_out_point.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module CKB
module Types
class CellOutputWithOutPoint
attr_accessor :lock, :out_point, :block_hash
attr_reader :capacity

# @param capacity [String | Integer] integer or hex number
# @param lock [CKB::Types::Script]
# @param out_point [CKB::Types::OutPoint]
# @param block_hash [String]
def initialize(capacity:, lock:, out_point:, block_hash:)
@capacity = Utils.to_int(capacity)
@lock = lock
@out_point = out_point
@block_hash = block_hash
end

def capacity=(value)
@capacity = Utils.to_int(value)
end

def to_h
{
capacity: Utils.to_hex(@capacity),
lock: @lock.to_h,
block_hash: @block_hash,
out_point: @out_point.to_h
}
end

def self.from_h(hash)
return if hash.nil?

new(
capacity: hash[:capacity],
lock: Script.from_h(hash[:lock]),
out_point: OutPoint.from_h(hash[:out_point]),
block_hash: hash[:block_hash]
)
end
end
end
end
14 changes: 4 additions & 10 deletions lib/ckb/types/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
module CKB
module Types
class Output
attr_accessor :lock, :type, :out_point
attr_accessor :lock, :type
attr_reader :capacity

# @param capacity [String | Integer] integer or hex number
# @param lock [CKB::Types::Script]
# @param type [CKB::Types::Script | nil]
# @param out_point [CKB::Types::OutPoint | nil]
def initialize(capacity:, lock:, type: nil, out_point: nil)
def initialize(capacity:, lock:, type: nil)
@capacity = Utils.to_int(capacity)
@lock = lock
@type = type
@out_point = out_point
end

def capacity=(value)
Expand All @@ -34,25 +32,21 @@ def calculate_min_capacity(data)
end

def to_h
hash = {
{
capacity: Utils.to_hex(@capacity),
lock: @lock.to_h,
type: @type&.to_h
}
hash[:out_point] = @out_point if @out_point
hash
end

def self.from_h(hash)
return if hash.nil?

type = Script.from_h(hash[:type]) if hash[:type]
out_point = OutPoint.from_h(hash[:out_point]) if hash[:out_point]
new(
capacity: hash[:capacity],
lock: Script.from_h(hash[:lock]),
type: type,
out_point: out_point
type: type
)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/ckb/types/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative "input"
require_relative "out_point"
require_relative "output"
require_relative "cell_output_with_out_point"
require_relative "cell_with_status"
require_relative "script"
require_relative "transaction_with_status"
Expand Down
33 changes: 33 additions & 0 deletions spec/ckb/types/cell_output_with_out_point_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
RSpec.describe CKB::Types::CellOutputWithOutPoint do
let(:cell_output_with_out_point_h) do
{
"block_hash": "0x03935a4b5e3c03a9c1deb93a39183a9a116c16cff3dc9ab129e847487da0e2b8",
"capacity": "0x1d1a94a200",
"lock": {
"args": [],
"code_hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5",
"hash_type": "data"
},
"out_point": {
"index": "0x0",
"tx_hash": "0x5ba156200c6310bf140fbbd3bfe7e8f03d4d5f82b612c1a8ec2501826eaabc17"
}
}
end

let(:cell_output_with_out_point) { CKB::Types::CellOutputWithOutPoint.from_h(cell_output_with_out_point_h) }

it "from_h" do
expect(cell_output_with_out_point).to be_a(CKB::Types::CellOutputWithOutPoint)
expect(cell_output_with_out_point.lock).to be_a(CKB::Types::Script)
expect(cell_output_with_out_point.out_point).to be_a(CKB::Types::OutPoint)
expect(cell_output_with_out_point.capacity).to eq cell_output_with_out_point_h[:capacity].hex
expect(cell_output_with_out_point.block_hash).to eq cell_output_with_out_point_h[:block_hash]
end

it "to_h" do
expect(
cell_output_with_out_point.to_h
).to eq cell_output_with_out_point_h
end
end

0 comments on commit 2ab7217

Please sign in to comment.