Class: Sibit::Blockchain
- Inherits:
-
Object
- Object
- Sibit::Blockchain
- Defined in:
- lib/sibit/blockchain.rb
Overview
Blockchain.info API.
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: Sibit::Log.new, http: Sibit::Http.new, dry: false) ⇒ 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: Sibit::Log.new, http: Sibit::Http.new, dry: false) ⇒ Blockchain
Constructor.
43 44 45 46 47 |
# File 'lib/sibit/blockchain.rb', line 43 def initialize(log: Sibit::Log.new, http: Sibit::Http.new, dry: false) @http = http @log = log @dry = dry end |
Instance Method Details
#balance(address) ⇒ Object
Gets the balance of the address, in satoshi.
76 77 78 79 80 81 82 83 84 |
# File 'lib/sibit/blockchain.rb', line 76 def balance(address) json = Sibit::Json.new(http: @http, log: @log).get( URI("https://blockchain.info/rawaddr/#{address}?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) ⇒ Object
This method should fetch a Blockchain block and return as a hash.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/sibit/blockchain.rb', line 126 def block(hash) json = Sibit::Json.new(http: @http, log: @log).get( URI("https://blockchain.info/rawblock/#{hash}") ) { 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.
87 88 89 |
# File 'lib/sibit/blockchain.rb', line 87 def fees raise Sibit::NotSupportedError, 'fees() is not provided by Blockchain API' end |
#height(hash) ⇒ Object
The height of the block.
66 67 68 69 70 71 72 73 |
# File 'lib/sibit/blockchain.rb', line 66 def height(hash) json = Sibit::Json.new(http: @http, log: @log).get( URI("https://blockchain.info/rawblock/#{hash}") ) h = json['height'] @log.info("The height of #{hash} is #{h}") h end |
#latest ⇒ Object
Gets the hash of the latest block.
117 118 119 120 121 122 123 |
# File 'lib/sibit/blockchain.rb', line 117 def latest hash = Sibit::Json.new(http: @http, log: @log).get( URI('https://blockchain.info/latestblock') )['hash'] @log.info("The latest block hash is #{hash}") hash end |
#next_of(_hash) ⇒ Object
Get hash of the block after this one.
61 62 63 |
# File 'lib/sibit/blockchain.rb', line 61 def next_of(_hash) raise Sibit::NotSupportedError, 'Blockchain API doesn\'t provide next_of()' end |
#price(currency = 'USD') ⇒ Object
Current price of BTC in USD (float returned).
50 51 52 53 54 55 56 57 58 |
# File 'lib/sibit/blockchain.rb', line 50 def price(currency = 'USD') h = Sibit::Json.new(http: @http, log: @log).get( URI('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) ⇒ Object
Push this transaction (in hex format) to the network.
108 109 110 111 112 113 114 |
# File 'lib/sibit/blockchain.rb', line 108 def push(hex) return if @dry Sibit::Json.new(http: @http, log: @log).post( URI('https://blockchain.info/pushtx'), hex ) end |
#utxos(sources) ⇒ Object
Fetch all unspent outputs per address. The argument is an array of Bitcoin addresses.
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/sibit/blockchain.rb', line 93 def utxos(sources) Sibit::Json.new(http: @http, log: @log).get( URI("https://blockchain.info/unspent?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 |