Sha256: a2a6deca3a8c3d6bd9642fc1327a20e25a35a3b4bb8200a39b897726f5e402eb

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

module TxCatcher

  class Transaction < Sequel::Model

    plugin      :validation_helpers
    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_create
      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
        super
        validates_unique :txid
        errors.add(:base, "No outputs for this transactions") if self.deposits.empty?
      end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
txcatcher-0.1.5 lib/txcatcher/models/transaction.rb