lib/sibit/btc.rb in sibit-0.21.2 vs lib/sibit/btc.rb in sibit-0.21.3
- old
+ new
@@ -18,17 +18,18 @@
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
-require 'uri'
+require 'iri'
require 'json'
-require_relative 'version'
+require 'uri'
require_relative 'error'
-require_relative 'log'
require_relative 'http'
require_relative 'json'
+require_relative 'log'
+require_relative 'version'
# Btc.com API.
#
# Here: https://btc.com/api-doc
#
@@ -50,11 +51,11 @@
raise Sibit::NotSupportedError, 'Btc.com API doesn\'t provide prices'
end
# Gets the balance of the address, in satoshi.
def balance(address)
- uri = URI("https://chain.api.btc.com/v3/address/#{address}/unspent")
+ uri = Iri.new('https://chain.api.btc.com/v3/address').append(address).append('unspent')
json = Sibit::Json.new(http: @http, log: @log).get(uri)
if json['err_no'] == 1
@log.info("The balance of #{address} is zero (not found)")
return 0
end
@@ -74,25 +75,25 @@
end
# Get hash of the block after this one.
def next_of(hash)
head = Sibit::Json.new(http: @http, log: @log).get(
- URI("https://chain.api.btc.com/v3/block/#{hash}")
+ Iri.new('https://chain.api.btc.com/v3/block').append(hash)
)
data = head['data']
raise Sibit::Error, "The block #{hash} not found" if data.nil?
nxt = data['next_block_hash']
nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000'
- @log.info("The block #{hash} is the latest, there is no next block") if nxt.nil?
+ @log.info("In BTC.com 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
# The height of the block.
def height(hash)
json = Sibit::Json.new(http: @http, log: @log).get(
- URI("https://chain.api.btc.com/v3/block/#{hash}")
+ Iri.new('https://chain.api.btc.com/v3/block').append(hash)
)
data = json['data']
raise Sibit::Error, "The block #{hash} not found" if data.nil?
h = data['height']
raise Sibit::Error, "The block #{hash} found but the height is absent" if h.nil?
@@ -105,11 +106,11 @@
raise Sibit::NotSupportedError, 'Btc.com doesn\'t provide recommended fees'
end
# Gets the hash of the latest block.
def latest
- uri = URI('https://chain.api.btc.com/v3/block/latest')
+ uri = Iri.new('https://chain.api.btc.com/v3/block/latest')
json = Sibit::Json.new(http: @http, log: @log).get(uri)
data = json['data']
raise Sibit::Error, 'The latest block not found' if data.nil?
hash = data['hash']
@log.info("The hash of the latest block is #{hash}")
@@ -119,19 +120,19 @@
# Fetch all unspent outputs per address.
def utxos(sources)
txns = []
sources.each do |hash|
json = Sibit::Json.new(http: @http, log: @log).get(
- URI("https://chain.api.btc.com/v3/address/#{hash}/unspent")
+ Iri.new('https://chain.api.btc.com/v3/address').append(hash).append('unspent')
)
data = json['data']
raise Sibit::Error, "The address #{hash} not found" if data.nil?
txns = data['list']
next if txns.nil?
txns.each do |u|
outs = Sibit::Json.new(http: @http, log: @log).get(
- URI("https://chain.api.btc.com/v3/tx/#{u['tx_hash']}?verbose=3")
+ Iri.new('https://chain.api.btc.com/v3/tx').append(u['tx_hash']).add(verbose: 3)
)['data']['outputs']
outs.each_with_index do |o, i|
next unless o['addresses'].include?(hash)
txns << {
value: o['value'],
@@ -152,11 +153,11 @@
end
# This method should fetch a Blockchain block and return as a hash.
def block(hash)
head = Sibit::Json.new(http: @http, log: @log).get(
- URI("https://chain.api.btc.com/v3/block/#{hash}")
+ Iri.new('https://chain.api.btc.com/v3/block').append(hash)
)
data = head['data']
raise Sibit::Error, "The block #{hash} not found" if data.nil?
nxt = data['next_block_hash']
nxt = nil if nxt == '0000000000000000000000000000000000000000000000000000000000000000'
@@ -175,10 +176,11 @@
page = 1
psize = 50
all = []
loop do
data = Sibit::Json.new(http: @http, log: @log).get(
- URI("https://chain.api.btc.com/v3/block/#{hash}/tx?page=#{page}&pagesize=#{psize}")
+ Iri.new('https://chain.api.btc.com/v3/block')
+ .append(hash).append('tx').add(page: page, pagesize: psize)
)['data']
raise Sibit::Error, "The block #{hash} has no data at page #{page}" if data.nil?
list = data['list']
raise Sibit::Error, "The list is empty for block #{hash} at page #{page}" if list.nil?
txns = list.map do |t|