Class: Capwatch::Fund

Inherits:
Object
  • Object
show all
Defined in:
lib/capwatch/fund.rb,
lib/capwatch/fund/config.rb

Defined Under Namespace

Classes: Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider:, config:) ⇒ Fund

Returns a new instance of Fund.



7
8
9
10
11
12
13
# File 'lib/capwatch/fund.rb', line 7

def initialize(provider:, config:)
  @provider = provider
  @config = config
  @positions = config.positions
  @coins = config.coins
  build
end

Instance Attribute Details

#coinsObject

Returns the value of attribute coins.



5
6
7
# File 'lib/capwatch/fund.rb', line 5

def coins
  @coins
end

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/capwatch/fund.rb', line 5

def config
  @config
end

#positionsObject

Returns the value of attribute positions.



5
6
7
# File 'lib/capwatch/fund.rb', line 5

def positions
  @positions
end

#providerObject

Returns the value of attribute provider.



5
6
7
# File 'lib/capwatch/fund.rb', line 5

def provider
  @provider
end

Instance Method Details

#[](symbol) ⇒ Object



15
16
17
# File 'lib/capwatch/fund.rb', line 15

def [](symbol)
  coins.find { |coin| coin.symbol == symbol }
end

#assign_pricesObject



55
56
57
58
59
# File 'lib/capwatch/fund.rb', line 55

def assign_prices
  coins.each do |coin|
    provider.update_coin(coin)
  end
end

#assign_quantityObject



49
50
51
52
53
# File 'lib/capwatch/fund.rb', line 49

def assign_quantity
  coins.each do |coin|
    coin.quantity = positions[coin.symbol]
  end
end

#buildObject



43
44
45
46
47
# File 'lib/capwatch/fund.rb', line 43

def build
  assign_quantity
  assign_prices
  distribution
end

#console_tableObject



81
82
83
# File 'lib/capwatch/fund.rb', line 81

def console_table
  Console.new(name = config.name, body = serialize, totals = fund_totals).draw_table
end

#distributionObject



61
62
63
64
65
# File 'lib/capwatch/fund.rb', line 61

def distribution
  coins.each do |coin|
    coin.distribution = coin.value_btc / value_btc
  end
end

#fund_totalsObject



71
72
73
74
75
76
77
78
79
# File 'lib/capwatch/fund.rb', line 71

def fund_totals
  {
    value_usd: value_usd,
    value_btc: value_btc,
    value_eth: value_eth,
    percent_change_24h: percent_change_24h,
    percent_change_7d: percent_change_7d
  }
end

#percent_change_1hObject



31
32
33
# File 'lib/capwatch/fund.rb', line 31

def percent_change_1h
  coins.map { |coin| coin.percent_change_1h * coin.distribution }.sum
end

#percent_change_24hObject



35
36
37
# File 'lib/capwatch/fund.rb', line 35

def percent_change_24h
  coins.map { |coin| coin.percent_change_24h * coin.distribution }.sum
end

#percent_change_7dObject



39
40
41
# File 'lib/capwatch/fund.rb', line 39

def percent_change_7d
  coins.map { |coin| coin.percent_change_7d * coin.distribution }.sum
end

#serializeObject



67
68
69
# File 'lib/capwatch/fund.rb', line 67

def serialize
  coins.map { |coin| coin.serialize }.to_json
end

#value_btcObject



19
20
21
# File 'lib/capwatch/fund.rb', line 19

def value_btc
  coins.map(&:value_btc).sum
end

#value_ethObject



27
28
29
# File 'lib/capwatch/fund.rb', line 27

def value_eth
  coins.map(&:value_eth).sum
end

#value_usdObject



23
24
25
# File 'lib/capwatch/fund.rb', line 23

def value_usd
  coins.map(&:value_usd).sum
end