Skip to main content

Crate brk_cohort

Crate brk_cohort 

Source
Expand description

§brk_cohort

UTXO and address cohort filtering for on-chain analytics.

§What It Enables

Slice the UTXO set and address population by age, amount, output type, halving epoch, or holder classification (STH/LTH). Build complex cohorts by combining filters for metrics like “realized cap of 1+ BTC UTXOs older than 150 days.”

§Key Features

  • Age-based: TimeFilter::GreaterOrEqual(hours), TimeFilter::Range(hours..hours), TimeFilter::LowerThan(hours)
  • Amount-based: AmountFilter::GreaterOrEqual(Sats::_1BTC), AmountFilter::Range(Sats::_100K..Sats::_1M)
  • Term classification: Term::Sth (short-term holders, <150 days), Term::Lth (long-term holders)
  • Epoch filters: Group by halving epoch
  • Type filters: Segment by output type (P2PKH, P2TR, etc.)
  • Context-aware naming: Automatic prefix generation (utxos_, addrs_) based on cohort context
  • Inclusion logic: Filter hierarchy for aggregation (Filter::includes)

§Filter Types

pub enum Filter {
    All,
    Term(Term),           // STH/LTH
    Time(TimeFilter),     // Age-based
    Amount(AmountFilter), // Value-based
    Epoch(Halving),  // Halving epoch
    Year(Year),           // Calendar year
    Type(OutputType),     // P2PKH, P2TR, etc.
}

§Core API

// TimeFilter values are in hours (e.g., 3600 hours = 150 days)
let filter = Filter::Time(TimeFilter::GreaterOrEqual(3600));

// Check membership
filter.contains_time(4000);  // true (4000 hours > 3600 hours)
filter.contains_amount(sats);

// Generate metric names (via CohortContext)
let ctx = CohortContext::Utxo;
ctx.full_name(&filter, "min_age_150d");  // "utxos_min_age_150d"

§Built On

  • brk_error for error handling
  • brk_types for Sats, Halving, OutputType
  • brk_traversable for data structure traversal

Structs§

AddrGroups
Age
Represents the age of a UTXO or address balance. Encapsulates all age-related calculations in one type-safe struct.
AgeRange
AmountBucket
Bucket index for amount ranges. Use for cheap comparisons and direct lookups.
AmountRange
ByAddrType
ByAnyAddr
ByEpoch
ByTerm
ByType
Class
CohortName
Display names for a cohort with id (for storage/API), short (for charts), and long (for tooltips/labels)
Loss
9 “at least X% loss” aggregate thresholds.
OverAge
OverAmount
Profit
14 “at least X% profit” aggregate thresholds.
ProfitabilityRange
25 profitability range buckets ordered from most profitable to most in loss.
SpendableType
UTXOGroups
UnderAge
UnderAmount
UnspendableType

Enums§

AmountFilter
CohortContext
Context for cohort naming - determines whether a prefix is needed.
Filter
StateLevel
Controls the level of state tracking for a cohort.
Term
Classification for short-term vs long-term holders. The threshold is 150 days (approximately 5 months) = 3600 hours.
TimeFilter

Constants§

AGE_BOUNDARIES
Age boundaries in hours. Defines the cohort ranges: [0, 1h), [1h, 1d), [1d, 1w), [1w, 1m), …, [15y, ∞)
AGE_RANGE_BOUNDS
Age range bounds (end = usize::MAX means unbounded)
AGE_RANGE_FILTERS
Age range filters
AGE_RANGE_NAMES
Age range names
AMOUNT_RANGE_BOUNDS
Amount range bounds
AMOUNT_RANGE_FILTERS
Amount range filters
AMOUNT_RANGE_NAMES
Amount range names
CLASS_FILTERS
Class filters
CLASS_NAMES
Class names
CLASS_VALUES
Class values
EPOCH_FILTERS
Epoch filters
EPOCH_NAMES
Epoch names
EPOCH_VALUES
Epoch values
HOURS_1D
HOURS_1H
HOURS_1M
HOURS_1W
HOURS_1Y
HOURS_2M
HOURS_2Y
HOURS_3M
HOURS_3Y
HOURS_4M
HOURS_4Y
HOURS_5M
HOURS_5Y
HOURS_6M
HOURS_6Y
HOURS_7Y
HOURS_8Y
HOURS_10Y
HOURS_12Y
HOURS_15Y
LOSS_COUNT
Number of loss thresholds.
LOSS_NAMES
“At least X% loss” threshold names (9 thresholds).
OVER_AGE_FILTERS
Over-age filters (GreaterOrEqual threshold in hours)
OVER_AGE_HOURS
Over-age thresholds in hours
OVER_AGE_NAMES
Over-age names
OVER_AMOUNT_FILTERS
Over-amount filters
OVER_AMOUNT_NAMES
Over-amount names
OVER_AMOUNT_THRESHOLDS
Over-amount thresholds
P2A
P2PK33
P2PK65
P2PKH
P2SH
P2TR
P2WPKH
P2WSH
PROFITABILITY_BOUNDARY_COUNT
Number of profitability range boundaries (24 boundaries → 25 buckets).
PROFITABILITY_RANGE_COUNT
Number of profitability range buckets.
PROFITABILITY_RANGE_NAMES
Profitability range names (25 ranges, from most profitable to most in loss)
PROFIT_COUNT
Number of profit thresholds.
PROFIT_NAMES
“At least X% profit” threshold names (14 thresholds).
SPENDABLE_TYPE_FILTERS
Spendable type filters
SPENDABLE_TYPE_NAMES
Spendable type names
SPENDABLE_TYPE_VALUES
Spendable type values
TERM_FILTERS
Term filters
TERM_NAMES
Term names
TERM_VALUES
Term values
UNDER_AGE_FILTERS
Under-age filters (LowerThan threshold in hours)
UNDER_AGE_HOURS
Under-age thresholds in hours
UNDER_AGE_NAMES
Under-age names
UNDER_AMOUNT_FILTERS
Under-amount filters
UNDER_AMOUNT_NAMES
Under-amount names
UNDER_AMOUNT_THRESHOLDS
Under-amount thresholds

Traits§

Filtered

Functions§

amounts_in_different_buckets
Check if two amounts are in different buckets. O(1).
compute_profitability_boundaries
Compute 24 boundary prices from spot price for profitability bucketing.
zip2_by_addr_type
Zip two ByAddrTypes with a function, producing a new ByAddrType.
zip_by_addr_type
Zip one ByAddrType with a function, producing a new ByAddrType.