Skip to content

Commit

Permalink
DigixGlobal#70 transaction hash as a separate argument
Browse files Browse the repository at this point in the history
  • Loading branch information
bshevchenko committed Sep 10, 2019
1 parent 752eabc commit e65ddcc
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
6 changes: 5 additions & 1 deletion app/graphql/mutations/resend_transaction_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class ResendTransactionMutation < Types::Base::BaseMutation
argument :id, ID,
required: true,
description: 'ID of the transaction to be resent'
argument :transaction_hash, String,
required: true,
description: 'Transaction hash'
argument :transaction_object, Types::Scalar::JSONObject,
required: true,
description: 'The JSONified transaction data object'
Expand All @@ -28,14 +31,15 @@ class ResendTransactionMutation < Types::Base::BaseMutation
- Nonce is not the same as the previous
EOS

def resolve(id:, transaction_object:, signed_transaction:)
def resolve(id:, transaction_hash:, transaction_object:, signed_transaction:)
key = :watched_transaction

unless (old = WatchingTransaction.find_by(id: id))
return form_error(key, 'transaction_object', 'Previous transaction not found')
end

attrs = {
transaction_hash: transaction_hash,
transaction_object: transaction_object,
signed_transaction: signed_transaction
}
Expand Down
6 changes: 5 additions & 1 deletion app/graphql/mutations/watch_transaction_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module Mutations
class WatchTransactionMutation < Types::Base::BaseMutation
description 'Given a transaction, save it to the database to be resent'

argument :transaction_hash, String,
required: true,
description: 'Transaction hash'
argument :transaction_object, Types::Scalar::JSONObject,
required: true,
description: 'The JSONified transaction data object'
Expand All @@ -23,10 +26,11 @@ class WatchTransactionMutation < Types::Base::BaseMutation
- Invalid transaction object
EOS

def resolve(transaction_object:, signed_transaction:)
def resolve(transaction_hash:, transaction_object:, signed_transaction:)
key = :watched_transaction

attrs = {
transaction_hash: transaction_hash,
transaction_object: transaction_object,
signed_transaction: signed_transaction
}
Expand Down
10 changes: 10 additions & 0 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,11 @@ input ResendTransactionMutationInput {
"""
signedTransaction: String!

"""
Transaction hash
"""
transactionHash: String!

"""
The JSONified transaction data object
"""
Expand Down Expand Up @@ -2509,6 +2514,11 @@ input WatchTransactionMutationInput {
"""
signedTransaction: String!

"""
Transaction hash
"""
transactionHash: String!

"""
The JSONified transaction data object
"""
Expand Down
10 changes: 4 additions & 6 deletions app/models/watching_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ def txhash=(value)

class << self
def watch(user, attrs)
transaction_object = attrs.fetch(:transaction_object, nil)
tx = WatchingTransaction.new(
user: user,
transaction_object: transaction_object,
transaction_object: attrs.fetch(:transaction_object, nil),
signed_transaction: attrs.fetch(:signed_transaction, nil),
txhash: transaction_object.fetch('transactionHash', nil)
txhash: attrs.fetch(:transaction_hash, nil)
)

return [:invalid_data, tx.errors] unless tx.valid?
Expand All @@ -61,7 +60,7 @@ def resend(user, watching_transaction, attrs)
user: user,
transaction_object: transaction_object,
signed_transaction: attrs.fetch(:signed_transaction, nil),
txhash: transaction_object.fetch('transactionHash', nil),
txhash: attrs.fetch(:transaction_hash, nil),
group_id: watching_transaction.group_id
)

Expand All @@ -79,8 +78,7 @@ def resend_transactions
ok_send, txhash = EthereumApi.send_raw_transaction(tx.signed_transaction)
if ok_send == :ok
Rails.logger.info "Resent transaction #{tx.txhash}, new hash is #{txhash}"
tx.transaction_object['transactionHash'] = txhash
tx.update_attributes(txhash: txhash, transaction_object: tx.transaction_object)
tx.update_attributes(txhash: txhash)
else
Rails.logger.info "Failed to resend #{tx.txhash}"
end
Expand Down
5 changes: 3 additions & 2 deletions test/factories/watching_transactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

FactoryBot.define do
sequence(:transaction_object) do |_|
{ nonce: Random.rand(1..1000), transactionHash: Faker::Blockchain::Ethereum.address }
{ nonce: Random.rand(1..1000) }
end
sequence(:fixed_transaction_object) do |_|
{ nonce: static_nonce, transactionHash: Faker::Blockchain::Ethereum.address }
{ nonce: static_nonce }
end
sequence(:signed_transaction) { |_| SecureRandom.hex(32) }
sequence(:id) { |_| SecureRandom.hex(32) }
Expand All @@ -22,6 +22,7 @@
end

factory :watch_transaction, class: 'Hash' do
transactionHash { generate(:txhash) }
transactionObject { JSON.generate(generate(:transaction_object)) }
signedTransaction { generate(:signed_transaction) }

Expand Down
4 changes: 2 additions & 2 deletions test/graphql/resend_transaction_mutation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class ResendTransactionMutationTest < ActiveSupport::TestCase
QUERY = <<~EOS
mutation($id: ID!, $transactionObject: JSONObject!, $signedTransaction: String!) {
resendTransaction(input: { id: $id, transactionObject: $transactionObject, signedTransaction: $signedTransaction }) {
mutation($id: ID!, $transactionHash: String!, $transactionObject: JSONObject!, $signedTransaction: String!) {
resendTransaction(input: { id: $id, transactionHash: $transactionHash, transactionObject: $transactionObject, signedTransaction: $signedTransaction }) {
watchedTransaction {
id
user {
Expand Down
4 changes: 2 additions & 2 deletions test/graphql/watch_transaction_mutation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class WatchTransactionMutationTest < ActiveSupport::TestCase
QUERY = <<~EOS
mutation($transactionObject: JSONObject!, $signedTransaction: String!) {
watchTransaction(input: { transactionObject: $transactionObject, signedTransaction: $signedTransaction }) {
mutation($transactionHash: String!, $transactionObject: JSONObject!, $signedTransaction: String!) {
watchTransaction(input: { transactionHash: $transactionHash, transactionObject: $transactionObject, signedTransaction: $signedTransaction }) {
watchedTransaction {
id
user {
Expand Down

0 comments on commit e65ddcc

Please sign in to comment.