Class: TxCatcher::Transaction
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- TxCatcher::Transaction
- Defined in:
- lib/txcatcher/models/transaction.rb
Instance Method Summary collapse
- #after_create ⇒ Object
- #before_create ⇒ Object
- #before_validation ⇒ Object
- #confirmations ⇒ Object
- #tx_hash ⇒ Object
-
#update_block_height!(limit = 100) ⇒ Object
Queries rpc node to check whether the transaction has been included in any of the blocks, but only if current block_height is nil.
Instance Method Details
#after_create ⇒ Object
26 27 28 29 30 31 |
# File 'lib/txcatcher/models/transaction.rb', line 26 def after_create self.deposits.each do |d| d.transaction = self d.save end end |
#before_create ⇒ Object
22 23 24 |
# File 'lib/txcatcher/models/transaction.rb', line 22 def before_create self.created_at = Time.now end |
#before_validation ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/txcatcher/models/transaction.rb', line 8 def before_validation return if !self.new? || !self.deposits.empty? parse_transaction assign_transaction_attrs @tx_hash["vout"].uniq { |out| out["n"] }.each do |out| amount = CryptoUnit.new(Config["currency"], out["value"], from_unit: :standart).to_i if out["value"] address = out["scriptPubKey"]["addresses"]&.first # Do not create a new deposit unless it actually makes sense to create one if address && amount && amount > 0 self.deposits << Deposit.new(amount: amount, address_string: address) end end end |
#confirmations ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/txcatcher/models/transaction.rb', line 37 def confirmations if self.block_height TxCatcher.current_block_height - self.block_height + 1 else 0 end end |
#tx_hash ⇒ Object
33 34 35 |
# File 'lib/txcatcher/models/transaction.rb', line 33 def tx_hash @tx_hash || parse_transaction end |
#update_block_height!(limit = 100) ⇒ Object
Queries rpc node to check whether the transaction has been included in any of the blocks, but only if current block_height is nil.
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 |
# File 'lib/txcatcher/models/transaction.rb', line 47 def update_block_height!(limit=100) # This calculates the approximate number of blocks to check. # So, for example, if transaction is less than 10 minutes old, # there's probably no reason to try and check more than 2-3 blocks back. # However, to make absolute sure, we always bump up this number for 10 blocks. # Over larger periods of time, the avg block per minute value should even out, so # it's probably going to be fine either way. created_minutes_ago = ((self.created_at - Time.now).to_i/60) blocks_to_check = (created_minutes_ago / 10).abs + 10 return self.block_height if self.block_height blocks_to_check = limit if blocks_to_check > limit i = 0 while i <= blocks_to_check block_hash = TxCatcher.rpc_node.getblockhash(TxCatcher.current_block_height - i) block = TxCatcher.rpc_node.getblock(block_hash) i += 1 LOGGER.report "--- checking block #{TxCatcher.current_block_height - i} for tx #{self.txid}" if block["tx"] && block["tx"].include?(self.txid) LOGGER.report "tx #{self.txid} block height updated to #{block["height"]}" self.update(block_height: block["height"]) return block["height"].to_i end end end |