forked from DigixGlobal/dao-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DigixGlobal#70 WatchedTransaction graphql api draft
- Loading branch information
1 parent
03e36e2
commit d4a0071
Showing
5 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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
21
app/graphql/types/watched_transaction/watched_transaction_type.rb
This file contains 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
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 |
This file contains 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