From d8d3d058fce8f1ed1be5845559bba93de380bbd6 Mon Sep 17 00:00:00 2001 From: classicalliu Date: Wed, 10 Apr 2019 23:44:50 +0800 Subject: [PATCH] fix: capacity in RPC interface changed to string --- lib/ckb/api.rb | 2 +- lib/ckb/wallet.rb | 15 ++++++++------- spec/ckb/api_spec.rb | 8 ++++---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/ckb/api.rb b/lib/ckb/api.rb index 2c303ff4..932bc9ab 100644 --- a/lib/ckb/api.rb +++ b/lib/ckb/api.rb @@ -69,7 +69,7 @@ def genesis_block end def genesis_block_hash - @genesis_block_hash ||= get_block_hash(0) + @genesis_block_hash ||= get_block_hash('0') end def get_block_hash(block_number) diff --git a/lib/ckb/wallet.rb b/lib/ckb/wallet.rb index ba7fdf35..7f6e60a7 100644 --- a/lib/ckb/wallet.rb +++ b/lib/ckb/wallet.rb @@ -30,12 +30,12 @@ def self.from_hex(api, privkey_hex) end def get_unspent_cells - to = api.get_tip_block_number + to = api.get_tip_block_number.to_i results = [] current_from = 1 while current_from <= to current_to = [current_from + 100, to].min - cells = api.get_cells_by_lock_hash(lock_hash, current_from, current_to) + cells = api.get_cells_by_lock_hash(lock_hash, current_from.to_s, current_to.to_s) results.concat(cells) current_from = current_to + 1 end @@ -43,7 +43,7 @@ def get_unspent_cells end def get_balance - get_unspent_cells.map { |cell| cell[:capacity] }.reduce(0, &:+) + get_unspent_cells.map { |cell| cell[:capacity].to_i }.reduce(0, &:+) end def generate_tx(target_address, capacity) @@ -52,14 +52,14 @@ def generate_tx(target_address, capacity) outputs = [ { - capacity: capacity, + capacity: capacity.to_s, data: "", lock: generate_lock(api.parse_address(target_address)) } ] if input_capacities > capacity outputs << { - capacity: input_capacities - capacity, + capacity: (input_capacities - capacity).to_s, data: "", lock: lock } @@ -68,7 +68,8 @@ def generate_tx(target_address, capacity) version: 0, deps: [api.system_script_out_point], inputs: CKB::Utils.sign_sighash_all_inputs(i.inputs, outputs, privkey), - outputs: outputs + outputs: outputs, + witnesses: [] } end @@ -117,7 +118,7 @@ def gather_inputs(capacity, min_capacity) args: [pubkey] } inputs << input - input_capacities += cell[:capacity] + input_capacities += cell[:capacity].to_i break if input_capacities >= capacity && (input_capacities - capacity) >= min_capacity end diff --git a/spec/ckb/api_spec.rb b/spec/ckb/api_spec.rb index bfe07385..f94ffafc 100644 --- a/spec/ckb/api_spec.rb +++ b/spec/ckb/api_spec.rb @@ -26,16 +26,16 @@ it "get tip header" do result = api.get_tip_header expect(result).not_to be nil - expect(result[:number] > 0).to be true + expect(result[:number].to_i > 0).to be true end it "get tip block number" do result = api.get_tip_block_number - expect(result > 0).to be true + expect(result.to_i > 0).to be true end it "get cells by lock hash" do - result = api.get_cells_by_lock_hash(lock_hash, 0, 100) + result = api.get_cells_by_lock_hash(lock_hash, '0', '100') expect(result).not_to be nil end @@ -47,7 +47,7 @@ end it "get live cell" do - cells = api.get_cells_by_lock_hash(lock_hash, 0, 100) + cells = api.get_cells_by_lock_hash(lock_hash, '0', '100') result = api.get_live_cell(cells[0][:out_point]) expect(result).not_to be nil end