Class: Sibit::Blockchair

Inherits:
Object
  • Object
show all
Defined in:
lib/sibit/blockchair.rb

Overview

Blockchair.com API.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright (c) 2019-2026 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(key: nil, log: Loog::NULL, http: Sibit::Http.new) ⇒ Blockchair

Constructor.



22
23
24
25
26
# File 'lib/sibit/blockchair.rb', line 22

def initialize(key: nil, log: Loog::NULL, http: Sibit::Http.new)
  @key = key
  @http = http
  @log = log
end

Instance Method Details

#balance(address) ⇒ Object

Gets the balance of the address, in satoshi.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sibit/blockchair.rb', line 44

def balance(address)
  uri = Iri.new('https://api.blockchair.com/bitcoin/dashboards/address').append(address)
  uri = uri.add(key: @key) unless @key.nil?
  json = Sibit::Json.new(http: @http, log: @log).get(uri)['data'][address]
  if json.nil?
    @log.debug("Address #{address} not found")
    return 0
  end
  b = json['address']['balance']
  @log.debug("The balance of #{address} is #{b} satoshi")
  b
end

#block(_hash) ⇒ Object

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



81
82
83
# File 'lib/sibit/blockchair.rb', line 81

def block(_hash)
  raise(Sibit::NotSupportedError, 'Blockchair doesn\'t implement block()')
end

#feesObject

Get recommended fees, in satoshi per byte.



58
59
60
# File 'lib/sibit/blockchair.rb', line 58

def fees
  raise(Sibit::NotSupportedError, 'Blockchair doesn\'t implement fees()')
end

#height(_hash) ⇒ Object

The height of the block.



34
35
36
# File 'lib/sibit/blockchair.rb', line 34

def height(_hash)
  raise(Sibit::NotSupportedError, 'Blockchair API doesn\'t provide height()')
end

#latestObject

Gets the hash of the latest block.



63
64
65
# File 'lib/sibit/blockchair.rb', line 63

def latest
  raise(Sibit::NotSupportedError, 'Blockchair doesn\'t implement latest()')
end

#next_of(_hash) ⇒ Object

Get hash of the block after this one.



39
40
41
# File 'lib/sibit/blockchair.rb', line 39

def next_of(_hash)
  raise(Sibit::NotSupportedError, 'Blockchair API doesn\'t provide next_of()')
end

#price(_currency = 'USD') ⇒ Object

Current price of BTC in USD (float returned).



29
30
31
# File 'lib/sibit/blockchair.rb', line 29

def price(_currency = 'USD')
  raise(Sibit::NotSupportedError, 'Blockchair doesn\'t provide BTC price')
end

#push(hex) ⇒ Object

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



73
74
75
76
77
78
# File 'lib/sibit/blockchair.rb', line 73

def push(hex)
  uri = Iri.new('https://api.blockchair.com/bitcoin/push/transaction')
  uri = uri.add(key: @key) unless @key.nil?
  Sibit::Json.new(http: @http, log: @log).post(uri, "data=#{hex}")
  @log.debug("Transaction (#{hex.length} in hex) has been pushed to Blockchair")
end

#utxos(_sources) ⇒ Object

Fetch all unspent outputs per address.



68
69
70
# File 'lib/sibit/blockchair.rb', line 68

def utxos(_sources)
  raise(Sibit::NotSupportedError, 'Blockchair doesn\'t implement utxos()')
end