forked from Carbonvote/carbonvote
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.rb
74 lines (60 loc) · 1.48 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
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_vote_amount
@yes_vote_amount ||= get_amount(:yes_contract).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
json({
yes_precentage: yes_precentage,
no_vote_amount: no_vote_amount,
yes_vote_amount: yes_vote_amount,
no_precentage: no_precentage
})
end
end