Skip to content

Commit

Permalink
DigixGlobal#70 ethereum_api instead of web3, updated scheduler code
Browse files Browse the repository at this point in the history
  • Loading branch information
bshevchenko committed Sep 4, 2019
1 parent a5af0f2 commit d74ad40
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,5 @@ gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

gem 'eth'
gem 'newrelic_rpm'
gem 'web3-eth'

gem 'sanitize', '~> 5.0'
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,6 @@ GEM
timeliness (>= 0.3.10, < 1)
warden (1.2.8)
rack (>= 2.0.6)
web3-eth (0.2.30)
digest-sha3 (~> 1.1.0)
rlp (~> 0.7.3)
webmock (3.6.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
Expand Down Expand Up @@ -377,7 +374,6 @@ DEPENDENCIES
typhoeus (>= 1.3.1)
tzinfo-data
validates_timeliness (~> 5.0.0.alpha3)
web3-eth
webmock (>= 3.4.2)

RUBY VERSION
Expand Down
29 changes: 14 additions & 15 deletions app/models/watching_transaction.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'ethereum_api'

class WatchingTransaction < ApplicationRecord
before_create :set_uuid

Expand Down Expand Up @@ -49,29 +51,26 @@ def watch(user, transaction_object, signed_transaction, group_id)
end

def resend
web3 = Web3::Eth::Rpc.new host: ENV.fetch('INFURA_SERVER_URL') { 'mainnet.infura.io' },
port: 443,
connect_options: {
open_timeout: 20,
read_timeout: 140,
use_ssl: true,
rpc_path: ENV.fetch('INFURA_SERVER_KEY')
}

WatchingTransaction.find_each do |tx|
data = web3.eth.getTransaction tx.txhash

unless data
ok_tx, data = EthereumApi.get_transaction_by_hash(tx.txhash)
unless ok_tx == :ok && data
if WatchingTransaction.find_by(group_id: tx.group_id).count == 1
receipt = web3.eth.sendSignedTransaction(tx.signed_transaction)
tx.transaction_object['transactionHash'] = receipt['transactionHash']
tx.update_attributes(txhash: receipt['transactionHash'], transaction_object: tx.transaction_object)
ok_send, txhash = EthereumApi.send_raw_transaction(tx.signed_transaction)
if ok_send == :ok
puts "Resent transaction #{tx.txhash}, new hash is #{txhash}"
tx.transaction_object['transactionHash'] = txhash
tx.update_attributes(txhash: txhash, transaction_object: tx.transaction_object)
else
puts "Failed to resend #{tx.txhash}"
end
else
puts "Destroying transaction #{tx.txhash}"
tx.destroy
end
end

unless data['blockNumber'].nil?
puts "Destroying transactions from group #{tx.group_id}"
WatchingTransaction.find_by(group_id: tx.group_id).destroy
end
end
Expand Down
8 changes: 8 additions & 0 deletions lib/ethereum_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ def get_block_by_block_number(block_number)
unwrap_result(request_ethereum_server('eth_getBlockByNumber', [block_number, false]))
end

def get_transaction_by_hash(txhash)
unwrap_result(request_ethereum_server('eth_getTransactionByHash', [txhash]))
end

def send_raw_transaction(data)
unwrap_result(request_ethereum_server('eth_sendRawTransaction', [{:data => data}]))
end

private

def request_ethereum_server(method_name, method_args)
Expand Down
7 changes: 7 additions & 0 deletions lib/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@
puts 'Cleaning up old challenges took too long.'
end

scheduler.every '5m' do
puts 'Watching transactions'
WatchingTransaction.resend
rescue Rufus::Scheduler::TimeoutError
puts 'Watching transactions took too long.'
end

scheduler.join

0 comments on commit d74ad40

Please sign in to comment.