Class: Sibit::Bitcoinchain
- Inherits:
-
Object
- Object
- Sibit::Bitcoinchain
- Defined in:
- lib/sibit/bitcoinchain.rb
Overview
Bitcoinchain.com 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, in satoshi per byte.
-
#height(_hash) ⇒ Object
The height of the block.
-
#initialize(log: Loog::NULL, http: Sibit::Http.new) ⇒ Bitcoinchain
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) ⇒ Bitcoinchain
Constructor.
23 24 25 26 |
# File 'lib/sibit/bitcoinchain.rb', line 23 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.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/sibit/bitcoinchain.rb', line 52 def balance(address) json = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api-r.bitcoinchain.com/v1/address').append(address), accept: [200, 409] )[0] b = json['balance'] if b.nil? @log.debug("The balance of #{address} is not visible") return 0 end b = Sibit::Coins.new(b).satoshi @log.debug("The balance of #{address} is #{b} satoshi (#{json['transactions']} txns)") b end |
#block(hash) ⇒ Object
This method should fetch a Blockchain block and return as a hash. Raises an exception if the block is not found.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/sibit/bitcoinchain.rb', line 93 def block(hash) head = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api-r.bitcoinchain.com/v1/block').append(hash) )[0] raise(Sibit::Error, "The block #{hash} is not found") if head.nil? txs = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api-r.bitcoinchain.com/v1/block/txs').append(hash) ) nxt = head['next_block'] nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000' { provider: self.class.name, hash: head['hash'], orphan: !head['is_main'], next: nxt, previous: head['prev_block'], txns: txs[0]['txs'].map do |t| { hash: t['self_hash'], outputs: t['outputs'].map do |o| { address: o['receiver'], value: Sibit::Coins.new(o['value']).satoshi } end } end } end |
#fees ⇒ Object
Get recommended fees, in satoshi per byte.
68 69 70 |
# File 'lib/sibit/bitcoinchain.rb', line 68 def fees raise(Sibit::NotSupportedError, 'Not implemented yet') end |
#height(_hash) ⇒ Object
The height of the block.
34 35 36 |
# File 'lib/sibit/bitcoinchain.rb', line 34 def height(_hash) raise(Sibit::NotSupportedError, 'Bitcoinchain API doesn\'t provide height()') end |
#latest ⇒ Object
Gets the hash of the latest block.
73 74 75 76 77 78 79 |
# File 'lib/sibit/bitcoinchain.rb', line 73 def latest hash = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api-r.bitcoinchain.com/v1/status') )['hash'] @log.debug("The latest block hash is #{hash}") hash end |
#next_of(hash) ⇒ Object
Get hash of the block after this one.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/sibit/bitcoinchain.rb', line 39 def next_of(hash) block = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api-r.bitcoinchain.com/v1/block').append(hash) )[0] raise(Sibit::Error, "Block #{hash} not found") if block.nil? nxt = block['next_block'] nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000' @log.debug("The block #{hash} is the latest, there is no next block") if nxt.nil? @log.debug("The next block of #{hash} is #{nxt}") unless nxt.nil? nxt end |
#price(_currency = 'USD') ⇒ Object
Current price of BTC in USD (float returned).
29 30 31 |
# File 'lib/sibit/bitcoinchain.rb', line 29 def price(_currency = 'USD') raise(Sibit::NotSupportedError, 'Bitcoinchain API doesn\'t provide BTC price') end |
#push(_hex) ⇒ Object
Push this transaction (in hex format) to the network.
87 88 89 |
# File 'lib/sibit/bitcoinchain.rb', line 87 def push(_hex) raise(Sibit::NotSupportedError, 'Not implemented yet') end |
#utxos(_sources) ⇒ Object
Fetch all unspent outputs per address.
82 83 84 |
# File 'lib/sibit/bitcoinchain.rb', line 82 def utxos(_sources) raise(Sibit::NotSupportedError, 'Not implemented yet') end |