Top Level Namespace

Defined Under Namespace

Modules: TxCatcher Classes: BitcoinRPC, Hash

Constant Summary collapse

LOGGER =
TxCatcher::Logger.new(log_file: "txcatcher_monitor.log", error_file: "txcatcher_monitor_errors.log", error_log_delimiter: "")

Constants included from TxCatcher::Initializer

TxCatcher::Initializer::GEM_ROOT, TxCatcher::Initializer::MIGRATIONS_ROOT

Instance Method Summary collapse

Methods included from TxCatcher::Initializer

#add_route, #connect_to_db, #connect_to_rpc_node, #create_config_files, #initialize_sentry, #migrations_pending?, #prepare, #read_config_file, #run_migrations, #set_goliath_args

Instance Method Details

#fetch_txcatcher_for_addr(addr) ⇒ Object



112
113
114
115
116
117
118
119
# File 'bin/txcatcher-monitor', line 112

def fetch_txcatcher_for_addr(addr)
  response = Faraday.get do |req|
    req.url TxCatcher::Config["monitor"]["txcatcher_url"] + "/addr/#{addr}/utxo"
    req.options.timeout = 5
    req.options.open_timeout = 5
  end
  JSON.parse(response.body)
end

#fetch_txcatcher_response(addr, sleep_time: 3) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'bin/txcatcher-monitor', line 102

def fetch_txcatcher_response(addr, sleep_time: 3)
  response = {}
  3.times do
    sleep sleep_time # let's wait, perhaps txcatcher hasn't caught up
    response = fetch_txcatcher_for_addr(addr)
    break unless response.empty?
  end
  return response
end

#latest_output_addrObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'bin/txcatcher-monitor', line 79

def latest_output_addr

  response = Faraday.get do |req|
    req.url TxCatcher::Config["monitor"]["blockchain_source_url"]
    req.options.timeout = 5
    req.options.open_timeout = 5
  end
  txs = JSON.parse(response.body)

  txs.each do |tx|
    if tx["outputs"] && tx["outputs"].first["addresses"]
      tx["outputs"].first["addresses"].each do |addr|
        # Blockcypher returns old Litecoin P2SH addresses which start with 3, which
        # txcatcher doesn't support. Let's just ignore them.
        if addr[0] != "3" || !TxCatcher::Config["monitor"]["ignore_3_litecoin_addr"]
          return addr
        end
      end
    end
  end

end

#send_alert(subject, text) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'bin/txcatcher-monitor', line 50

def send_alert(subject, text)

  return unless TxCatcher::Config["monitor"]["alert_mail"]

  # Do not send emails more often than once in 1 hour
  email_time_file = TxCatcher::Config.config_dir + "/txcatcher_monitor_last_email_time.txt"
  t = Time.parse(File.read(email_time_file)) if File.exists?(email_time_file)
  return if t && t > Time.now - 3600

  ses_config = TxCatcher::Config["monitor"]["aws_ses"]
  ses = Aws::SES::Client.new(
    region: ses_config["region"],
    access_key_id: ses_config["access_key"],
    secret_access_key: ses_config["secret_access_key"]
  )

  resp = ses.send_email({
    destination: { to_addresses: TxCatcher::Config["monitor"]["alert_mail"]["to"] },
    source: TxCatcher::Config["monitor"]["alert_mail"]["from"],
    message: {
      body: { text: { charset: "utf-8", data: text }},
      subject: { charset: "utf-8", data: subject   }
    }
  })

  File.open(email_time_file, 'w') { |f| f.write Time.now.to_s }

end