Class: Sibit::Blockchair

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

Overview

Btc.com API.

Instance Method Summary collapse

Constructor Details

#initialize(key: nil, log: Sibit::Log.new, http: Sibit::Http.new, dry: false) ⇒ Blockchair

Constructor.



41
42
43
44
45
46
# File 'lib/sibit/blockchair.rb', line 41

def initialize(key: nil, log: Sibit::Log.new, http: Sibit::Http.new, dry: false)
  @key = key
  @http = http
  @log = log
  @dry = dry
end

Instance Method Details

#balance(address) ⇒ Object

Gets the balance of the address, in satoshi.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sibit/blockchair.rb', line 65

def balance(address)
  json = Sibit::Json.new(http: @http, log: @log).get(
    URI("https://api.blockchair.com/bitcoin/dashboards/address/#{address}?#{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) ⇒ Object

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



104
105
106
# File 'lib/sibit/blockchair.rb', line 104

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

#feesObject

Get recommended fees, in satoshi per byte.



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

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

#height(_hash) ⇒ Object

The height of the block.



54
55
56
# File 'lib/sibit/blockchair.rb', line 54

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

#latestObject

Gets the hash of the latest block.



85
86
87
# File 'lib/sibit/blockchair.rb', line 85

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

#next_of(_hash) ⇒ Object

Get hash of the block after this one.



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

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') ⇒ Object

Current price of BTC in USD (float returned).



49
50
51
# File 'lib/sibit/blockchair.rb', line 49

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.



95
96
97
98
99
100
101
# File 'lib/sibit/blockchair.rb', line 95

def push(hex)
  Sibit::Json.new(http: @http, log: @log).post(
    URI("https://api.blockchair.com/bitcoin/push/transaction?#{the_key}"),
    "data=#{hex}"
  )
  @log.info("Transaction (#{hex.length} in hex) has been pushed to Blockchair")
end

#utxos(_sources) ⇒ Object

Fetch all unspent outputs per address.



90
91
92
# File 'lib/sibit/blockchair.rb', line 90

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