aapy: higher weight for larger pools

This commit is contained in:
Dagur Valberg Johannsson 2024-11-13 22:32:32 +01:00
parent 1f3ffa42a2
commit 2449665fad
No known key found for this signature in database
GPG key ID: FD701804AEE88107
2 changed files with 15 additions and 5 deletions

View file

@ -16,22 +16,28 @@ impl APYAggregator {
I: Iterator<Item = PoolPeriod>,
{
let mut weighted_apy_sum = Decimal::ZERO;
let mut total_active_time = Decimal::ZERO;
let mut total_weight = Decimal::ZERO;
for pool in pools {
let days_active = pool.duration_days(&period_start)?;
if !days_active.is_zero() {
let (_pool_yield, apy) = pool.yield_and_apy(&period_start)?;
weighted_apy_sum += apy * days_active;
total_active_time += days_active;
// we give larger pools more weight in the aggregated apy. They have more liquidity,
// so the aggregated number will be more accurate.
let pool_size = pool.end_k_sqrt()?;
let weighted_apy = apy * days_active * pool_size;
weighted_apy_sum += weighted_apy;
total_weight += days_active * pool_size;
}
}
if total_active_time.is_zero() {
if total_weight.is_zero() {
Ok(Decimal::ZERO)
} else {
Ok(weighted_apy_sum / total_active_time)
Ok(weighted_apy_sum / total_weight)
}
}
}

View file

@ -77,6 +77,10 @@ impl PoolPeriod {
.unwrap_or_default())
}
pub fn end_k_sqrt(&self) -> Result<Decimal> {
self.end_k.sqrt().context("failed to sqrt end")
}
pub fn pool_yield(&self) -> Result<Decimal> {
let start_k_sr = self.start_k.sqrt().context("failed to sqrt start")?;
let end_k_sr = self.end_k.sqrt().context("failed to sqrt end")?;