Skip to content

Commit

Permalink
feat: add get_capacity_by_lock_hash RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Jan 2, 2020
1 parent 65c101f commit 9de8da2
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ def index_lock_hash(lock_hash, index_from: 0)
Types::LockHashIndexState.from_h(state)
end

# @param lock_hash [String]
#
# @return [CKB::Types::LockHashCapacity]
def get_capacity_by_lock_hash(lock_hash)
result = rpc.get_capacity_by_lock_hash(lock_hash)
Types::LockHashCapacity.from_h(result) unless result.nil?
end

# @param block_hash [String] 0x...
#
# @return [CKB::Types::BlockHeader]
Expand Down
5 changes: 5 additions & 0 deletions lib/ckb/rpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ def index_lock_hash(lock_hash, index_from: 0)
rpc_request("index_lock_hash", params: [lock_hash, Utils.to_hex(index_from)])
end

# @param lock_hash [String]
def get_capacity_by_lock_hash(lock_hash)
rpc_request('get_capacity_by_lock_hash', params: [lock_hash])
end

def get_header(block_hash)
rpc_request("get_header", params: [block_hash])
end
Expand Down
36 changes: 36 additions & 0 deletions lib/ckb/types/lock_hash_capacity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

module CKB
module Types
class LockHashCapacity
attr_accessor :capacity, :cells_count, :block_number

# @param capacity [String | Integer] integer or hex number
# @param cells_count [String | Integer] integer or hex number
# @param block_number [String | Integer] integer or hex number
def initialize(capacity:, cells_count:, block_number:)
@capacity = Utils.to_int(capacity)
@cells_count = Utils.to_int(cells_count)
@block_number = Utils.to_int(block_number)
end

def to_h
{
capacity: Utils.to_hex(@capacity),
cells_count: Utils.to_hex(@cells_count),
block_number: Utils.to_hex(@block_number)
}
end

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

new(
capacity: hash[:capacity],
cells_count: hash[:cells_count],
block_number: hash[:block_number]
)
end
end
end
end
1 change: 1 addition & 0 deletions lib/ckb/types/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
require_relative "block_template"
require_relative "estimate_result"
require_relative "witness"
require_relative "lock_hash_capacity"

module CKB
module Types
Expand Down
6 changes: 6 additions & 0 deletions spec/ckb/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@
result = api.get_transactions_by_lock_hash(lock_hash, 0, 10)
expect(result).not_to be nil
end

it "get_capacity_by_lock_hash" do
api.index_lock_hash(lock_hash)
result = api.get_capacity_by_lock_hash(lock_hash)
expect(result).not_to be nil
end
end

it "get block header" do
Expand Down
6 changes: 6 additions & 0 deletions spec/ckb/rpc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@
result = rpc.get_transactions_by_lock_hash(lock_hash, 0, 10)
expect(result).not_to be nil
end

it "get_capacity_by_lock_hash" do
rpc.index_lock_hash(lock_hash)
result = rpc.get_capacity_by_lock_hash(lock_hash)
expect(result).not_to be nil
end
end

it "get block header" do
Expand Down
24 changes: 24 additions & 0 deletions spec/ckb/types/lock_hash_capacity_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
RSpec.describe CKB::Types::LockHashCapacity do
let(:lock_hash_capacity_h) do
{
"block_number": "0x400",
"capacity": "0xb00fb84df292",
"cells_count": "0x3f5"
}
end

let(:lock_hash_capacity) { CKB::Types::LockHashCapacity.from_h(lock_hash_capacity_h) }

it "from_h" do
expect(lock_hash_capacity).to be_a(CKB::Types::LockHashCapacity)
expect(lock_hash_capacity.capacity).to eq lock_hash_capacity_h[:capacity].hex
expect(lock_hash_capacity.cells_count).to eq lock_hash_capacity_h[:cells_count].hex
expect(lock_hash_capacity.block_number).to eq lock_hash_capacity_h[:block_number].hex
end

it "to_h" do
expect(
lock_hash_capacity.to_h
).to eq lock_hash_capacity_h
end
end

0 comments on commit 9de8da2

Please sign in to comment.