Skip to content

Commit

Permalink
feat: add get_epoch_by_number RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed May 8, 2019
1 parent 2a12fe0 commit 51eb43f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ def get_current_epoch
Types::Epoch.from_h(rpc.get_current_epoch)
end

# @param number [Integer | String]
#
# @return [CKB::Types::Epoch]
def get_epoch_by_number(number)
Types::Epoch.from_h(
rpc.get_epoch_by_number(number)
)
end

def inspect
"\#<API@#{uri}>"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/ckb/rpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def get_current_epoch
rpc_request("get_current_epoch")
end

def get_epoch_by_number(number)
rpc_request("get_epoch_by_number", params: [number.to_s])
end

def inspect
"\#<RPC@#{uri}>"
end
Expand Down
1 change: 1 addition & 0 deletions lib/ckb/types/epoch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def to_h
end

def self.from_h(hash)
return if hash.nil?
new(
block_reward: hash[:block_reward],
difficulty: hash[:difficulty],
Expand Down
7 changes: 7 additions & 0 deletions spec/ckb/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@
expect(result).not_to be nil
expect(result).to be_a(Types::Epoch)
end

it "get epoch by number" do
number = 1
result = api.get_epoch_by_number(number)
expect(result).to be_a(Types::Epoch)
expect(result.number).to eq number.to_s
end
end
7 changes: 7 additions & 0 deletions spec/ckb/rpc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@
result = rpc.get_transaction_trace(trace_tx_hash)
expect(result).to be nil
end

it "get epoch by number" do
number = '1'
result = rpc.get_epoch_by_number(number)
expect(result).to be_a(Hash)
expect(result[:number]).to eq number
end
end

0 comments on commit 51eb43f

Please sign in to comment.