lib/sibit.rb in sibit-0.7.3 vs lib/sibit.rb in sibit-0.7.4
- old
+ new
@@ -70,11 +70,11 @@
# Constructor.
#
# You may provide the log you want to see the messages in. If you don't
# provide anything, the console will be used. The object you provide
- # has to respond to the method +debug+ or +puts+ in order to receive logging
+ # has to respond to the method +info+ or +puts+ in order to receive logging
# messages.
def initialize(log: STDOUT)
@log = log
end
@@ -96,12 +96,12 @@
end
# Gets the balance of the address, in satoshi.
def balance(address)
json = get_json("/rawaddr/#{address}")
- debug("Total transactions: #{json['n_tx']}")
- debug("Received/sent: #{json['total_received']}/#{json['total_sent']}")
+ info("Total transactions: #{json['n_tx']}")
+ info("Received/sent: #{json['total_received']}/#{json['total_sent']}")
json['final_balance']
end
# Sends a payment and returns the transaction hash.
#
@@ -126,31 +126,35 @@
unspent = 0
size = 100
utxos = get_json(
"/unspent?active=#{sources.keys.join('|')}&limit=1000"
)['unspent_outputs']
- debug("#{utxos.count} UTXOs found:")
+ info("#{utxos.count} UTXOs found:")
utxos.each do |utxo|
unspent += utxo['value']
builder.input do |i|
i.prev_out(utxo['tx_hash_big_endian'])
i.prev_out_index(utxo['tx_output_n'])
i.prev_out_script = [utxo['script']].pack('H*')
address = Bitcoin::Script.new([utxo['script']].pack('H*')).get_address
i.signature_key(key(sources[address]))
end
size += 180
- debug(" #{utxo['value']}/#{utxo['confirmations']} at #{utxo['tx_hash_big_endian']}")
+ info(" #{utxo['value']}/#{utxo['confirmations']} at #{utxo['tx_hash_big_endian']}")
break if unspent > satoshi
end
raise Error, "Not enough funds to send #{amount}, only #{unspent} left" if unspent < satoshi
builder.output(satoshi, target)
+ f = mfee(fee, size)
tx = builder.tx(
input_value: unspent,
- leave_fee: mfee(fee, size),
+ leave_fee: f,
change_address: change
)
+ info("A new Bitcoin transaction #{tx.hash} prepared; #{tx.in.count} inputs; \
+#{tx.out.count} outputs; fee is #{f}; size is #{size}; unspent is #{unspent}; \
+amount is #{amount}; target address is #{target}; change address is #{change}")
post_tx(tx.to_payload.bth)
tx.hash
end
# Gets the hash of the latest block.
@@ -163,11 +167,11 @@
# response for correctness.
def get_json(uri)
start = Time.now
res = Net::HTTP.get_response(URI('https://blockchain.info' + uri))
raise Error, "Failed to retrieve #{uri}: #{res.code}" unless res.code == '200'
- debug("GET #{uri}: #{res.code}/#{res.body.length}b in #{age(start)}")
+ info("GET #{uri}: #{res.code}/#{res.body.length}b in #{age(start)}")
JSON.parse(res.body)
end
private
@@ -180,22 +184,15 @@
end
def mfee(fee, size)
return fee.to_i if fee.is_a?(Integer)
raise Error, 'Fee should either be a String or Integer' unless fee.is_a?(String)
- case fee
- when 'S'
- return 10 * size
- when 'M'
- return 50 * size
- when 'L'
- return 100 * size
- when 'XL'
- return 250 * size
- else
- raise Error, "Can't understand the fee: #{fee.inspect}"
- end
+ return 10 * size if fee == 'S'
+ return 50 * size if fee == 'M'
+ return 100 * size if fee == 'L'
+ return 250 * size if fee == 'XL'
+ raise Error, "Can't understand the fee: #{fee.inspect}"
end
# Make key from private key string in Hash160.
def key(hash160)
key = Bitcoin::Key.new
@@ -210,15 +207,15 @@
def post_tx(body)
start = Time.now
uri = URI('https://blockchain.info/pushtx')
res = Net::HTTP.post_form(uri, tx: body)
raise Error, "Failed to post tx to #{uri}: #{res.code}\n#{res.body}" unless res.code == '200'
- debug("POST #{uri}: #{res.code} in #{age(start)}")
+ info("POST #{uri}: #{res.code} in #{age(start)}")
end
- def debug(msg)
- if @log.respond_to?(:debug)
- @log.debug(msg)
+ def info(msg)
+ if @log.respond_to?(:info)
+ @log.info(msg)
elsif @log.respond_to?(:puts)
@log.puts(msg)
end
end
end