From 2ab7217d66c860ba171047d304c2ccea12942bf7 Mon Sep 17 00:00:00 2001 From: classicalliu Date: Mon, 23 Sep 2019 10:59:33 +0800 Subject: [PATCH] feat: add CellOutputWithOutPoint type and add block_hash --- lib/ckb/api.rb | 4 +- lib/ckb/types/cell_output_with_out_point.rb | 45 +++++++++++++++++++ lib/ckb/types/output.rb | 14 ++---- lib/ckb/types/types.rb | 1 + .../types/cell_output_with_out_point_spec.rb | 33 ++++++++++++++ 5 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 lib/ckb/types/cell_output_with_out_point.rb create mode 100644 spec/ckb/types/cell_output_with_out_point_spec.rb diff --git a/lib/ckb/api.rb b/lib/ckb/api.rb index 84e19853..f2b0b2e2 100644 --- a/lib/ckb/api.rb +++ b/lib/ckb/api.rb @@ -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] diff --git a/lib/ckb/types/cell_output_with_out_point.rb b/lib/ckb/types/cell_output_with_out_point.rb new file mode 100644 index 00000000..ee5d0608 --- /dev/null +++ b/lib/ckb/types/cell_output_with_out_point.rb @@ -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 diff --git a/lib/ckb/types/output.rb b/lib/ckb/types/output.rb index 6fa1ed2e..82083bd4 100644 --- a/lib/ckb/types/output.rb +++ b/lib/ckb/types/output.rb @@ -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) @@ -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 diff --git a/lib/ckb/types/types.rb b/lib/ckb/types/types.rb index ffad4a65..89595537 100644 --- a/lib/ckb/types/types.rb +++ b/lib/ckb/types/types.rb @@ -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" diff --git a/spec/ckb/types/cell_output_with_out_point_spec.rb b/spec/ckb/types/cell_output_with_out_point_spec.rb new file mode 100644 index 00000000..1ab6806d --- /dev/null +++ b/spec/ckb/types/cell_output_with_out_point_spec.rb @@ -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