class Sibit::Cryptoapis
Btc.com API.
Public Class Methods
new(key, log: Sibit::Log.new, http: Sibit::Http.new, dry: false)
click to toggle source
Constructor.
# File lib/sibit/cryptoapis.rb, line 41 def initialize(key, log: Sibit::Log.new, http: Sibit::Http.new, dry: false) @key = key @http = http @log = log @dry = dry end
Public Instance Methods
balance(address)
click to toggle source
Gets the balance of the address, in satoshi.
# File lib/sibit/cryptoapis.rb, line 76 def balance(address) json = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api.cryptoapis.io/v1/bc/btc/mainnet/address').append(address), headers: headers )['payload'] b = (json['balance'].to_f * 100_000_000).to_i @log.info("The balance of #{address} is #{b} satoshi") b end
block(hash)
click to toggle source
This method should fetch a Blockchain
block and return as a hash.
# File lib/sibit/cryptoapis.rb, line 116 def block(hash) head = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks').append(hash), headers: headers )['payload'] { provider: self.class.name, hash: head['hash'], orphan: false, next: head['nextblockhash'], previous: head['previousblockhash'], txns: txns(hash) } end
fees()
click to toggle source
Get recommended fees, in satoshi per byte.
# File lib/sibit/cryptoapis.rb, line 87 def fees raise Sibit::NotSupportedError, 'Cryptoapis doesn\'t provide recommended fees' end
height(hash)
click to toggle source
The height of the block.
# File lib/sibit/cryptoapis.rb, line 65 def height(hash) json = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks').append(hash), headers: headers )['payload'] h = json['height'] @log.info("The height of #{hash} is #{h}") h end
latest()
click to toggle source
Gets the hash of the latest block.
# File lib/sibit/cryptoapis.rb, line 92 def latest hash = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks/latest'), headers: headers )['payload']['hash'] @log.info("The latest block hash is #{hash}") hash end
next_of(hash)
click to toggle source
Get hash of the block after this one.
# File lib/sibit/cryptoapis.rb, line 54 def next_of(hash) nxt = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api.cryptoapis.io/v1/bc/btc/mainnet/blocks').append(hash), headers: headers )['payload']['hash'] @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')
click to toggle source
Current price of BTC in USD (float returned).
# File lib/sibit/cryptoapis.rb, line 49 def price(_currency = 'USD') raise Sibit::NotSupportedError, 'Cryptoapis doesn\'t provide BTC price' end
push(hex)
click to toggle source
Push this transaction (in hex format) to the network.
# File lib/sibit/cryptoapis.rb, line 107 def push(hex) Sibit::Json.new(http: @http, log: @log).post( Iri.new('https://api.cryptoapis.io/v1/bc/btc/testnet/txs/send'), JSON.pretty_generate(hex: hex), headers: headers ) end
utxos(_sources)
click to toggle source
Fetch all unspent outputs per address.
# File lib/sibit/cryptoapis.rb, line 102 def utxos(_sources) raise Sibit::NotSupportedError, 'Not implemented yet' end
Private Instance Methods
headers()
click to toggle source
# File lib/sibit/cryptoapis.rb, line 133 def headers return {} if @key.nil? || @key.empty? { 'X-API-Key': @key } end
txns(hash)
click to toggle source
# File lib/sibit/cryptoapis.rb, line 140 def txns(hash) index = 0 limit = 200 all = [] loop do txns = Sibit::Json.new(http: @http, log: @log).get( Iri.new('https://api.cryptoapis.io/v1/bc/btc/mainnet/txs/block/') .append(hash).add(index: index, limit: limit), headers: headers )['payload'].map do |t| { hash: t['hash'], outputs: t['txouts'].map do |o| { address: o['addresses'][0], value: o['amount'].to_f * 100_000_000 } end } end all += txns index += txns.length break if txns.length < limit end all end