Skip to content

Commit

Permalink
fix: capacity in RPC interface changed to string
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Apr 10, 2019
1 parent d4d64ba commit d8d3d05
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions lib/ckb/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ 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
results
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)
Expand All @@ -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
}
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/ckb/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit d8d3d05

Please sign in to comment.