Class: Sibit::Blockchain
- Inherits:
-
Object
- Object
- Sibit::Blockchain
- Defined in:
- lib/sibit/blockchain.rb
Overview
Blockchain.info API.
It works through the Blockchain API: https://www.blockchain.com/api/blockchain_api
- Author
Yegor Bugayenko (yegor256@gmail.com)
- Copyright
Copyright (c) 2019-2026 Yegor Bugayenko
- License
MIT
Instance Method Summary collapse
-
#balance(address) ⇒ Object
Gets the balance of the address, in satoshi.
-
#block(hash) ⇒ Object
This method should fetch a Blockchain block and return as a hash.
-
#fees ⇒ Object
Get recommended fees.
-
#height(hash) ⇒ Object
The height of the block.
-
#initialize(log: Loog::NULL, http: Sibit::Http.new) ⇒ Blockchain
constructor
Constructor.
-
#latest ⇒ Object
Gets the hash of the latest block.
-
#next_of(_hash) ⇒ Object
Get hash of the block after this one.
-
#price(currency = 'USD') ⇒ Object
Current price of BTC in USD (float returned).
-
#push(hex) ⇒ Object
Push this transaction (in hex format) to the network.
-
#utxos(sources) ⇒ Object
Fetch all unspent outputs per address.
Constructor Details
#initialize(log: Loog::NULL, http: Sibit::Http.new) ⇒ Blockchain
Constructor.
26 27 28 29 |
# File 'lib/sibit/blockchain.rb', line 26 def initialize(log: Loog::NULL, http: Sibit::Http.new) @http = http @log = log end |
Instance Method Details
#balance(address) ⇒ Object
Gets the balance of the address, in satoshi.
58 59 60 61 62 63 64 65 66 |
# File 'lib/sibit/blockchain.rb', line 58 def balance(address) json = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://blockchain.info/rawaddr').append(address).add(limit: 0) ) b = json['final_balance'] raise(Sibit::Error, "The balance of #{address} is absent") unless b.is_a?(Integer) @log.debug("The balance of #{address} is #{b} satoshi (#{json['n_tx']} txns)") b end |
#block(hash) ⇒ Object
This method should fetch a Blockchain block and return as a hash.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/sibit/blockchain.rb', line 120 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 ⇒ Object
Get recommended fees.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sibit/blockchain.rb', line 69 def fees json = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api.blockchain.info/mempool/fees') ) @log.debug( [ 'Currently recommended Bitcoin fees: ', "#{json['regular']}/#{json['priority']}/#{json['limits']['max']} sat/byte" ].join ) { S: (json['regular'] / 3.0).ceil, M: json['regular'], L: json['priority'], XL: json['limits']['max'] } end |
#height(hash) ⇒ Object
The height of the block.
48 49 50 51 52 53 54 55 |
# File 'lib/sibit/blockchain.rb', line 48 def height(hash) h = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://blockchain.info/rawblock').append(hash) )['height'] raise(Sibit::Error, "The block #{hash} found but the height is absent") if h.nil? @log.debug("The height of #{hash} is #{h}") h end |
#latest ⇒ Object
Gets the hash of the latest block.
111 112 113 114 115 116 117 |
# File 'lib/sibit/blockchain.rb', line 111 def latest hash = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://blockchain.info/latestblock') )['hash'] @log.debug("The latest block hash is #{hash}") hash end |
#next_of(_hash) ⇒ Object
Get hash of the block after this one.
43 44 45 |
# File 'lib/sibit/blockchain.rb', line 43 def next_of(_hash) raise(Sibit::NotSupportedError, 'next_of() in Blockchain API is broken, always returns NULL') end |
#price(currency = 'USD') ⇒ Object
Current price of BTC in USD (float returned).
32 33 34 35 36 37 38 39 40 |
# File 'lib/sibit/blockchain.rb', line 32 def price(currency = 'USD') h = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://blockchain.info/ticker') )[currency] raise(Sibit::Error, "Unrecognized currency #{currency}") if h.nil? price = h['15m'] @log.debug("The price of BTC is #{price} USD") price end |
#push(hex) ⇒ Object
Push this transaction (in hex format) to the network.
104 105 106 107 108 |
# File 'lib/sibit/blockchain.rb', line 104 def push(hex) Sibit::Json.new(http: @http, log: @log).post( Iri.new('https://blockchain.info/pushtx'), "tx=#{CGI.escape(hex)}" ) end |
#utxos(sources) ⇒ Object
Fetch all unspent outputs per address. The argument is an array of Bitcoin addresses.
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sibit/blockchain.rb', line 89 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 |