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
- #response(env) ⇒ Object
- #route_for(path) ⇒ Object
- #utxo(path) ⇒ Object
Instance Method Details
#address(path) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/txcatcher/server.rb', line 27 def address(path) path = path.split("/").delete_if { |i| i.empty? } addr = path.last address = Address.where(address: addr).eager(deposits: :transactions).first if address transactions_ids = address.deposits.map { |d| d.transaction.txid } deposits = address.deposits.map do |d| t = d.transaction t.update(protected: true) unless t.protected t.update_block_height! { txid: t.txid, amount: d.amount_in_btc, satoshis: d.amount, confirmations: t.confirmations, block_height: t.block_height } end [200, {}, { address: address.address, received: address.received, deposits: deposits }.to_json] else [200, {}, { address: addr, received: 0, deposits: [] }.to_json] end end |
#broadcast_tx(txhex) ⇒ Object
75 76 77 78 79 |
# File 'lib/txcatcher/server.rb', line 75 def broadcast_tx(txhex) TxCatcher.rpc_node.sendrawtransaction(txhex) tx = TxCatcher.rpc_node.decoderawtransaction(txhex) [200, {}, tx.to_json] end |
#response(env) ⇒ Object
7 8 9 10 |
# File 'lib/txcatcher/server.rb', line 7 def response(env) uri = env["REQUEST_URI"] return route_for(uri) end |
#route_for(path) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/txcatcher/server.rb', line 12 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 == "/" || path.empty? [200, {}, "TxCatcher server, #{TxCatcher::Config.rpcnode["name"]}, current_block_height: #{TxCatcher.current_block_height}"] else [404, {}, { error: "404, not found" }.to_json] end end |
#utxo(path) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/txcatcher/server.rb', line 52 def utxo(path) path = path.split("/").delete_if { |i| i.empty? } path.pop addr = path.last model = Address.where(address: addr).eager(deposits: :transactions).first return [200, {}, "{}"] unless model transactions = model.deposits.map { |d| d.transaction } utxos = transactions.map do |t| outs = t.tx_hash["vout"].select { |out| out["scriptPubKey"]["addresses"] == [addr] } outs.map! do |out| t.update(protected: true) unless t.protected t.update_block_height! out["confirmations"] = t.confirmations || 0 out["txid"] = t.txid out end outs end.flatten [200, {}, utxos.to_json] end |