Skip to content

Commit

Permalink
DigixGlobal#70 resend transaction cron job code
Browse files Browse the repository at this point in the history
  • Loading branch information
bshevchenko committed Sep 2, 2019
1 parent bccfa76 commit e5764cf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ end
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

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

gem 'sanitize', '~> 5.0'
6 changes: 5 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ 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 @@ -374,10 +377,11 @@ DEPENDENCIES
typhoeus (>= 1.3.1)
tzinfo-data
validates_timeliness (~> 5.0.0.alpha3)
web3-eth
webmock (>= 3.4.2)

RUBY VERSION
ruby 2.6.0p0

BUNDLED WITH
1.17.2
1.17.3
8 changes: 7 additions & 1 deletion app/graphql/mutations/resend_transaction_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ def resolve(id:, transaction_object:, signed_transaction:)
key = :watched_transaction

old = WatchingTransaction.find(id)

user = context.fetch(:current_user, nil)
unless user.id == old.user.id
return form_error(key, 'id', 'Unauthorized action')
end

old_data = JSON.parse(old.transaction_object)
data = JSON.parse(transaction_object)

unless old_data['nonce'] == data['nonce']
return form_error(key, 'id', 'Nonce is not the same as the previous')
return form_error(key, 'transaction_object', 'Nonce is not the same as the previous')
end

result, tx_or_errors = WatchingTransaction.watch(
Expand Down
30 changes: 30 additions & 0 deletions app/models/watching_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,35 @@ def watch(user, transaction_object, signed_transaction, group_id)

[:ok, tx]
end

def resend # For 5 minute cron job
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
if WatchingTransaction.find_by(group_id: tx.group_id).count == 1
receipt = web3.eth.sendSignedTransaction(tx.signed_transaction)
data = JSON.parse(tx.transaction_object)
data['transactionHash'] = receipt['transactionHash']
tx.update_attributes(txhash: receipt['transactionHash'], transaction_object: JSON.generate(data))
else
tx.destroy
end
end

if data['blockNumber'] != nil
WatchingTransaction.find_by(group_id: tx.group_id).destroy
end
end
end
end
end

0 comments on commit e5764cf

Please sign in to comment.