module Bitbot::Common

Public Instance Methods

blockchain() click to toggle source

Returns a Blockchain API helper. Everyone uses the same one, but it doesn't keep any state so it's fine.

# File lib/bitbot/plugin/common.rb, line 43
def blockchain
  @@cached_blockchain ||=
    Bitbot::Blockchain.new(config['blockchain']['wallet_id'],
                           config['blockchain']['password1'],
                           config['blockchain']['password2'])
end
cached_addresses() click to toggle source
# File lib/bitbot/plugin/common.rb, line 8
def cached_addresses
  @cached_addresses
end
cached_addresses=(new) click to toggle source
# File lib/bitbot/plugin/common.rb, line 12
def cached_addresses=(new)
  @cached_addresses = new
end
db() click to toggle source

Returns a database handle for use in the current thread.

# File lib/bitbot/plugin/common.rb, line 34
def db
  Thread.current[:bitbot_db] ||=
    Bitbot::Database.new(File.join(config['data']['path'], "bitbot.db"))
end
exchange_rate(currency) click to toggle source
# File lib/bitbot/plugin/common.rb, line 16
def exchange_rate(currency)
  if @cached_exchange_rates
    return @cached_exchange_rates[currency]["15m"]
  end
  nil
end
exchange_rates=(new) click to toggle source
# File lib/bitbot/plugin/common.rb, line 23
def exchange_rates=(new)
  @cached_exchange_rates = new
end
satoshi_to_str(satoshi) click to toggle source

Some number formatting helpers

# File lib/bitbot/plugin/common.rb, line 73
def satoshi_to_str(satoshi)
  str = "฿%.8f" % (satoshi.to_f / 10**8)
  # strip trailing 0s
  str.gsub(/0*$/, '')
end
satoshi_to_usd(satoshi) click to toggle source
# File lib/bitbot/plugin/common.rb, line 79
def satoshi_to_usd(satoshi)
  rate = exchange_rate("USD")
  if rate
    "$%.2f" % (satoshi.to_f / 10**8 * rate)
  else
    "$?"
  end
end
satoshi_with_usd(satoshi) click to toggle source
# File lib/bitbot/plugin/common.rb, line 88
def satoshi_with_usd(satoshi)
  btc_str = satoshi_to_str(satoshi)
  if satoshi < 0
    btc_str = btc_str.irc(:red)
  else
    btc_str = btc_str.irc(:green)
  end

  usd_str = "[".irc(:grey) + satoshi_to_usd(satoshi).irc(:light_grey) + "]".irc(:grey)

  "#{btc_str} #{usd_str}"
end
str_to_satoshi(str) click to toggle source

Takes a string, and returns an int with number of satoshi. Eventually this could be smart enough to handle specified units too, rather than just assuming BTC every time.

# File lib/bitbot/plugin/common.rb, line 66
def str_to_satoshi(str)
  (str.to_f * 10**8).to_i
end
user_with_username(username) click to toggle source

Returns the User for the given username. If you want to get a User based on nick, User(nick) is easier.

# File lib/bitbot/plugin/common.rb, line 54
def user_with_username(username)
  bot.user_list.each do |user|
    return user if user.nick == username
  end
  nil
end
withdrawal_fee() click to toggle source
# File lib/bitbot/plugin/common.rb, line 27
def withdrawal_fee
  config['blockchain']['withdrawal_fee'] || 50000
end