Class: Wallets::Transfer
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Wallets::Transfer
- 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
- #inbound_transaction ⇒ Object
- #inbound_transactions ⇒ Object
- #metadata ⇒ Object
- #metadata=(hash) ⇒ Object
- #outbound_transaction ⇒ Object
- #outbound_transactions ⇒ Object
- #reload ⇒ Object
Class Method Details
.resolved_config ⇒ Object
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_name ⇒ Object
18 19 20 |
# File 'lib/wallets/models/transfer.rb', line 18 def self.table_name || "#{resolved_config.table_prefix}transfers" end |
.transaction_class ⇒ Object
27 28 29 |
# File 'lib/wallets/models/transfer.rb', line 27 def self.transaction_class transaction_class_name.constantize end |
Instance Method Details
#inbound_transaction ⇒ Object
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_transactions ⇒ Object
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 |
#metadata ⇒ Object
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 (hash) @indifferent_metadata = nil super(hash.respond_to?(:to_h) ? hash.to_h : {}) end |
#outbound_transaction ⇒ Object
67 68 69 |
# File 'lib/wallets/models/transfer.rb', line 67 def outbound_transaction outbound_transactions.order(:id).first end |
#outbound_transactions ⇒ Object
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 |
#reload ⇒ Object
58 59 60 61 |
# File 'lib/wallets/models/transfer.rb', line 58 def reload(*) @indifferent_metadata = nil super end |