class Sibit::Blockchain
Blockchain.info API.
Public Class Methods
new(log: Sibit::Log.new, http: Sibit::Http.new, dry: false)
click to toggle source
Constructor.
# File lib/sibit/blockchain.rb, line 44 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/blockchain.rb, line 87 def balance(address) json = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://blockchain.info/rawaddr').append(address).add(limit: 0), accept: [200, 500] ) b = json['final_balance'] @log.info("The balance of #{address} is #{b} satoshi (#{json['n_tx']} txns)") b end
block(hash)
click to toggle source
This method should fetch a Blockchain
block and return as a hash.
# File lib/sibit/blockchain.rb, line 137 def block(hash) json = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://blockchain.info/rawblock').append(hash) ) { provider: self.class.name, hash: json['hash'], orphan: !json['main_chain'], next: json['next_block'][0], previous: json['prev_block'], txns: json['tx'].map do |t| { hash: t['hash'], outputs: t['out'].map do |o| { address: o['addr'], value: o['value'] } end } end } end
fees()
click to toggle source
Get recommended fees.
# File lib/sibit/blockchain.rb, line 98 def fees raise Sibit::NotSupportedError, 'fees() is not provided by Blockchain API' end
height(hash)
click to toggle source
The height of the block.
# File lib/sibit/blockchain.rb, line 77 def height(hash) json = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://blockchain.info/rawblock').append(hash) ) h = json['height'] @log.info("The height of #{hash} is #{h}") h end
latest()
click to toggle source
Gets the hash of the latest block.
# File lib/sibit/blockchain.rb, line 128 def latest hash = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://blockchain.info/latestblock') )['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/blockchain.rb, line 62 def next_of(_hash) raise Sibit::NotSupportedError, 'next_of() in Blockchain API is broken, always returns NULL' # json = Sibit::Json.new(http: @http, log: @log).get( # Iri.new('https://blockchain.info/rawblock').append(hash) # ) # nxt = json['next_block'][0] # if nxt.nil? # @log.info("There is no block after #{hash}") # else # @log.info("The next block of #{hash} is #{nxt}") # end # nxt end
price(currency = 'USD')
click to toggle source
Current price of BTC in USD (float returned).
# File lib/sibit/blockchain.rb, line 51 def price(currency = 'USD') h = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://blockchain.info/ticker') )[currency] raise Error, "Unrecognized currency #{currency}" if h.nil? price = h['15m'] @log.info("The price of BTC is #{price} USD") price end
push(hex)
click to toggle source
Push this transaction (in hex format) to the network.
# File lib/sibit/blockchain.rb, line 119 def push(hex) return if @dry Sibit::Json.new(http: @http, log: @log).post( Iri.new('https://blockchain.info/pushtx'), hex ) end
utxos(sources)
click to toggle source
Fetch all unspent outputs per address. The argument is an array of Bitcoin addresses.
# File lib/sibit/blockchain.rb, line 104 def utxos(sources) Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://blockchain.info/unspent').add(active: sources.join('|'), limit: 1000) )['unspent_outputs'].map do |u| { value: u['value'], hash: u['tx_hash_big_endian'], index: u['tx_output_n'], confirmations: u['confirmations'], script: [u['script']].pack('H*') } end end