Class: Sibit::Bitcoinchain
- Inherits:
-
Object
- Object
- Sibit::Bitcoinchain
- Defined in:
- lib/sibit/bitcoinchain.rb
Overview
Btc.com 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, in satoshi per byte.
-
#height(_hash) ⇒ Object
The height of the block.
-
#initialize(log: Sibit::Log.new, http: Sibit::Http.new, dry: false) ⇒ 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
Instance Method Details
#balance(address) ⇒ Object
Gets the balance of the address, in satoshi.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sibit/bitcoinchain.rb', line 71 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.info("The balance of #{address} is not visible") return 0 end b *= 100_000_000 b = b.to_i @log.info("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.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/sibit/bitcoinchain.rb', line 113 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: o['value'] * 100_000_000 } end } end } end |
#fees ⇒ Object
Get recommended fees, in satoshi per byte.
88 89 90 |
# File 'lib/sibit/bitcoinchain.rb', line 88 def fees raise Sibit::NotSupportedError, 'Not implemented yet' end |
#height(_hash) ⇒ Object
The height of the block.
53 54 55 |
# File 'lib/sibit/bitcoinchain.rb', line 53 def height(_hash) raise Sibit::NotSupportedError, 'Bitcoinchain API doesn\'t provide height()' end |
#latest ⇒ Object
Gets the hash of the latest block.
93 94 95 96 97 98 99 |
# File 'lib/sibit/bitcoinchain.rb', line 93 def latest hash = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api-r.bitcoinchain.com/v1/status') )['hash'] @log.info("The latest block hash is #{hash}") hash end |
#next_of(hash) ⇒ Object
Get hash of the block after this one.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/sibit/bitcoinchain.rb', line 58 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.info("The block #{hash} is the latest, there is no next block") if nxt.nil? @log.info("The next block of #{hash} is #{nxt}") unless nxt.nil? nxt end |
#price(_currency = 'USD') ⇒ Object
Current price of BTC in USD (float returned).
48 49 50 |
# File 'lib/sibit/bitcoinchain.rb', line 48 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.
107 108 109 |
# File 'lib/sibit/bitcoinchain.rb', line 107 def push(_hex) raise Sibit::NotSupportedError, 'Not implemented yet' end |
#utxos(_sources) ⇒ Object
Fetch all unspent outputs per address.
102 103 104 |
# File 'lib/sibit/bitcoinchain.rb', line 102 def utxos(_sources) raise Sibit::NotSupportedError, 'Not implemented yet' end |