class Sibit::Blockchair

Btc.com API.

Public Class Methods

new(key: nil, log: Sibit::Log.new, http: Sibit::Http.new, dry: false) click to toggle source

Constructor.

# File lib/sibit/blockchair.rb, line 42
def initialize(key: nil, log: Sibit::Log.new, http: Sibit::Http.new, dry: false)
  @key = key
  @http = http
  @log = log
  @dry = dry
end

Public Instance Methods

balance(address) click to toggle source

Gets the balance of the address, in satoshi.

# File lib/sibit/blockchair.rb, line 66
def balance(address)
  json = Sibit::Json.new(http: @http, log: @log).get(
    Iri.new('https://api.blockchair.com/bitcoin/dashboards/address').append(address).fragment(the_key)
  )['data'][address]
  if json.nil?
    @log.info("Address #{address} not found")
    return 0
  end
  a = json['address']
  b = a['balance']
  @log.info("The balance of #{address} is #{b} satoshi")
  b
end
block(_hash) click to toggle source

This method should fetch a Blockchain block and return as a hash.

# File lib/sibit/blockchair.rb, line 105
def block(_hash)
  raise Sibit::NotSupportedError, 'Blockchair doesn\'t implement block()'
end
fees() click to toggle source

Get recommended fees, in satoshi per byte.

# File lib/sibit/blockchair.rb, line 81
def fees
  raise Sibit::NotSupportedError, 'Blockchair doesn\'t implement fees()'
end
height(_hash) click to toggle source

The height of the block.

# File lib/sibit/blockchair.rb, line 55
def height(_hash)
  raise Sibit::NotSupportedError, 'Blockchair API doesn\'t provide height()'
end
latest() click to toggle source

Gets the hash of the latest block.

# File lib/sibit/blockchair.rb, line 86
def latest
  raise Sibit::NotSupportedError, 'Blockchair doesn\'t implement latest()'
end
next_of(_hash) click to toggle source

Get hash of the block after this one.

# File lib/sibit/blockchair.rb, line 60
def next_of(_hash)
  # They don't provide next block hash
  raise Sibit::NotSupportedError, 'Blockchair API doesn\'t provide next_of()'
end
price(_currency = 'USD') click to toggle source

Current price of BTC in USD (float returned).

# File lib/sibit/blockchair.rb, line 50
def price(_currency = 'USD')
  raise Sibit::NotSupportedError, 'Blockchair doesn\'t provide BTC price'
end
push(hex) click to toggle source

Push this transaction (in hex format) to the network.

# File lib/sibit/blockchair.rb, line 96
def push(hex)
  Sibit::Json.new(http: @http, log: @log).post(
    Iri.new('https://api.blockchair.com/bitcoin/push/transaction').fragment(the_key),
    "data=#{hex}"
  )
  @log.info("Transaction (#{hex.length} in hex) has been pushed to Blockchair")
end
utxos(_sources) click to toggle source

Fetch all unspent outputs per address.

# File lib/sibit/blockchair.rb, line 91
def utxos(_sources)
  raise Sibit::NotSupportedError, 'Blockchair doesn\'t implement utxos()'
end

Private Instance Methods

the_key() click to toggle source
# File lib/sibit/blockchair.rb, line 111
def the_key
  @key.nil? ? '' : "key=#{CGI.escape(@key)}"
end