Sha256: 9136bc98462e5272f1c09d8ac239ab5493734926d5529fbc59a91622b0a077ce

Contents?: true

Size: 1.89 KB

Versions: 7

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module Glueby
  module Internal
    class Wallet
      module AR
        class Utxo < ::ActiveRecord::Base
          belongs_to :key

          validates :txid, uniqueness: { scope: :index, case_sensitive: false }
          validate :check_dust_output

          enum status: { init: 0, broadcasted: 1, finalized: 2 }

          def color_id
            script = Tapyrus::Script.parse_from_payload(script_pubkey.htb)
            script.color_id&.to_hex
          end

          # Delete utxo spent by specified tx.
          #
          # @param [Tapyrus::Tx] the spending tx
          def self.destroy_for_inputs(tx)
            tx.inputs.each do |input|
              Utxo.destroy_by(txid: input.out_point.txid, index: input.out_point.index)
            end
          end

          # Create utxo or update utxo for tx outputs
          # if there is no key for script pubkey in an output, utxo for the output is not created.
          #
          # @param [Tapyrus::Tx] tx
          def self.create_or_update_for_outputs(tx, status: :finalized)
            tx.outputs.each.with_index do |output, index|
              key = Key.key_for_output(output)
              next unless key

              utxo = Utxo.find_or_initialize_by(txid: tx.txid, index: index)
              utxo.update!(
                label: key.label,
                script_pubkey: output.script_pubkey.to_hex,
                value: output.value,
                status: status,
                key: key
              )
            end
          end

        private

          def check_dust_output
            output = Tapyrus::TxOut.new(value: value, script_pubkey: Tapyrus::Script.parse_from_payload(script_pubkey.htb))
            if !color_id && output.dust?
              errors.add(:value, "is less than dust limit(#{output.send(:dust_threshold)})")
            end
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
glueby-1.2.3 lib/glueby/internal/wallet/active_record/utxo.rb
glueby-1.2.2 lib/glueby/internal/wallet/active_record/utxo.rb
glueby-1.2.1 lib/glueby/internal/wallet/active_record/utxo.rb
glueby-1.2.0 lib/glueby/internal/wallet/active_record/utxo.rb
glueby-1.2.0.beta.3 lib/glueby/internal/wallet/active_record/utxo.rb
glueby-1.2.0.beta.2 lib/glueby/internal/wallet/active_record/utxo.rb
glueby-1.2.0.beta.1 lib/glueby/internal/wallet/active_record/utxo.rb