-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbtc_chain.rb
executable file
·392 lines (314 loc) · 11.4 KB
/
btc_chain.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env ruby
#
# Ruby parser for Angelshares in the Bitcoin Blockchain.
# Usage: $ ruby btc_chain.rb [block=276970] [header=1]
#
# Donations accepted:
# - BTC 1Bzc7PatbRzXz6EAmvSuBuoWED96qy3zgc
# - PTS PcDLYukq5RtKyRCeC1Gv5VhAJh88ykzfka
#
# Copyright (C) 2014 donSchoe <[email protected]>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
require 'net/http'
require 'open-uri'
require 'json'
################################################################################
# BTC daemon connection
@connection = 'http://user:[email protected]:8332'
# Enable/Disable debugging output.
@debug = true
# Enable/Display daily summaries and output clean CSV only.
@clean_csv = true
################################################################################
require 'net/http'
require 'uri'
require 'json'
class BitcoinRPC
def initialize(service_url)
@uri = URI.parse(service_url)
end
def method_missing(name, *args)
post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
resp = JSON.parse( http_post_request(post_body) )
raise JSONRPCError, resp['error'] if resp['error']
resp['result']
end
def http_post_request(post_body)
http = Net::HTTP.new(@uri.host, @uri.port)
request = Net::HTTP::Post.new(@uri.request_uri)
request.basic_auth @uri.user, @uri.password
request.content_type = 'application/json'
request.body = post_body
http.request(request).body
end
class JSONRPCError < RuntimeError; end
end
@rpc = BitcoinRPC.new(@connection)
# gets block number (height) to start the script at
if ARGV[0].nil?
# default
blockstrt = 276970
else
# from args
blockstrt = ARGV[0].to_i
end
# initializes global args
@sum = 0.0
@ags = 0
@day = 1388620800
i=0
################################################################################
# script output start (CSV header)
$stdout.sync = true
$stderr.sync = true
if ARGV[1].nil?
# default
header = 1
else
# from args
header = ARGV[1].to_i
end
if header > 0
puts "\"BLOCK\";\"DATETIME\";\"TXBITS\";\"SENDER\";\"DONATION[BTC]\";\"DAYSUM[BTC]\";\"DAYRATE[AGS/BTC]\""
end
# parses given transactions
def parse_tx(hi=nil, time=nil, tx)
# block to catch huge transactions over size limit
begin
# gets transaction JSON data
jsontx = @rpc.getrawtransaction(tx, 1)
# check every transaction output
jsontx['vout'].each do |vout|
# gets recieving address and value
address = vout["scriptPubKey"]["addresses"]
value = vout["value"]
# checks addresses for being angelshares donation address
if not address.nil?
if address.include? '1ANGELwQwWxMmbdaSWhWLqBEtPTkWb8uDc'
# display daily summary and split CSV data in days
while (time.to_i > @day.to_i) do
# disable summary output for clean CSV files
if not @clean_csv
puts "+++++ Day Total: #{@sum.round(8)} BTC (#{@ags.round(8)} AGS/BTC) +++++"
puts ""
puts "+++++ New Day : #{Time.at(@day.to_i).utc} +++++"
puts "\"BLOCK\";\"DATETIME\";\"TXBITS\";\"SENDER\";\"DONATION[BTC]\";\"DAYSUM[BTC]\";\"DAYRATE[AGS/BTC]\""
end
# reset BTC sum and sitch day
@sum = 0.0
@day += 86400
end
# gets UTC timestamp
stamp = Time.at(time.to_i).utc
# checks each input for sender addresses
senderhash = Hash.new
jsontx['vin'].each do |vin|
# parses the sender from input txid and n
sendertx = vin['txid']
sendernn = vin['vout']
# block to catch huge transactions over size limit
begin
# gets transaction JSON data
senderjsontx = @rpc.getrawtransaction(sendertx, 1)
# scan sender transaction for sender address
senderjsontx['vout'].each do |sendervout|
if sendervout['n'].eql? sendernn
# gets angelshares sender address and input value
if senderhash[sendervout['scriptPubKey']['addresses'].first.to_s].nil?
senderhash[sendervout['scriptPubKey']['addresses'].first.to_s] = sendervout['value'].to_f
else
senderhash[sendervout['scriptPubKey']['addresses'].first.to_s] += sendervout['value'].to_f
end
end
end
# catches transactions which are too big to parse and uses web API
rescue Errno::E2BIG
if @debug
$stderr.puts "!!!WARNG TX TOO BIG TO PARSE #{sendertx}"
end
# uses blockchain info to get tx JSON data
senderjsontx = open("https://blockchain.info/tx/#{sendertx}?format=json").read
senderjsontx = JSON.parse(senderjsontx)
# scan sender transaction for sender address
senderjsontx['out'].each do |sendervout|
if sendervout['n'].eql? sendernn
# gets angelshares sender address and input value
if senderhash[sendervout['addr'].to_s].nil?
senderhash[sendervout['addr'].to_s] = (sendervout['value'].to_f / 100000000.0)
else
senderhash[sendervout['addr'].to_s] += (sendervout['value'].to_f / 100000000.0)
end
end
end
end
end
# gets donation value by each input address of the transaction
outval = value
presum = 0.0
sumval = 0.0
senderhash.each do |key, inval|
printval = 0.0
sumval += inval
if sumval <= outval
printval = inval
else
printval = outval - presum
end
# prints donation stats if input value is above 0
if printval > 0
# sums up donated BTC value
@sum += printval
# calculates current angelshares ratio
@ags = 5000.0 / @sum
txbits = tx
puts "\"" + hi.to_s + "\";\"" + stamp.to_s + "\";\"" + txbits.to_s + "\";\"" + key.to_s + "\";\"" + printval.round(8).to_s + "\";\"" + @sum.round(8).to_s + "\";\"" + @ags.round(8).to_s + "\""
end
presum += inval
end
end
else
# debugging warning: transaction without output address
if @debug
$stderr.puts "!!!WARNG ADDRESS EMPTY #{vout.to_s}"
end
end
end
# catches transactions which are too big to parse and uses web API
rescue Errno::E2BIG
if @debug
$stderr.puts "!!!WARNG TX TOO BIG TO PARSE #{tx}"
end
# uses blockchain info to get tx JSON data
jsontx = open("https://blockchain.info/tx/#{tx}?format=json").read
jsontx = JSON.parse(jsontx)
# check every transaction output
jsontx['out'].each do |vout|
# gets recieving address and value
address = vout["addr"]
value = vout["value"]
# checks addresses for being angelshares donation address
if not address.nil?
if address.include? '1ANGELwQwWxMmbdaSWhWLqBEtPTkWb8uDc'
# display daily summary and split CSV data in days
while (time.to_i > @day.to_i) do
# disable summary output for clean CSV files
if not @clean_csv
puts "+++++ Day Total: #{@sum.round(8)} BTC (#{@ags.round(8)} AGS/BTC) +++++"
puts ""
puts "+++++ New Day : #{Time.at(@day.to_i).utc} +++++"
puts "\"BLOCK\";\"DATETIME\";\"TXBITS\";\"SENDER\";\"DONATION[BTC]\";\"DAYSUM[BTC]\";\"DAYRATE[AGS/BTC]\""
end
# reset BTC sum and sitch day
@sum = 0.0
@day += 86400
end
# gets UTC timestamp
stamp = Time.at(time.to_i).utc
# checks each input for sender addresses
senderhash = Hash.new
jsontx['inputs'].each do |vin|
# parses the sender from input
senderadr = vin['addr']
senderval = vin['value']
# gets angelshares sender address and input value
if senderhash[sendervout[senderadr].first.to_s].nil?
senderhash[sendervout[senderadr].first.to_s] = (senderval.to_f / 100000000.0)
else
senderhash[sendervout[senderadr].first.to_s] += (senderval.to_f / 100000000.0)
end
end
# gets donation value by each input address of the transaction
outval = value
presum = 0.0
sumval = 0.0
senderhash.each do |key, inval|
printval = 0.0
sumval += inval
if sumval <= outval
printval = inval
else
printval = outval - presum
end
# prints donation stats if input value is above 0
if printval > 0
# sums up donated BTC value
@sum += printval
# calculates current angelshares ratio
@ags = 5000.0 / @sum
txbits = tx[0..8]
puts "\"" + hi.to_s + "\";\"" + stamp.to_s + "\";\"" + txbits.to_s + "\";\"" + key.to_s + "\";\"" + printval.round(8).to_s + "\";\"" + @sum.round(8).to_s + "\";\"" + @ags.round(8).to_s + "\""
end
presum += inval
end
end
else
# debugging warning: transaction without output address
if @debug
$stderr.puts "!!!WARNG ADDRESS EMPTY #{vout.to_s}"
end
end
end
end
end
# starts parsing the blockchain in infinite loop
while true do
# debugging output: loop number & start block height
if @debug
$stderr.puts "---DEBUG LOOP #{i}"
$stderr.puts "---DEBUG BLOCK #{blockstrt}"
end
# gets current block height
blockhigh = @rpc.getblockcount
#reads every block by block
(blockstrt.to_i..blockhigh.to_i).each do |hi|
if @debug
$stderr.puts "---DEBUG BLOCK #{hi}"
end
# gets block hash string
blockhash = @rpc.getblockhash(hi)
# gets JSON block data
blockinfo = @rpc.getblock(blockhash)
# gets block transactions & time
transactions = blockinfo['tx']
time = blockinfo['time']
# parses transactions ...
if not transactions.nil?
if not transactions.size <= 1
transactions.each do |tx|
# ... one by one
parse_tx(hi, time, tx)
end
else
# ... only one available
parse_tx(hi, time, transactions.first)
end
else
# debugging warning: block without transactions
if @debug
$stderr.puts "!!!WARNG NO TRANSACTIONS IN BLOCK #{hi}"
end
end
end
# debugging output: current loop summary
if @debug
$stderr.puts "---DEBUG SUM #{@sum.round(8)}"
$stderr.puts "---DEBUG VALUE #{@ags.round(8)}"
end
# resets starting block height to next unparsed block
blockstrt = blockhigh.to_i + 1
i += 1
# wait for new blocks to appear
sleep(600)
end