Class: Istox::BlockchainStatusHandler

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

Class Method Summary collapse

Class Method Details

.chainhub_response_event(transaction) ⇒ Object

format of transaction

type: 'CHAINHUB_RESPONSE_EVENT',
uuid:'123', //original request uuid sent from backend
success:true, //status of the whole request in true/false
message:'',  //In case of error, the message contain the error reason
txnCount:1,  //Number of blockchain transactions from the original request
txnHashes:[] //Hashes for all transactions that has been submitted and mined by the blockchain for verification purpose



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
45
46
47
48
49
50
51
52
53
54
# File 'lib/istox/consumers/blockchain_status_handler.rb', line 16

def chainhub_response_event(transaction)
  receipt = ::Istox::BlockchainReceipt.where(txid: transaction.uuid).first
  if receipt.blank?
    log.info 'Transaction doesn\'t belong to this backend, skipping...'
    return
  end

  status = transaction.success ? 'confirmed' : 'failed'

  receipt.update!(status: status, message: transaction.message,
                  txhash: transaction.txn_hashes.present? ? transaction.txn_hashes.join(',') : nil)

  resource = begin
    cls = class_eval("::#{receipt.resource_name}", __FILE__, __LINE__)
    if cls.respond_to?('with_deleted')
      cls.with_deleted.find(receipt.resource_id)
    else
      cls.find(receipt.resource_id)
    end
             rescue NameError
               log.warn "Transaction bind to #{receipt.resource_name} with id #{receipt.resource_id} class not found, skipping..."
               return
  end

  return if resource_handled?(receipt)

  if status == 'confirmed'
    resource.handle_confirm(receipt)

    mark_resource_handled(receipt)
    publish_to_frontend(resource, true, receipt, receipt.sid)

  elsif status == 'failed'
    resource.handle_fail(receipt)

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