Module: Istox::BlockchainReceiptQuery

Extended by:
ActiveSupport::Concern
Defined in:
lib/istox/models/concerns/blockchain_receipt_query.rb

Instance Method Summary collapse

Instance Method Details

#get_blockchain_receiptsObject



81
82
83
# File 'lib/istox/models/concerns/blockchain_receipt_query.rb', line 81

def get_blockchain_receipts
  ::Istox::BlockchainReceipt.where(resource_id: self.id, resource_name: self.class.name).all
end

#handle_confirm(resource_action) ⇒ Object

overrideable, when blockchain transaction has confirmed



33
34
35
# File 'lib/istox/models/concerns/blockchain_receipt_query.rb', line 33

def handle_confirm(resource_action)
  self
end

#handle_extraObject

overrideable, extra data that will be published back to frontend



43
44
45
# File 'lib/istox/models/concerns/blockchain_receipt_query.rb', line 43

def handle_extra
  nil
end

#handle_fail(resource_action) ⇒ Object

overrideable, when blockchain transaction has failed



38
39
40
# File 'lib/istox/models/concerns/blockchain_receipt_query.rb', line 38

def handle_fail(resource_action)
  self
end

#outcomeObject

outcome of the blockchain transaction, can be “pending” / “confirmed” / “failed”



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
# File 'lib/istox/models/concerns/blockchain_receipt_query.rb', line 48

def outcome
  @blockchain_receipts = get_blockchain_receipts

  return "confirmed" if @blockchain_receipts.empty?
  
  txhashes = @blockchain_receipts.map do |blockchain_receipt|
    blockchain_receipt.txhash
  end

  # return pending state if there is any unknown hashes
  return "pending" if txhashes.any?{ |e| e.blank? }

  # if transactions store already exists, we should retrieve the state from
  @transaction_store ||= get_block_transactions(txhashes)
  @blocks = []

  @blockchain_receipts.map do |blockchain_receipt|
    transaction = @transaction_store.find { |t| t.txhash == blockchain_receipt.txhash}
    
    if transaction
      @blocks.push(transaction)
    else
      @blocks.push(OpenStruct.new({ txhash: blockchain_receipt.txhash, status: 'pending' }))
    end
  end
  


  return "failed" if blocks_failed?
  return "pending" if blocks_pending?
  return "confirmed" if blocks_confirmed?
end