lib/sibit/cryptoapis.rb in sibit-0.15.0 vs lib/sibit/cryptoapis.rb in sibit-0.15.1

- old
+ new

@@ -44,19 +44,19 @@ @dry = dry end # Current price of BTC in USD (float returned). def price(_currency) - raise Sibit::Error, 'Not implemented yet' + raise Sibit::Error, 'Cryptoapis doesn\'t provide BTC price' end # Gets the balance of the address, in satoshi. def balance(address) - Sibit::Json.new(http: @http, log: @log).get( + (Sibit::Json.new(http: @http, log: @log).get( URI("https://api.cryptoapis.io/v1/bc/btc/mainnet/address/#{address}"), headers: headers - )['payload']['balance'].to_f * 100_000_000 + )['payload']['balance'].to_f * 100_000_000).to_i end # Get recommended fees, in satoshi per byte. def fees raise Sibit::Error, 'Not implemented yet' @@ -93,12 +93,34 @@ { hash: head['hash'], orphan: false, next: head['nextblockhash'], previous: head['previousblockhash'], - txns: Sibit::Json.new(http: @http, log: @log).get( - URI("https://api.cryptoapis.io/v1/bc/btc/mainnet/txs/block/#{hash}"), + txns: txns(hash) + } + end + + private + + def headers + { + 'X-API-Key': @key + } + end + + def txns(hash) + index = 0 + limit = 200 + all = [] + loop do + txns = Sibit::Json.new(http: @http, log: @log).get( + URI( + [ + 'https://api.cryptoapis.io/v1/bc/btc/mainnet/txs/block/', + "#{hash}?index=#{index}&limit=#{limit}" + ].join + ), headers: headers )['payload'].map do |t| { hash: t['hash'], outputs: t['txouts'].map do |o| @@ -107,17 +129,13 @@ value: o['amount'].to_f * 100_000_000 } end } end - } - end - - private - - def headers - { - 'X-API-Key': @key - } + all += txns + index += txns.length + break if txns.length < limit + end + all end end end