lib/sibit/btc.rb in sibit-0.18.6 vs lib/sibit/btc.rb in sibit-0.18.7
- old
+ new
@@ -81,11 +81,13 @@
# The height of the block.
def height(hash)
json = Sibit::Json.new(http: @http, log: @log).get(
URI("https://chain.api.btc.com/v3/block/#{hash}")
)
- h = json['data']['height']
+ data = json['data']
+ raise Sibit::Error, "The block #{hash} not found" if data.nil?
+ h = data['height']
@log.info("The height of #{hash} is #{h}")
h
end
# Get recommended fees, in satoshi per byte.
@@ -95,11 +97,13 @@
# Gets the hash of the latest block.
def latest
uri = URI('https://chain.api.btc.com/v3/block/latest')
json = Sibit::Json.new(http: @http, log: @log).get(uri)
- hash = json['data']['hash']
+ data = json['data']
+ raise Sibit::Error, 'The latest block not found' if data.nil?
+ hash = data['hash']
@log.info("The hash of the latest block is #{hash}")
hash
end
# Fetch all unspent outputs per address.
@@ -107,11 +111,13 @@
txns = []
sources.each do |hash|
json = Sibit::Json.new(http: @http, log: @log).get(
URI("https://chain.api.btc.com/v3/address/#{hash}/unspent")
)
- json['data']['list'].each do |u|
+ data = json['data']
+ raise Sibit::Error, "The address #{hash} not found" if data.nil?
+ data['list'].each do |u|
outs = Sibit::Json.new(http: @http, log: @log).get(
URI("https://chain.api.btc.com/v3/tx/#{u['tx_hash']}?verbose=3")
)['data']['outputs']
outs.each_with_index do |o, i|
next unless o['addresses'].include?(hash)
@@ -136,16 +142,18 @@
# This method should fetch a Blockchain block and return as a hash.
def block(hash)
head = Sibit::Json.new(http: @http, log: @log).get(
URI("https://chain.api.btc.com/v3/block/#{hash}")
)
- nxt = head['data']['next_block_hash']
+ data = head['data']
+ raise Sibit::Error, "The block #{hash} not found" if data.nil?
+ nxt = data['next_block_hash']
nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000'
{
- hash: head['data']['hash'],
- orphan: head['data']['is_orphan'],
+ hash: data['hash'],
+ orphan: data['is_orphan'],
next: nxt,
- previous: head['data']['prev_block_hash'],
+ previous: data['prev_block_hash'],
txns: txns(hash)
}
end
private