mirror of
https://github.com/we-promise/sure.git
synced 2026-04-08 06:44:52 +00:00
* Feat(CoinStats): Scaffold implementation, not yet functional * Feat(CoinStats): Implement crypto wallet balance and transactions * Feat(CoinStats): Add tests, Minor improvements * Feat(CoinStats): Utilize bulk fetch API endpoints * Feat(CoinStats): Migrate strings to i8n * Feat(CoinStats): Fix error handling in wallet link modal * Feat(CoinStats): Implement hourly provider sync job * Feat(CoinStats): Generate docstrings * Fix(CoinStats): Validate API Key on provider update * Fix(Providers): Safely handle race condition in merchance creation * Fix(CoinStats): Don't catch system signals in account processor * Fix(CoinStats): Preload before iterating accounts * Fix(CoinStats): Add no opener / referrer to API dashboard link * Fix(CoinStats): Use strict matching for symbols * Fix(CoinStats): Remove dead code in transactions importer * Fix(CoinStats): Avoid transaction fallback ID collisions * Fix(CoinStats): Improve Blockchains fetch error handling * Fix(CoinStats): Enforce NOT NULL constraint for API Key schema * Fix(CoinStats): Migrate sync status strings to i8n * Fix(CoinStats): Use class name rather than hardcoded string * Fix(CoinStats): Use account currency rather than hardcoded USD * Fix(CoinStats): Migrate from standalone to Provider class * Fix(CoinStats): Fix test failures due to string changes
72 lines
2.5 KiB
Ruby
72 lines
2.5 KiB
Ruby
# Represents a single crypto token/coin within a CoinStats wallet.
|
|
# Each wallet address may have multiple CoinstatsAccounts (one per token).
|
|
class CoinstatsAccount < ApplicationRecord
|
|
include CurrencyNormalizable
|
|
|
|
belongs_to :coinstats_item
|
|
|
|
# Association through account_providers (standard pattern for all providers)
|
|
has_one :account_provider, as: :provider, dependent: :destroy
|
|
has_one :account, through: :account_provider, source: :account
|
|
|
|
validates :name, :currency, presence: true
|
|
validates :account_id, uniqueness: { scope: :coinstats_item_id, allow_nil: true }
|
|
|
|
# Alias for compatibility with provider adapter pattern
|
|
alias_method :current_account, :account
|
|
|
|
# Updates account with latest balance data from CoinStats API.
|
|
# @param account_snapshot [Hash] Normalized balance data from API
|
|
def upsert_coinstats_snapshot!(account_snapshot)
|
|
# Convert to symbol keys or handle both string and symbol keys
|
|
snapshot = account_snapshot.with_indifferent_access
|
|
|
|
# Build attributes to update
|
|
attrs = {
|
|
current_balance: snapshot[:balance] || snapshot[:current_balance],
|
|
currency: parse_currency(snapshot[:currency]) || "USD",
|
|
name: snapshot[:name],
|
|
account_status: snapshot[:status],
|
|
provider: snapshot[:provider],
|
|
institution_metadata: {
|
|
logo: snapshot[:institution_logo]
|
|
}.compact,
|
|
raw_payload: account_snapshot
|
|
}
|
|
|
|
# Only set account_id if provided and not already set (preserves ID from initial creation)
|
|
if snapshot[:id].present? && account_id.blank?
|
|
attrs[:account_id] = snapshot[:id].to_s
|
|
end
|
|
|
|
update!(attrs)
|
|
end
|
|
|
|
# Stores transaction data from CoinStats API for later processing.
|
|
# @param transactions_snapshot [Hash, Array] Raw transactions response or array
|
|
def upsert_coinstats_transactions_snapshot!(transactions_snapshot)
|
|
# CoinStats API returns: { meta: { page, limit }, result: [...] }
|
|
# Extract just the result array for storage, or use directly if already an array
|
|
transactions_array = if transactions_snapshot.is_a?(Hash)
|
|
snapshot = transactions_snapshot.with_indifferent_access
|
|
snapshot[:result] || []
|
|
elsif transactions_snapshot.is_a?(Array)
|
|
transactions_snapshot
|
|
else
|
|
[]
|
|
end
|
|
|
|
assign_attributes(
|
|
raw_transactions_payload: transactions_array
|
|
)
|
|
|
|
save!
|
|
end
|
|
|
|
private
|
|
|
|
def log_invalid_currency(currency_value)
|
|
Rails.logger.warn("Invalid currency code '#{currency_value}' for CoinstatsAccount #{id}, defaulting to USD")
|
|
end
|
|
end
|