Created
April 14, 2018 13:46
-
-
Save justindavies/d24e8ab1e1b8fd69cfc4b923ba17a4e2 to your computer and use it in GitHub Desktop.
Import Ethereum transactions using Ruby
This file contains hidden or 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
#!/usr/local/bin/ruby | |
# Used to export Blocks from a Web3 compatible endpoint | |
# I use: | |
# geth --rpc --rpccorsdomain http://localhost:8080 --fast | |
# Need to add a unique index for tx | |
# db.transactions.createIndex({"hash":1},{unique:1}) | |
require 'web3/eth' | |
require 'mongoid' | |
force_sync = ARGV[0] | |
# Lazy initialise the Transaction class | |
class Transaction | |
include Mongoid::Document | |
include Mongoid::Attributes::Dynamic | |
end | |
Mongoid.load!("./mongoid.yml", :development) | |
web3 = Web3::Eth::Rpc.new # host: "parity-service" | |
begin | |
syncing = web3.eth.syncing | |
last_block = syncing["currentBlock"].to_i(16) | |
rescue => exception | |
last_block = web3.eth.blockNumber | |
end | |
puts(last_block) | |
all_transactions = [] | |
until last_block == 0 | |
begin | |
block = web3.eth.getBlockByNumber(last_block) | |
transactions = [] | |
for tx in block.transactions | |
#if tx.value_eth > 0 | |
transactions << {:hash => tx.hash, :block_number => last_block, :block_time => block.timestamp_time, :from => tx.from, :to => tx.to, :value => tx.value_eth, :data => tx.input} | |
#Block.create({:hash => tx.hash, :block_number => last_block, :block_time => block.timestamp_time, :from => tx.from, :to => tx.to, :value => tx.value_eth, :data => tx.input}) | |
#end | |
end | |
rescue => exception | |
puts(exception) | |
last_block-=1 | |
next | |
end | |
begin | |
last_block-=1 | |
Transaction.collection.insert_many(transactions) | |
puts("Block #{last_block} at #{block.timestamp_time}") | |
rescue Mongo::Error::BulkWriteError => exception | |
if force_sync.nil? | |
puts("Hitting existing block...retrying") | |
sleep(1) | |
begin | |
syncing = web3.eth.syncing | |
last_block = syncing["currentBlock"].to_i(16) | |
puts(last_block) | |
rescue => exception | |
last_block = web3.eth.blockNumber | |
puts(last_block) | |
end | |
else | |
puts "Skipping #{last_block+1}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment