lib/sibit.rb in sibit-0.18.8 vs lib/sibit.rb in sibit-0.19.0
- old
+ new
@@ -53,13 +53,11 @@
@api = api
end
# Current price of 1 BTC in USD (or another currency), float returned.
def price(currency = 'USD')
- first_one do |api|
- api.price(currency)
- end
+ @api.price(currency)
end
# Generates new Bitcon private key and returns in Hash160 format.
def generate
key = Bitcoin::Key.generate.priv
@@ -74,33 +72,27 @@
key.addr
end
# Gets the balance of the address, in satoshi.
def balance(address)
- first_one do |api|
- api.balance(address)
- end
+ @api.balance(address)
end
# Get the height of the block.
def height(hash)
- first_one do |api|
- api.height(hash)
- end
+ @api.height(hash)
end
# Get the hash of the next block.
def next_of(hash)
- first_one do |api|
- api.next_of(hash)
- end
+ @api.next_of(hash)
end
# Get recommended fees, in satoshi per byte. The method returns
# a hash: { S: 12, M: 45, L: 100, XL: 200 }
def fees
- first_one(&:fees)
+ @api.fees
end
# Sends a payment and returns the transaction hash.
#
# If the payment can't be signed (the key is wrong, for example) or the
@@ -122,11 +114,11 @@
p = price('USD')
satoshi = satoshi(amount)
builder = Bitcoin::Builder::TxBuilder.new
unspent = 0
size = 100
- utxos = first_one { |api| api.utxos(sources.keys) }
+ utxos = @api.utxos(sources.keys)
@log.info("#{utxos.count} UTXOs found, these will be used \
(value/confirmations at tx_hash):")
utxos.each do |utxo|
unspent += utxo[:value]
builder.input do |i|
@@ -168,19 +160,17 @@
Tx size: #{size} bytes
Unspent: #{num(unspent, p)}
Amount: #{num(satoshi, p)}
Target address: #{target}
Change address is #{change}")
- first_one do |api|
- api.push(tx.to_payload.bth)
- end
+ @api.push(tx.to_payload.bth)
tx.hash
end
# Gets the hash of the latest block.
def latest
- first_one(&:latest)
+ @api.latest
end
# You call this method and provide a callback. You provide the hash
# of the starting block. The method will go through the Blockchain,
# fetch blocks and find transactions, one by one, passing them to the
@@ -195,20 +185,20 @@
block = start
count = 0
wrong = []
json = {}
loop do
- json = first_one { |api| api.block(block) }
+ json = @api.block(block)
if json[:orphan]
steps = 4
@log.info("Orphan block found at #{block}, moving #{steps} steps back...")
wrong << block
steps.times do
block = json[:previous]
wrong << block
@log.info("Moved back to #{block}")
- json = first_one { |api| api.block(block) }
+ json = @api.block(block)
end
next
end
checked = 0
checked_outputs = 0
@@ -239,29 +229,9 @@
@log.info("Scanned from #{start} to #{json[:hash]} (#{count} blocks)")
json[:hash]
end
private
-
- def first_one
- return yield @api unless @api.is_a?(Array)
- done = false
- result = nil
- @api.each do |api|
- begin
- result = yield api
- done = true
- break
- rescue Sibit::Error => e
- @log.info("The API #{api.class.name} failed: #{e.message}")
- end
- end
- unless done
- raise Sibit::Error, "No APIs out of #{@api.length} managed to succeed: \
-#{@api.map { |a| a.class.name }.join(', ')}"
- end
- result
- end
def num(satoshi, usd)
format(
'%<satoshi>ss/$%<dollars>0.2f',
satoshi: satoshi.to_s.gsub(/\d(?=(...)+$)/, '\0,'),