-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
88 lines (72 loc) · 1.96 KB
/
app.rb
1
2
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
87
88
require "bundler/setup"
require 'sinatra/base'
require "sinatra/reloader"
require "sinatra/config_file"
require "sinatra/json"
require 'redis'
class App < Sinatra::Base
configure :development do
register Sinatra::Reloader
end
register Sinatra::ConfigFile
config_file 'settings.yml'
helpers do
def redis
@redis ||= Redis.new(url: settings.redis_url)
end
def last_block
redis.get('processed-block-number') || 0
end
def get_amount(name)
key = settings.contract_addresses[name]
(redis.get("#{key}-amount") || 0).to_f
end
def yes_votes
@yes_votes ||= {
"0 ≤ reward < 1.5" => get_amount(:yes_contract_1),
"1.5 ≤ reward < 2" => get_amount(:yes_contract_2),
"2 ≤ reward < 3" => get_amount(:yes_contract_3),
"3 ≤ reward < 4" => get_amount(:yes_contract_4),
"reward ≥ 4" => get_amount(:yes_contract_5)
}
end
def yes_vote_amount
@yes_vote_amount ||= yes_votes.values.sum.round(4)
end
def no_vote_amount
@no_vote_amount ||= get_amount(:no_contract).round(4)
end
def precentage(n, base)
return 0.0 if base.zero?
(n.to_f / base.to_f * 100).round(4)
end
def total_amount
@total_amount ||= yes_vote_amount + no_vote_amount
end
def yes_precentage
precentage(yes_vote_amount, total_amount)
end
def no_precentage
precentage(no_vote_amount, total_amount)
end
end
get '/' do
erb :index, locals: {
settings: settings,
last_block: last_block,
no_vote_amount: no_vote_amount,
yes_vote_amount: yes_vote_amount
}
end
get '/vote' do
yes_drilldown = yes_votes.reduce([]) do |sum, i|
sum << [CGI.escape_html(i[0]), i[1], precentage(i[1], yes_vote_amount)]
end
json({
yes_precentage: yes_precentage,
yes_drilldown: yes_drilldown,
no_vote_amount: no_vote_amount,
no_precentage: no_precentage
})
end
end