Class: Istox::BlockchainStatusHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/istox/consumers/blockchain_status_handler.rb

Class Method Summary collapse

Class Method Details

.txn_data_confirmed(transactions) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/istox/consumers/blockchain_status_handler.rb', line 5

def txn_data_confirmed(transactions)
  found_txns = transactions.map do |transaction|
    receipt = ::Istox::BlockchainReceipt.where(txid: transaction.uuid).first
    if !receipt.blank?
      handling_receipt = ::Istox::BlockchainReceipt.where(txhash: transaction.id, txid: transaction.uuid).first
      raise 'Unable to find receipt' if handling_receipt.blank?
      transaction
    else
      puts 'Transaction doesnt belong here, skipping...'
      nil
    end
  end.compact

  found_txns.each do |transaction|
    receipt = ::Istox::BlockchainReceipt.where(txhash: transaction.id, txid: transaction.uuid).first

    receipt.update!(status: "confirmed")

    resource = begin
      class_eval("::#{receipt.resource_name}").find(receipt.resource_id)
    rescue => e
      puts "Class not found, skipping..."
      next
    end

    receipt_list = get_receipt_list(resource)

    next if resource_handled?(receipt_list)

    next if resource.outcome == 'pending'

    if resource.outcome == 'confirmed'
      resource.handle_confirm(receipt.resource_action)
      
      mark_resource_handled(receipt_list)
      publish_to_frontend(resource, true, receipt, receipt.sid, receipt_list)
    end

  end
end

.txn_status_changed(transactions, payload_target) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/istox/consumers/blockchain_status_handler.rb', line 46

def txn_status_changed(transactions, payload_target)
  # pre check all transaction does exists
  found_txns = transactions.map do |transaction|
    receipt = ::Istox::BlockchainReceipt.where(txid: transaction.uuid).first
    if !receipt.blank?
      handling_receipt = ::Istox::BlockchainReceipt.where(txhash: transaction.id, txid: transaction.uuid).first
      raise 'Unable to find receipt' if handling_receipt.blank?
      transaction
    else
      puts 'Transaction doesnt belong here, skipping...'
      nil
    end
  end.compact

  found_txns.each do |transaction|
    receipt = ::Istox::BlockchainReceipt.where(txhash: transaction.id, txid: transaction.uuid).first

    next if !(%w[failed pending].include?(transaction.status))
    receipt.update!(status: transaction.status)
    
    resource = begin
      class_eval("::#{receipt.resource_name}").find(receipt.resource_id)
    rescue => e
      puts "Class not found, skipping..."
      next
    end

    receipt_list = get_receipt_list(resource)

    next if resource_handled?(receipt_list)

    # if transaction.status == 'confirmed'
    #   next if resource.outcome == 'pending'

    #   if resource.outcome == 'confirmed'
    #     resource.handle_confirm(receipt.resource_action)
        
    #     mark_resource_handled(receipt_list)
    #     publish_to_frontend(resource, true, receipt, receipt.sid, receipt_list)
    #   end
    # end

    if transaction.status == 'failed'
      resource.handle_fail(receipt.resource_action)

      mark_resource_handled(receipt_list)
      publish_to_frontend(resource, false, receipt, receipt.sid, receipt_list)
    end

  end
end