Skip to content

Commit 92ca150

Browse files
committed
feat: add ckb-indexer types and get_cells api
1 parent a935a7c commit 92ca150

File tree

7 files changed

+132
-0
lines changed

7 files changed

+132
-0
lines changed

lib/ckb.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
require "ckb/mock_transaction_dumper"
2020
require "ckb/system_code_hash"
2121
require "ckb/script_hash_type"
22+
require "ckb/indexer/types/types"
23+
require "ckb/indexer/api"
2224

2325
module CKB
2426
class Error < StandardError; end

lib/ckb/indexer/api.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module CKB
2+
module Indexer
3+
class API
4+
attr_reader :rpc
5+
DEFAULT_LIMIT = 1000
6+
7+
def initialize(indexer_host, timeout_config = {})
8+
@rpc = CKB::RPC.new(host: indexer_host, timeout_config: timeout_config)
9+
end
10+
11+
def get_cells(search_key:, order: "asc", limit: DEFAULT_LIMIT, after_cursor: nil)
12+
result = rpc.get_cells(search_key.to_h, order, Utils.to_hex(limit), after_cursor)
13+
CKB::Indexer::Types::LiveCells.from_h(result)
14+
end
15+
end
16+
end
17+
end

lib/ckb/indexer/types/live_cell.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module CKB
2+
module Indexer
3+
module Types
4+
class LiveCell
5+
attr_reader :block_number, :out_point, :output, :output_data, :tx_index
6+
7+
# @param block_number [String | Integer] integer or hex number
8+
# @param out_point [CKB::Types::OutPoint]
9+
# @param output [CKB::Types::Output]
10+
# @param output_data [string]
11+
# @param tx_index [String | Integer] integer or hex number
12+
def initialize(block_number:, out_point:, output:, output_data:, tx_index:)
13+
@block_number = Utils.to_int(block_number)
14+
@out_point = out_point
15+
@output = output
16+
@output_data = output_data
17+
@tx_index = Utils.to_int(tx_index)
18+
end
19+
20+
def self.from_h(hash)
21+
return if hash.nil?
22+
23+
new(
24+
block_number: hash[:block_number],
25+
out_point: CKB::Types::OutPoint.from_h(hash[:out_point]),
26+
output: CKB::Types::Output.from_h(hash[:output]),
27+
output_data: hash[:output_data],
28+
tx_index: hash[:tx_index]
29+
)
30+
end
31+
end
32+
end
33+
end
34+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module CKB
2+
module Indexer
3+
module Types
4+
class LiveCells
5+
attr_reader :last_cursor, :objects
6+
# @param last_cursor [String]
7+
# @param objects [CKB::Types::LiveCell[]]
8+
def initialize(last_cursor:, objects:)
9+
@last_cursor = last_cursor
10+
@objects = objects
11+
end
12+
13+
def self.from_h(hash)
14+
return if hash.nil?
15+
16+
new(
17+
last_cursor: hash[:last_cursor],
18+
objects: hash[:objects].map { |object| CKB::Indexer::Types::LiveCell.from_h(object) }
19+
)
20+
end
21+
end
22+
end
23+
end
24+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module CKB
2+
module Indexer
3+
module Types
4+
class SearchKey
5+
attr_accessor :script, :script_type, :args_len
6+
7+
def initialize(script, script_type, args_len = nil)
8+
raise ArgumentError, "script type must be CKB::Types::Script" unless script.is_a?(CKB::Types::Script)
9+
raise ArgumentError, "invalid script_type: #{script_type}" unless %w(lock type).include?(script_type)
10+
11+
@script = script
12+
@script_type = script_type
13+
@args_len = args_len
14+
end
15+
16+
def to_h
17+
hash = {
18+
script: script.to_h,
19+
script_type: script_type
20+
}
21+
hash[args_len] = Utils.to_hex(args_len) if args_len
22+
23+
hash
24+
end
25+
end
26+
end
27+
end
28+
end

lib/ckb/indexer/types/types.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "search_key"
4+
require_relative "live_cell"
5+
require_relative "live_cells"
6+
7+
module CKB
8+
module Indexer
9+
module Types
10+
end
11+
end
12+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe CKB::Indexer::Types::SearchKey do
4+
it "should raise error when script type is not CKB::Types::Script" do
5+
expect {
6+
CKB::Indexer::Types::SearchKey.new(123, "type")
7+
}.to raise_error(ArgumentError, "script type must be CKB::Types::Script")
8+
end
9+
10+
it "should raise error when script_type is invalid" do
11+
expect {
12+
CKB::Indexer::Types::SearchKey.new(CKB::Types::Script.new(code_hash: CKB::SystemCodeHash::SECP256K1_BLAKE160_SIGHASH_ALL_TYPE_HASH, args: "0x", hash_type: "type"), "t")
13+
}.to raise_error(ArgumentError, "invalid script_type: t")
14+
end
15+
end

0 commit comments

Comments
 (0)