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
#initialize(log: Sibit::Log.new, http: Sibit::Http.new, dry: false) ⇒ Bitcoinchain
Constructor.
40 41 42 43 44 |
# File 'lib/sibit/bitcoinchain.rb', line 40 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.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/sibit/bitcoinchain.rb', line 70 def balance(address) json = Sibit::Json.new(http: @http, log: @log).get( URI("https://api-r.bitcoinchain.com/v1/address/#{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.
112 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 |
# File 'lib/sibit/bitcoinchain.rb', line 112 def block(hash) head = Sibit::Json.new(http: @http, log: @log).get( URI("https://api-r.bitcoinchain.com/v1/block/#{hash}") )[0] raise Sibit::Error, "The block #{hash} is not found" if head.nil? txs = Sibit::Json.new(http: @http, log: @log).get( URI("https://api-r.bitcoinchain.com/v1/block/txs/#{hash}") ) nxt = head['next_block'] nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000' { 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.
87 88 89 |
# File 'lib/sibit/bitcoinchain.rb', line 87 def fees raise Sibit::NotSupportedError, 'Not implemented yet' end |
#height(_hash) ⇒ Object
The height of the block.
52 53 54 |
# File 'lib/sibit/bitcoinchain.rb', line 52 def height(_hash) raise Sibit::NotSupportedError, 'Bitcoinchain API doesn\'t provide height()' end |
#latest ⇒ Object
Gets the hash of the latest block.
92 93 94 95 96 97 98 |
# File 'lib/sibit/bitcoinchain.rb', line 92 def latest hash = Sibit::Json.new(http: @http, log: @log).get( URI('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.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sibit/bitcoinchain.rb', line 57 def next_of(hash) block = Sibit::Json.new(http: @http, log: @log).get( URI("https://api-r.bitcoinchain.com/v1/block/#{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).
47 48 49 |
# File 'lib/sibit/bitcoinchain.rb', line 47 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.
106 107 108 |
# File 'lib/sibit/bitcoinchain.rb', line 106 def push(_hex) raise Sibit::NotSupportedError, 'Not implemented yet' end |
#utxos(_sources) ⇒ Object
Fetch all unspent outputs per address.
101 102 103 |
# File 'lib/sibit/bitcoinchain.rb', line 101 def utxos(_sources) raise Sibit::NotSupportedError, 'Not implemented yet' end |