class Sibit::Bitcoinchain

Btc.com API.

Public Class Methods

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

Constructor.

# File lib/sibit/bitcoinchain.rb, line 41
def initialize(log: Sibit::Log.new, http: Sibit::Http.new, dry: false)
  @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/bitcoinchain.rb, line 71
def balance(address)
  json = Sibit::Json.new(http: @http, log: @log).get(
    Iri.new('https://api-r.bitcoinchain.com/v1/address').append(address),
    accept: [200, 409]
  )[0]
  b = json['balance']
  if b.nil?
    @log.info("The balance of #{address} is not visible")
    return 0
  end
  b *= 100_000_000
  b = b.to_i
  @log.info("The balance of #{address} is #{b} satoshi (#{json['transactions']} txns)")
  b
end
block(hash) click to toggle source

This method should fetch a Blockchain block and return as a hash. Raises an exception if the block is not found.

# File lib/sibit/bitcoinchain.rb, line 113
def block(hash)
  head = Sibit::Json.new(http: @http, log: @log).get(
    Iri.new('https://api-r.bitcoinchain.com/v1/block').append(hash)
  )[0]
  raise Sibit::Error, "The block #{hash} is not found" if head.nil?
  txs = Sibit::Json.new(http: @http, log: @log).get(
    Iri.new('https://api-r.bitcoinchain.com/v1/block/txs').append(hash)
  )
  nxt = head['next_block']
  nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000'
  {
    provider: self.class.name,
    hash: head['hash'],
    orphan: !head['is_main'],
    next: nxt,
    previous: head['prev_block'],
    txns: txs[0]['txs'].map do |t|
      {
        hash: t['self_hash'],
        outputs: t['outputs'].map do |o|
          {
            address: o['receiver'],
            value: o['value'] * 100_000_000
          }
        end
      }
    end
  }
end
fees() click to toggle source

Get recommended fees, in satoshi per byte.

# File lib/sibit/bitcoinchain.rb, line 88
def fees
  raise Sibit::NotSupportedError, 'Not implemented yet'
end
height(_hash) click to toggle source

The height of the block.

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

Gets the hash of the latest block.

# File lib/sibit/bitcoinchain.rb, line 93
def latest
  hash = Sibit::Json.new(http: @http, log: @log).get(
    Iri.new('https://api-r.bitcoinchain.com/v1/status')
  )['hash']
  @log.info("The latest block hash is #{hash}")
  hash
end
next_of(hash) click to toggle source

Get hash of the block after this one.

# File lib/sibit/bitcoinchain.rb, line 58
def next_of(hash)
  block = Sibit::Json.new(http: @http, log: @log).get(
    Iri.new('https://api-r.bitcoinchain.com/v1/block').append(hash)
  )[0]
  raise Sibit::Error, "Block #{hash} not found" if block.nil?
  nxt = block['next_block']
  nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000'
  @log.info("The block #{hash} is the latest, there is no next block") if nxt.nil?
  @log.info("The next block of #{hash} is #{nxt}") unless nxt.nil?
  nxt
end
price(_currency = 'USD') click to toggle source

Current price of BTC in USD (float returned).

# File lib/sibit/bitcoinchain.rb, line 48
def price(_currency = 'USD')
  raise Sibit::NotSupportedError, 'Bitcoinchain API doesn\'t provide BTC price'
end
push(_hex) click to toggle source

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

# File lib/sibit/bitcoinchain.rb, line 107
def push(_hex)
  raise Sibit::NotSupportedError, 'Not implemented yet'
end
utxos(_sources) click to toggle source

Fetch all unspent outputs per address.

# File lib/sibit/bitcoinchain.rb, line 102
def utxos(_sources)
  raise Sibit::NotSupportedError, 'Not implemented yet'
end