3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
|
# File 'lib/capwatch/calculator.rb', line 3
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_24h = x['percent_change_24h'].to_f || 0
percent_change_7d = x['percent_change_7d'].to_f || 0
table << [
asset_name,
price,
quant_value,
value_usd,
value_btc,
value_eth,
distribution,
percent_change_24h,
percent_change_7d
]
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
= [
'',
'',
'',
total_value_usd,
total_value_btc,
total_value_eth,
'',
a_24h,
a_7d
]
table.sort_by! { |e| -e[3].to_f }
table.each.with_index(1) { |e, i| e[0] = "#{i}. #{e[0]}" }
{}
.merge(title: title)
.merge(table: table)
.merge(footer: )
end
|