-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats_api_thread.rb
More file actions
141 lines (127 loc) · 5.26 KB
/
stats_api_thread.rb
File metadata and controls
141 lines (127 loc) · 5.26 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require "digest/sha3"
class StatsApiThread < Thread
attr_reader :stats
CONTRACT_ADDR = "0x3A9FfF453d50D4Ac52A6890647b823379ba36B9E"
DEFAULT_STATS = {}
def initialize(every:, on_change: nil, parity:)
@on_change = on_change
@parity = parity
@stats = DEFAULT_STATS
super(every) do |every|
loop do
begin
work
rescue => e
puts "ERROR", e, e.backtrace
ensure
sleep every
end
end
end
rescue => e
puts "ERROR", e, e.backtrace
end
private
def on_change(*val)
@on_change.call(*val) if @on_change
end
# def hashrate(mining_target, spr)
# hashrate = 2**256 / mining_target / spr
# end
def work
puts "updating stats"
s = @parity.batch_call({
currentEthBlock: ["eth_blockNumber", nil],
name: "name()",
symbol: "symbol()",
decimals: "decimals()",
circulatingSupply: "totalSupply()",
heapTop: "heapTop()",
heapSize: "heapSize()",
# heapWinner: "Winner()",
# heapWinner2: "_winner()",
# tokensMinted: "tokensMinted()"
# ldps: "latestDifficultyPeriodStarted()",
# miningTarget: "miningTarget()",
# challengeNumber: "challengeNumber()",
# rewardEra: "rewardEra()",
# maxSupplyForEra: "maxSupplyForEra()",
tokensBurned: "tokensBurned()"
# circulatingSupply: "tokensMinted()",
# lastRewardTo: "lastRewardTo()",
# lastRewardAmount: "lastRewardAmount()",
# lrebn: "lastRewardEthBlockNumber()",
# epochCount: "epochCount()",
# maximumTarget: "_MAXIMUM_TARGET()",
# minimumTarget: "_MINIMUM_TARGET()",
# bpr: "_BLOCKS_PER_READJUSTMENT()"
}, contract_addr: CONTRACT_ADDR)
# difficulty = s[:maximumTarget] / s[:miningTarget]
# rsr = s[:epochCount] % 1024
# ebsldp = s[:currentEthBlock] - s[:ldps]
# ssr = ebsldp * 15.0
# spr = rsr > 0 ? ssr / rsr : 600
# hr = hashrate(s[:miningTarget], spr)
dec_units = 10**s[:decimals]
heapTopAddress = s[:heapTop][0,42]
heapTopAmount = s[:heapTop][42..-1].to_i(16)
initialSupply = 1010101
# winnerAddress = s[:heapWinner][0,42]
# winnerAmount = s[:heapWinner][42..-1].to_i(16)
# winnerAddress2 = s[:heapWinner2][0,42]
# winnerAmount2 = s[:heapWinner2][42..-1].to_i(16)
# latestWinner = s[:heapWinner][42..-1].to_i(16)
stats = {
apiVersion: "5.12",
name: s[:name],
symbol: s[:symbol],
contractUrl: "https://etherscan.io/address/#{CONTRACT_ADDR}",
contractAddress: CONTRACT_ADDR,
decimals: dec_units,
initialSupply: initialSupply,
circulatingSupply: s[:circulatingSupply],
circulatingSupplyReadable: s[:circulatingSupply] / dec_units.to_f,
tokensBurned: initialSupply - s[:circulatingSupply] / dec_units.to_f,
heapSize: s[:heapSize],
heapTopAddress: heapTopAddress,
heapTopAmount: heapTopAmount,
heapTopAmountReadable: heapTopAmount / dec_units.to_f,
# winnerAddress: winnerAddress,
# winnerAmount: winnerAmount,
# winnerAddress2: winnerAddress2,
# winnerAmount2: winnerAmount2,
# heapWinner: s[:heapWinner].to_s,
currentEthBlock: s[:currentEthBlock]
# difficulty: difficulty
# minimumTarget: s[:minimumTarget].to_s,
# maximumTarget: s[:maximumTarget].to_s,
# miningTarget: s[:miningTarget].to_s,
# challengeNumber: s[:challengeNumber],
# rewardEra: s[:rewardEra].to_s,
# maxSupplyForEra: s[:maxSupplyForEra],
# blocksPerReadjustment: s[:bpr],
# latestDifficultyPeriodStarted: s[:ldps],
# circulatingSupply: (s[:tokensMinted] / dec_units.to_f).to_i,
# lastRewardTo: "0x%040x" % s[:lastRewardTo].to_i(16),
# lastRewardAmount: (s[:lastRewardAmount] / dec_units.to_f).to_f,
# lastRewardEthBlockNumber: s[:lrebn],
# ethBlocksSinceLastDifficultyPeriod: ebsldp,
# secondsPerWinner: spw
# hashrateEstimate: hr,
# hashrateEstimateDescription: "%0.2f GH/s" % (hr * 1e-9),
# rewardsSinceReadjustment: rsr
}
changed = stats != @stats
@stats = stats # atomic replace
on_change @stats if changed
puts "done updating stats"
end
end
if $0 == __FILE__
# for testing
require "./parity_rpc"
config = YAML::load_file("config.yml")
parity = ParityRPC.new(url: config["provider"])
sat = StatsApiThread.new(parity: parity, every: 10, on_change: Proc.new { puts sat.stats })
sleep
end