Class: Wallets::Transfer

Inherits:
ApplicationRecord show all
Defined in:
lib/wallets/models/transfer.rb

Overview

A transfer records an internal movement of value between two wallets of the same asset. The actual balance impact lives in the linked transactions on each side so the ledger remains append-only.

Transfers keep the outbound leg singular and the inbound legs plural so the receiver can preserve the sender’s expiration buckets when one transfer consumes multiple source transactions with different expirations.

Constant Summary collapse

SUPPORTED_EXPIRATION_POLICIES =
%w[preserve none fixed].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resolved_configObject



22
23
24
25
# File 'lib/wallets/models/transfer.rb', line 22

def self.resolved_config
  value = config_provider
  value.respond_to?(:call) ? value.call : value
end

.table_nameObject



18
19
20
# File 'lib/wallets/models/transfer.rb', line 18

def self.table_name
  embedded_table_name || "#{resolved_config.table_prefix}transfers"
end

.transaction_classObject



27
28
29
# File 'lib/wallets/models/transfer.rb', line 27

def self.transaction_class
  transaction_class_name.constantize
end

Instance Method Details

#inbound_transactionObject



75
76
77
78
# File 'lib/wallets/models/transfer.rb', line 75

def inbound_transaction
  records = inbound_transactions.order(:id).limit(2).to_a
  records.one? ? records.first : nil
end

#inbound_transactionsObject



71
72
73
# File 'lib/wallets/models/transfer.rb', line 71

def inbound_transactions
  transfer_transactions_for(wallet_id: to_wallet_id).where("amount > 0")
end

#metadataObject



49
50
51
# File 'lib/wallets/models/transfer.rb', line 49

def 
  @indifferent_metadata ||= ActiveSupport::HashWithIndifferentAccess.new(super || {})
end

#metadata=(hash) ⇒ Object



53
54
55
56
# File 'lib/wallets/models/transfer.rb', line 53

def metadata=(hash)
  @indifferent_metadata = nil
  super(hash.respond_to?(:to_h) ? hash.to_h : {})
end

#outbound_transactionObject



67
68
69
# File 'lib/wallets/models/transfer.rb', line 67

def outbound_transaction
  outbound_transactions.order(:id).first
end

#outbound_transactionsObject



63
64
65
# File 'lib/wallets/models/transfer.rb', line 63

def outbound_transactions
  transfer_transactions_for(wallet_id: from_wallet_id).where("amount < 0")
end

#reloadObject



58
59
60
61
# File 'lib/wallets/models/transfer.rb', line 58

def reload(*)
  @indifferent_metadata = nil
  super
end