Module: Bitbot::Common

Included in:
Plugin
Defined in:
lib/bitbot/plugin/common.rb

Instance Method Summary collapse

Instance Method Details

#blockchainObject

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



43
44
45
46
47
48
# 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_addressesObject



8
9
10
# File 'lib/bitbot/plugin/common.rb', line 8

def cached_addresses
  @cached_addresses
end

#cached_addresses=(new) ⇒ Object



12
13
14
# File 'lib/bitbot/plugin/common.rb', line 12

def cached_addresses=(new)
  @cached_addresses = new
end

#dbObject

Returns a database handle for use in the current thread.



34
35
36
37
# 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) ⇒ Object



16
17
18
19
20
21
# 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) ⇒ Object



23
24
25
# File 'lib/bitbot/plugin/common.rb', line 23

def exchange_rates=(new)
  @cached_exchange_rates = new
end

#satoshi_to_str(satoshi) ⇒ Object

Some number formatting helpers



73
74
75
76
77
# 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) ⇒ Object



79
80
81
82
83
84
85
86
# 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) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# 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(:blue) + "]".irc(:grey)

  "#{btc_str} #{usd_str}"
end

#str_to_satoshi(str) ⇒ Object

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.



66
67
68
# 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) ⇒ Object

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



54
55
56
57
58
59
# File 'lib/bitbot/plugin/common.rb', line 54

def user_with_username(username)
  bot.user_list.each do |user|
    return user if user.user == username
  end
  nil
end

#withdrawal_feeObject



27
28
29
# File 'lib/bitbot/plugin/common.rb', line 27

def withdrawal_fee
  config['blockchain']['withdrawal_fee'] || 50000
end