Class: Capwatch::Calculator

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

Class Method Summary collapse

Class Method Details

.fund_hash(fund, coinmarketcap_json) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/capwatch.rb', line 19

def self.fund_hash(fund, coinmarketcap_json)
  table = []

  title = fund['name']
  symbols = fund['symbols']
  fund_keys = symbols.keys

  price_eth_btc = coinmarketcap_json.find do |x|
    x['symbol'] == 'ETH'
  end['price_usd'].to_f

  filtered_response_json = coinmarketcap_json.select do |x|
    fund_keys.include?(x['symbol'])
  end

  total_value_usd = filtered_response_json.inject(0) do |sum, n|
    sum + symbols[n['symbol']] * n['price_usd'].to_f
  end

  total_value_btc = filtered_response_json.inject(0) do |sum, n|
    sum + symbols[n['symbol']] * n['price_btc'].to_f
  end

  total_value_eth = filtered_response_json.inject(0) do |sum, n|
    sum + symbols[n['symbol']] * n['price_usd'].to_f / price_eth_btc
  end

  distribution_hash = {}

  fund_keys.each do |x|
    x = filtered_response_json.find { |e| e['symbol'] == x }
    symbol = x['symbol']
    asset_name = "#{x['name']} (#{symbol})"
    quant_value = symbols[symbol]
    price = x['price_usd'].to_f
    value_btc = quant_value * x['price_btc'].to_f
    value_eth = quant_value * x['price_usd'].to_f / price_eth_btc
    value_usd = quant_value * x['price_usd'].to_f
    distribution_float = value_usd / total_value_usd
    distribution_hash[symbol] = distribution_float
    distribution = distribution_float * 100
    percent_change_1h = x['percent_change_1h'].to_f || 0
    percent_change_24h = x['percent_change_24h'].to_f || 0
    percent_change_7d = x['percent_change_7d'].to_f || 0
    table << [
      asset_name,
      quant_value,
      price,
      value_usd,
      value_btc,
      value_eth,
      distribution,
      percent_change_1h,
      percent_change_24h,
      percent_change_7d
    ]
  end

  a_1h = filtered_response_json.inject(0) do |sum, n|
    sum + n['percent_change_1h'].to_f * distribution_hash[n['symbol']].to_f
  end

  a_24h = filtered_response_json.inject(0) do |sum, n|
    sum + n['percent_change_24h'].to_f * distribution_hash[n['symbol']].to_f
  end

  a_7d = filtered_response_json.inject(0) do |sum, n|
    sum + n['percent_change_7d'].to_f * distribution_hash[n['symbol']].to_f
  end

  footer = [
    '',
    '',
    '',
    total_value_usd,
    total_value_btc,
    total_value_eth,
    '',
    a_1h,
    a_24h,
    a_7d
  ]

  table.sort_by! { |a| -a[6].to_f } # DIST (%)

  {}
    .merge(title: title)
    .merge(table: table)
    .merge(footer: footer)
end