lib/sibit/btc.rb in sibit-0.13.0 vs lib/sibit/btc.rb in sibit-0.13.1
- old
+ new
@@ -50,10 +50,15 @@
# Gets the balance of the address, in satoshi.
def balance(address)
uri = URI("https://chain.api.btc.com/v3/address/#{address}/unspent")
json = Sibit::Json.new(http: @http, log: @log).get(uri)
- txns = json['data']['list']
+ data = json['data']
+ if data.nil?
+ @log.info("The balance of #{address} is zero (not found)")
+ return 0
+ end
+ txns = data['list']
balance = txns.map { |tx| tx['value'] }.inject(&:+) || 0
@log.info("The balance of #{address} is #{balance}, total txns: #{txns.count}")
balance
end