Sha256: 74d9d14387c452723d586f1a79d94a467ecee4e159a16cb57d2b584cdd9b8263

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

module TxCatcher

  class Transaction < Sequel::Model
    one_to_many :deposits

    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 = Satoshi.new(out["value"], from_unit: :btc).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

    def after_save
      self.deposits.each do |d|
        d.transaction = self
        d.save
      end
    end

    def tx_hash
      @tx_hash || parse_transaction
    end

    def confirmations
      if self.block_height
        TxCatcher.current_block_height - self.block_height + 1
      else
        0
      end
    end

    private

      def parse_transaction
        @tx_hash = TxCatcher.rpc_node.decoderawtransaction(self.hex)
      end

      def assign_transaction_attrs
        self.txid = @tx_hash["txid"]
      end

      def validate
        errors.add(:base, "No outputs for this transactions") if self.deposits.empty?
      end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
txcatcher-0.1.4 lib/txcatcher/models/transaction.rb
txcatcher-0.1.3 lib/txcatcher/models/transaction.rb
txcatcher-0.1.2 lib/txcatcher/models/transaction.rb
txcatcher-0.1.1 lib/txcatcher/models/transaction.rb
txcatcher-0.1.0 lib/txcatcher/models/transaction.rb