Confirmations

如何使用 Blockchain.info DATA API 確定交易的確認次數及其費用?

  • January 29, 2017

我如何使用BlockChain.info DATA API知道任何給定的交易是否有確認並支付了礦工費?

如果從http://blockchain.info/rawtx/$tx_hash端點返回的交易有確認,它將有一個block_height成員。然後,您可以通過從http://blockchain.info/latestblock端點檢索到的最新高度減去該值來粗略計算其確認次數。

程式碼範例,在 Ruby 中:

#!/usr/bin/env ruby
require "open-uri"
require "json"
# call this script with `ruby block_height.rb <tx_hash>`
tx = ARGV.shift
puts "Getting info for #{tx}..."
j = JSON.parse open("http://blockchain.info/rawtx/#{tx}").read
if j["block_height"]
 b = JSON.parse open("http://blockchain.info/latestblock").read
 puts "%d confirmations" % (b["height"] - j["block_height"] + 1)
else
 time_since = Time.now.gmtime.to_i - j["time"]
 puts "It's been #{time_since} seconds since the transaction was created."
 puts "It's not been ten minutes yet!" if time_since < 600
 puts "It's due any time now." if time_since >= 600
end

計算交易費用最好通過將交易的輸入和輸出相加,然後從輸出中減去輸入來完成。不同的是交易費用。

引用自:https://bitcoin.stackexchange.com/questions/7328