Skip to content

Commit

Permalink
DigixGlobal#70 WatchedTransaction graphql api draft
Browse files Browse the repository at this point in the history
  • Loading branch information
bshevchenko committed Sep 1, 2019
1 parent 03e36e2 commit d4a0071
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
63 changes: 63 additions & 0 deletions app/graphql/mutations/resend_transaction_mutation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

module Mutations
class WatchTransactionMutation < Types::Base::BaseMutation
description 'Given an old transaction, resend it with new parameters or gas prices'

argument :id, String,
required: true,
description: 'ID of the transaction to be resent'
argument :transaction_object, String,
required: true,
description: 'The JSONified transaction data object'
argument :signed_transaction, String,
required: true,
description: 'Signed transaction in HEX format'

field :watched_transaction, Types::WatchedTransaction::WatchedTransactionType,
null: true,
description: 'Newly created transaction'
field :errors, [UserErrorType],
null: false,
description: <<~EOS
Mutation errors
Operation Errors:
- Previous transaction not found
- Nonce is not the same as the previous
EOS

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

unless (old = WatchingTransaction.find(id))
return form_error(key, 'transaction_object', 'Previous transaction not found')
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')
end

result, tx_or_errors = WatchingTransaction.resend(
context.fetch(:current_user),
transaction_object,
signed_transaction,
id
)

case result
when :invalid_data
model_errors(key, tx_or_errors)
when :ok
model_result(key, tx_or_errors)
end
end

def self.authorized?(object, context)
super && context.fetch(:current_user, nil)
end
end
end
43 changes: 43 additions & 0 deletions app/graphql/mutations/watch_transaction_mutation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module Mutations
class WatchTransactionMutation < Types::Base::BaseMutation
description 'Given a transaction, save it to the database to be resent'

argument :transaction_object, String,
required: true,
description: 'The JSONified transaction data object'
argument :signed_transaction, String,
required: true,
description: 'Signed transaction in HEX format'

field :watched_transaction, Types::WatchedTransaction::WatchedTransactionType,
null: true,
description: 'Newly created transaction'
field :errors, [UserErrorType],
null: false,
description: 'Mutation errors'

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

result, tx_or_errors = WatchingTransaction.watch(
context.fetch(:current_user),
transaction_object,
signed_transaction,
nil
)

case result
when :invalid_data
model_errors(key, tx_or_errors)
when :ok
model_result(key, tx_or_errors)
end
end

def self.authorized?(object, context)
super && context.fetch(:current_user, nil)
end
end
end
19 changes: 19 additions & 0 deletions app/graphql/resolvers/watched_transaction_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Resolvers
class WatchedTransactionResolver < Resolvers::Base
type Types::WatchedTransaction::WatchedTransactionType, null: true

argument :txhash, String,
required: true,
description: 'Find the last watched transaction in the group with that txhash'

def resolve(txhash:)
WatchingTransaction.find_by(txhash: txhash) # TODO or get group_id from this one and find latter by it?
end

def self.authorized?(object, context)
super && context.fetch(:current_user, nil)
end
end
end
21 changes: 21 additions & 0 deletions app/graphql/types/watched_transaction/watched_transaction_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Types
module WatchedTransaction
class WatchedTransactionType < Types::Base::BaseObject
description 'Transactions that are being watched in the blockchain'

field :id, ID,
null: false,
description: 'UUID of the watched transaction'

field :user, Types::User::UserType,
null: false,
description: 'Signer of the transaction'

field :transaction_object, String,
null: false,
description: 'The JSONified transaction data object'
end
end
end
17 changes: 17 additions & 0 deletions app/models/watching_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,21 @@ def set_uuid
def txhash=(value)
super(value&.downcase)
end

def watch(user, transaction_object, signed_transaction, group_id)
data = JSON.parse(transaction_object)

tx = WatchingTransaction.new(
user: user,
transaction_object: transaction_object,
signed_transaction: signed_transaction,
txhash: data.transactionHash,
group_id: group_id
)

return [:invalid_data, comment.errors] unless comment.valid?
return [:database_error, comment.errors] unless comment.save

[:ok, tx]
end
end

0 comments on commit d4a0071

Please sign in to comment.