Class: TxCatcher::Server
- Inherits:
-
Goliath::API
- Object
- Goliath::API
- TxCatcher::Server
- Defined in:
- lib/txcatcher/server.rb
Instance Method Summary collapse
- #address(path) ⇒ Object
- #broadcast_tx(txhex) ⇒ Object
- #feerate(blocks_target) ⇒ Object
- #response(env) ⇒ Object
- #route_for(path) ⇒ Object
- #tx(path) ⇒ Object
- #utxo(path) ⇒ Object
Instance Method Details
#address(path) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/txcatcher/server.rb', line 40 def address(path) path = path.sub(/\?.*/, '').split("/").delete_if { |i| i.empty? } addr = path.last address = Address.find_or_create(address: addr) unless address.deposits.empty? deposits = Deposit.where(address_id: address.id) deposits_count = deposits.count deposits = deposits.eager(:transaction).limit(params["limit"] || 100) transactions = deposits.map { |d| d.transaction } deposits = deposits.map do |d| t = d.transaction t.update(protected: true) unless t.protected t.update_block_height result = { txid: t.txid, amount: d.amount_in_btc, satoshis: d.amount, confirmations: t.confirmations, block_height: t.block_height, } result.merge!({ rbf: "yes", rbf_previous_txid: t.rbf_previous_transaction.txid }) if t.rbf? result end return [200, {}, { address: address.address, received: address.received, deposits_count: deposits_count, deposits_shown: deposits.size, deposits: deposits }.to_json] else return [200, {}, { address: addr, received: 0, deposits: [] }.to_json] end end |
#broadcast_tx(txhex) ⇒ Object
122 123 124 125 126 |
# File 'lib/txcatcher/server.rb', line 122 def broadcast_tx(txhex) TxCatcher.rpc_node.sendrawtransaction(txhex) tx = TxCatcher.rpc_node.decoderawtransaction(txhex) return [200, {}, tx.to_json] end |
#feerate(blocks_target) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/txcatcher/server.rb', line 128 def feerate(blocks_target) result = TxCatcher.rpc_node.estimatesmartfee(blocks_target.to_i) # This is for older versions of bitcoind/litecoind clients if result.to_s.include?("error") result = TxCatcher.rpc_node.estimatefee(blocks_target.to_i) result = { "feerate" => result, "blocks" => blocks_target} end return [200, {}, result["feerate"].to_s] end |
#response(env) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/txcatcher/server.rb', line 7 def response(env) uri = env["REQUEST_URI"] begin return route_for(uri) rescue BitcoinRPC::JSONRPCError => e LOGGER.report e.to_s + "\n" + e.backtrace.join("\n") return [400, {}, { error: e.data }.to_json] rescue Exception => e LOGGER.report e.to_s + "\n" + e.backtrace.join("\n") return [500, {}, { error: e.to_s }.to_json] end end |
#route_for(path) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/txcatcher/server.rb', line 20 def route_for(path) puts "[REQUEST: #{path}]" if path =~ /\A\/addr\/[0-9a-zA-Z]+\/utxo/ utxo(path) elsif path.start_with? "/addr/" address(path) elsif path.start_with? "/tx/send" broadcast_tx(params["rawtx"]) elsif path.start_with? "/tx/" tx(path) elsif path.start_with? "/feerate" feerate(params["blocks_target"] || 2) elsif path == "/" || path.empty? version = File.read(File.(File.dirname(__FILE__) + "../../../VERSION")) [200, {}, "TxCatcher server v#{version.strip}, #{TxCatcher::Config.rpcnode["name"]}, current_block_height: #{TxCatcher.current_block_height}"] else [404, {}, { error: "404, not found" }.to_json] end end |
#tx(path) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/txcatcher/server.rb', line 109 def tx(path) path = path.sub(/\?.*/, '').split("/").delete_if { |i| i.empty? } txid = path.last tx = Transaction.find_or_create_from_rpc(txid) if tx && (!tx.deposits.empty? || tx.rbf?) tx.update_block_height! if tx.block_height.nil? return [200, {}, tx.to_json] else return [404, {}, "Transaction not found"] end end |
#utxo(path) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/txcatcher/server.rb', line 70 def utxo(path) path = path.sub(/\?.*/, '').split("/").delete_if { |i| i.empty? } path.pop addr = path.last address = Address.find_or_create(address: addr) deposits = Deposit.where(address_id: address.id).limit(params["limit"] || 100).eager(:transaction) return [200, {}, "{}"] if address.deposits.empty? transactions = deposits.map { |d| d.transaction } transactions.sort! { |t1,t2| t2.created_at <=> t1.created_at } transactions.each do |t| # If we see a transaction with 0 confirmations, let's check if it got any news confirmations # by querying Bitcoind RPC. t.update_block_height_from_rpc if t.confirmations == 0 # If still not confirmed, let's make it protected so it's not deleted during cleanup. t.protected = true if t.confirmations == 0 end Transaction.update_all(transactions) # will only update the ones that changed! utxos = transactions.map do |t| outs = t.tx_hash["vout"].select { |out| out["scriptPubKey"]["addresses"] == [addr] } outs.map! do |out| out["confirmations"] = t.confirmations || 0 out["txid"] = t.txid if t.rbf? out["rbf"] = "yes" out["rbf_previous_txid"] = t.rbf_previous_transaction.txid end out end outs end.flatten return [200, {}, utxos.to_json] end |