Created
November 21, 2013 18:11
-
-
Save hpyhacking/7586626 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
class TempTask | |
def self.get_shareholders | |
ipo_address = '1LDipoYVWA7UsFzWrrcXUGLnsXWuYaJB9f' | |
raw = get_raw(ipo_address) | |
ipo_result = JSON.parse(raw) | |
txs = ipo_result["txs"].reverse | |
for i in txs | |
out_addrs = [] | |
inputs_addrs = [] | |
out = [] | |
inputs = [] | |
out_value = 0 | |
inputs_value = 0 | |
for j in i["out"] | |
if out.select{|t| t[:key]==j["addr"]}.empty? | |
out << {:key => j["addr"],:value => j["value"]} | |
else | |
out.select{|t| t[:key]==j["addr"]}[0][:value] += j["value"] | |
end | |
out_value += j["value"] | |
end | |
if i["inputs"].length > 1 | |
tx = get_tx(i["hash"]) | |
p "get tx hash #{i["hash"]} and wait 0.5s" | |
sleep 1 | |
for j in tx["inputs"] | |
if inputs.select{|t| t[:key] == j["prev_out"]["addr"]}.empty? | |
inputs << {:key => j["prev_out"]["addr"],:value => j["prev_out"]["value"]} | |
else | |
inputs.select{|t| t[:key] == j["prev_out"]["addr"]}[0][:value] += j["prev_out"]["value"] | |
end | |
inputs_value += j["prev_out"]["value"] | |
end | |
elsif i["inputs"].length == 1 | |
inputs << {:key => i["inputs"][0]["prev_out"]["addr"],:value => i["inputs"][0]["prev_out"]["value"]} | |
end | |
out.each{|t| out_addrs << t[:key]} | |
inputs.each{|t| inputs_addrs << t[:key]} | |
if (ipo_address.in? out_addrs) && (not(ipo_address.in? inputs_addrs)) | |
address = inputs_addrs[0] | |
value = out.select{|t| t[:key] == ipo_address}[0][:value] | |
if record = Shareholder.find_by_btc_address(address) | |
record.share_count += value/100000000.0 | |
record.last_modify_time = Time.now.utc | |
record.save() | |
else | |
Shareholder.create({:shareholder_id => (Shareholder.maximum(:shareholder_id) || 0)+1, | |
:btc_address => address, | |
:share_count => value/100000000.0, | |
:status => 200, | |
:creation_time => Time.at(i["time"]), | |
:last_modify_time => Time.now.utc | |
}) | |
end | |
end | |
end | |
end | |
protected | |
def self.get_tx(tx_hash) | |
require 'open-uri' | |
tx_result = JSON.parse open("http://blockchain.info/tx/#{tx_hash}?format=json").read | |
end | |
def self.get_raw(address) | |
require 'open-uri' | |
address_result = JSON.parse open("http://blockchain.info/address/#{address}?format=json").read | |
address_txs = address_result["txs"] | |
temp_length = address_result["txs"].length | |
offset = temp_length | |
while temp_length >=50 | |
temp_txs = (JSON.parse open("http://blockchain.info/address/#{address}?format=json&offset=#{offset}").read)["txs"] | |
sleep 1 | |
address_txs += temp_txs | |
temp_length = temp_txs.length | |
offset += temp_length | |
end | |
address_result["txs"] = address_txs | |
return address_result.to_json | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment