vault.crypto_vaults
Documentation for eth_defi.vault.crypto_vaults Python module.
Build isolated metadata for the private crypto-vaults bundle.
The module uses the shared metric calculations and JSON serialisation helpers with its own input selection and persistent state. Public top-vault selection and crypto-bundle admission remain separate.
Module Attributes
Bundle identifier stored in crypto metadata and manifest documents. |
|
Stable schema version for crypto metadata, sticky state and manifests. |
|
Private daily Parquet filename for the isolated crypto-vaults bundle. |
|
Maximum provider-day gap filled when building the effective USD rate curve. |
|
Broad plausibility ranges for inverted USD-per-native exchange rates. |
|
Hard native-unit admission thresholds for the private crypto bundle. |
|
Columns read by |
Functions
Build effective USD rate curves and provenance for crypto metrics. |
|
|
Calculate and atomically persist private crypto-vault metadata. |
|
Create the isolated daily stablecoin/ETH/BTC price Parquet. |
|
Convert one common metric record to the crypto bundle schema. |
|
Calculate hard ETH/BTC admission from native lifetime peak assets. |
|
Resolve crypto bundle paths under one pipeline data directory. |
Classes
Summarise native-unit admission for one cleaned price frame. |
|
Explicit local paths for one crypto-vaults build. |
|
Validated effective USD-rate series for one denomination family. |
- CRYPTO_VAULTS_BUNDLE_NAME = 'crypto-vaults'
Bundle identifier stored in crypto metadata and manifest documents.
- CRYPTO_VAULTS_SCHEMA_VERSION = 2
Stable schema version for crypto metadata, sticky state and manifests.
- CRYPTO_CLEANED_PRICE_FILENAME = 'crypto-cleaned-vault-prices-1d.parquet'
Private daily Parquet filename for the isolated crypto-vaults bundle.
- USD_RATE_FORWARD_FILL_DAYS = 3
Maximum provider-day gap filled when building the effective USD rate curve. A longer gap starts a new metric segment rather than inventing a price.
- USD_RATE_BOUNDS = {'btc': (100.0, 1000000.0), 'eth': (10.0, 100000.0)}
Broad plausibility ranges for inverted USD-per-native exchange rates. They reject malformed provider values without turning a normal market move into an unsupported denomination failure.
- CRYPTO_NATIVE_MIN_PEAK_TOTAL_ASSETS = mappingproxy({<DenominationFamily.eth: 'eth'>: Decimal('2.5'), <DenominationFamily.btc: 'btc'>: Decimal('0.1')})
Hard native-unit admission thresholds for the private crypto bundle.
These values deliberately do not use a live USD conversion. The source denomination family is the unit used by
total_assetsin the cleaned price data, so the policy cannot drift as exchange rates change.
- NATIVE_METRIC_COLUMNS = ('id', 'chain', 'event_count', 'block_number', 'share_price', 'total_assets', 'vault_poll_frequency', 'available_liquidity', 'utilisation', 'leader_fraction', 'leader_commission', 'account_pnl', 'follower_count', 'cumulative_volume', 'perp_position_data_status', 'perp_long_notional', 'perp_short_notional', 'perp_largest_position_notional', 'perp_open_position_count', 'perp_metrics_observed_at', 'perp_quote_asset')
Columns read by
calculate_vault_recordon the native fast path.Keeping this projection explicit prevents large descriptive/object columns from being copied into every per-vault group. Optional columns retain the protocol-specific fields that the common record builder can export.
- class USDExchangeRateSeriesBuild
Bases:
objectValidated effective USD-rate series for one denomination family.
The result keeps the optional rate curve, its export provenance and a stable error together, avoiding positional tuple handling at the caller.
- rates: Optional[pandas.Series]
Effective USD-per-underlying daily rate curve when validation succeeds.
- class CryptoNativeAdmission
Bases:
objectSummarise native-unit admission for one cleaned price frame.
The summary is calculated from the lifetime maximum finite
total_assetsvalue for each ETH/BTC vault. It is also used to filter the cleaned Parquet before the expensive metadata calculation.- property skipped_ids: frozenset[str]
Return native IDs rejected by the hard threshold.
The difference is computed from immutable ID sets, so callers cannot accidentally alter the admission decision.
- Returns
Rejected native vault IDs.
- class CryptoVaultPaths
Bases:
objectExplicit local paths for one crypto-vaults build.
Instances are constructed by
resolve_crypto_vault_paths(), which derives every payload from one bundle directory.- directory: pathlib.Path
Bundle directory below the pipeline data directory.
- cleaned_price_path: pathlib.Path
Daily observation-preserving Parquet file.
- metadata_path: pathlib.Path
JSON metadata output.
- compressed_metadata_path: pathlib.Path
Brotli-compressed JSON metadata output.
- sticky_state_path: pathlib.Path
Private sticky qualification state.
- manifest_path: pathlib.Path
Current bundle manifest, created during R2 publication.
- __init__(directory, cleaned_price_path, metadata_path, compressed_metadata_path, sticky_state_path, manifest_path)
- Parameters
directory (pathlib.Path) –
cleaned_price_path (pathlib.Path) –
metadata_path (pathlib.Path) –
compressed_metadata_path (pathlib.Path) –
sticky_state_path (pathlib.Path) –
manifest_path (pathlib.Path) –
- Return type
None
- resolve_crypto_vault_paths(data_dir, directory=None)
Resolve crypto bundle paths under one pipeline data directory.
Every private bundle artefact is derived here so cleaning, metadata generation and publication cannot silently choose different locations.
- Parameters
data_dir (pathlib.Path) – Root pipeline data directory.
directory (Optional[pathlib.Path]) – Optional explicit crypto bundle directory.
- Returns
Explicit bundle paths using backup-safe unique basenames.
- Return type
- calculate_crypto_native_admission(vault_db, prices_df)
Calculate hard ETH/BTC admission from native lifetime peak assets.
The helper reads only
idandtotal_assetsplus the denomination family from the vault database. Invalid, negative and non-finite asset observations cannot qualify a vault. Threshold comparisons use the float64 representation actually stored in the Parquet, with the Decimal constants converted once at the policy boundary.- Parameters
vault_db (eth_defi.vault.vaultdb.VaultDatabase) – Vault metadata database used to classify each price row.
prices_df (pandas.DataFrame) – Cleaned price rows with
idandtotal_assetscolumns.
- Returns
Native admission summary including qualifying IDs and audit counts.
- Raises
ValueError – If the input frame lacks the columns required for native admission.
- Return type
- build_crypto_vault_prices(*, vault_db_path, uncleaned_path, cleaned_path, cleaned_stablecoin_path, cleaned_stablecoin_daily_path=None, settlement_db_path=None)
Create the isolated daily stablecoin/ETH/BTC price Parquet.
Stablecoin rows are derived from the existing standard cleaned Parquet; ETH/BTC rows are cleaned from raw data. The result retains the final valid price observation for each vault and UTC day. It also retains each vault’s first valid observation before its first daily close, preserving the lifetime-return basis and same-day chart eligibility. It does not forward fill the exported rows; the shared lifetime-metrics calculation forward fills only its internal calendar-day series.
- Parameters
vault_db_path (pathlib.Path) – Common scanner vault-metadata pickle.
uncleaned_path (pathlib.Path) – Shared raw vault-price Parquet source.
cleaned_path (pathlib.Path) – Isolated daily crypto Parquet destination.
cleaned_stablecoin_path (pathlib.Path) – Existing stablecoin-only cleaned Parquet from the standard cleaner.
cleaned_stablecoin_daily_path (Optional[pathlib.Path]) – Optional daily sidecar written by the standard cleaner while its hourly frame is resident. If absent, the hourly source is read and materialised as before.
settlement_db_path (Optional[pathlib.Path]) – Optional vault-settlement DuckDB database.
- Returns
None. Raises if price cleaning cannot safely complete.- Return type
None
Baseline (2026-09-22 production log): the crypto phase reread and materialised 10,116,623 hourly stablecoin rows and took about 1m30s. With the in-memory daily sidecar, the production-shaped bundle builder took 23.36 seconds for 2,409,598 output rows; the primary cleaner’s sidecar write took 5.91 seconds, for 29.27 seconds of integrated extra work. The production inputs were not retained as an immutable before/after pair, so no like-for-like speed-up is claimed. Peak RSS was 7.1 GiB for the builder process; the old phase did not record RSS. The sidecar is used only when its observation-policy version matches and its modification time is at least as new as the hourly source. The mtime comparison is a best-effort stale-file guard for restored artefacts, not a cryptographic generation identifier; the hourly file remains the fallback authority.
- build_crypto_vault_record(record, vault_row, threshold_usd)
Convert one common metric record to the crypto bundle schema.
The transformation preserves the observed denomination while replacing mixed-unit rankings and choosing the applicable stablecoin or native qualification policy.
- Parameters
vault_row (eth_defi.vault.vaultdb.VaultRow) – Source vault database row.
threshold_usd (decimal.Decimal) – Fixed USD qualification guideline for stablecoin records. ETH/BTC records use
CRYPTO_NATIVE_MIN_PEAK_TOTAL_ASSETSinstead.
- Returns
Native-unit crypto record and its resolved qualification threshold.
- Return type
tuple[dict[str, Any], decimal.Decimal]
- build_crypto_usd_conversion_context(vault_db, prices_df, exchange_rate_parquet_path)
Build effective USD rate curves and provenance for crypto metrics.
Currency API values are stored as quote units per USD. The provider label for day
Drepresents the value fetched around 00:00 UTC, so each raw USD→ETH/BTC value becomes an effective USD price forD - 1. Bounded forward fill only bridges short provider gaps; metric calculation later isolates the newest remaining contiguous segment.- Parameters
vault_db (eth_defi.vault.vaultdb.VaultDatabase) – Common vault metadata used to map vault IDs to denomination families.
prices_df (pandas.DataFrame) – Crypto cleaned prices with a naive UTC DatetimeIndex.
exchange_rate_parquet_path (pathlib.Path) – The verified cleaned exchange-rate snapshot used by this export.
- Returns
Shared calculation context and JSON-serialisable provenance.
- Raises
FileNotFoundError – If the shared exchange-rate snapshot is absent.
ValueError – If its schema does not support a reliable USD conversion.
- Return type
tuple[eth_defi.research.vault_metrics.CryptoUSDConversionContext, dict[str, Any]]
- build_crypto_vault_metadata(*, vault_db_path, cleaned_price_path, metadata_path, sticky_state_path, exchange_rate_parquet_path=None, threshold_usd=None)
Calculate and atomically persist private crypto-vault metadata.
The function follows the existing top-vaults commit order: metadata JSON is written first and sticky state immediately afterwards. A later R2 failure leaves the local state advanced, ready for the next successful publication.
- Parameters
vault_db_path (pathlib.Path) – Common vault metadata pickle.
cleaned_price_path (pathlib.Path) – Crypto daily cleaned Parquet path.
metadata_path (pathlib.Path) – Crypto metadata JSON destination.
sticky_state_path (pathlib.Path) – Crypto sticky-state JSON destination.
exchange_rate_parquet_path (Optional[pathlib.Path]) – Optional verified snapshot used to add USD period metrics to ETH/BTC vaults. Stablecoin records deliberately do not get a duplicate USD metric view.
threshold_usd (Optional[decimal.Decimal]) – Optional fixed USD guideline for stablecoin sticky admission; defaults to environment/config value. ETH/BTC admission is native-unit based.
- Returns
JSON-serialisable metadata document.
- Return type