Skip to content

Instantly share code, notes, and snippets.

@shideneyu
Created December 27, 2017 12:17
Show Gist options
  • Save shideneyu/ab1481f20e06cfc9b260888cd15b8789 to your computer and use it in GitHub Desktop.
Save shideneyu/ab1481f20e06cfc9b260888cd15b8789 to your computer and use it in GitHub Desktop.
require 'open-uri'
require 'json'
require 'pry'
require 'mysql2'
require 'active_record'
class Wallet < ActiveRecord::Base
end
class Account
attr_accessor :amount, :tx_id, :currency
def initialize(amount, tx_id, currency)
@amount = amount
@tx_id = tx_id
@currency = currency
end
end
def find_eth_amount(address)
eth_json = JSON.parse(open("http://api.etherscan.io/api?module=account&action=txlist&address=#{address}&startblock=0&endblock=99999999&sort=desc&page=1&offset=1").read) ; nil
return unless eth_json['result'].first
puts "Transaction found for #{address}"
amount_eth = eth_json['result'].first['value'].to_f / 1000000000000000000.to_f
tx_id = eth_json['result'].first['hash']
Account.new(amount_eth, tx_id, 'eth')
rescue OpenURI::HTTPError
puts 'Error'
sleep 1
retry
end
def find_btc_amount(address)
btc_json = JSON.parse(open("https://blockchain.info/address/#{address}?format=json").read) ; nil
return unless btc_json['txs'].first
puts "Transaction found for #{address}"
amount_btc = btc_json['total_received'].to_f / 100000000.to_f
tx_id = btc_json['txs'].first['hash']
Account.new(amount_btc, tx_id, 'btc')
rescue OpenURI::HTTPError
puts 'Error'
sleep 1
retry
end
def generate_file
data = Wallet.where('amount is not null').to_json(only: ['amount','currency','tx_id', 'created_at', 'rate'])
puts 'Generating new file...'
File.open("data.json","wb") do |f|
f.write(data.to_s)
end
end
def push
generate_file
puts 'Pushing to master...'
system('git commit -am "Updated data."')
system('git push origin master')
puts 'done '
end
def get_price(currency)
if currency == 'btc'
JSON.parse(open("https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=USD").read).first['price_usd']
else
JSON.parse(open("https://api.coinmarketcap.com/v1/ticker/ethereum/?convert=USD").read).first['price_usd']
end
end
def transaction_url(id, currency)
if currency == 'eth'
"<a href='https://etherscan.io/address/#{id}'>#{(id.to_s[0..5] + '...')}</a>"
else
"<a href='https://blockchain.info/address/#{id}'>#{(id.to_s[0..5] + '...')}</a>"
end
end
def fallback
Wallet.where('amount is not null and id <= ?', last_id*2).each do |wallet|
next if wallet.tx_id[0..3] == 'http'
puts wallet.id
transaction = find_eth_amount(wallet.eth_address) if wallet.eth_address
transaction = find_btc_amount(wallet.btc_address) if wallet.btc_address
wallet.tx_id = transaction.tx_id.to_s
wallet.save
end
puts 'ok'
end
def update_json
loop do
last_id = JSON.parse(open("https://liminality-network.herokuapp.com/users").read).last['id'] ; nil
if last_id.to_s != ''
Wallet.where('amount is null and id <= ?', last_id*2).each do |wallet|
puts wallet.id
transaction = find_eth_amount(wallet.eth_address) if wallet.eth_address
transaction = find_btc_amount(wallet.btc_address) if wallet.btc_address
if transaction && transaction.amount.to_f > 0.0
wallet.amount = transaction.amount.to_s
wallet.tx_id = transaction.tx_id.to_s
wallet.currency = transaction.currency
wallet.rate = get_price(wallet.currency).to_s
wallet.save
push
end
end
end
end
end
update_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment