aapy: higher weight for larger pools
This commit is contained in:
parent
1f3ffa42a2
commit
2449665fad
2 changed files with 15 additions and 5 deletions
|
|
@ -16,22 +16,28 @@ impl APYAggregator {
|
||||||
I: Iterator<Item = PoolPeriod>,
|
I: Iterator<Item = PoolPeriod>,
|
||||||
{
|
{
|
||||||
let mut weighted_apy_sum = Decimal::ZERO;
|
let mut weighted_apy_sum = Decimal::ZERO;
|
||||||
let mut total_active_time = Decimal::ZERO;
|
let mut total_weight = Decimal::ZERO;
|
||||||
|
|
||||||
for pool in pools {
|
for pool in pools {
|
||||||
let days_active = pool.duration_days(&period_start)?;
|
let days_active = pool.duration_days(&period_start)?;
|
||||||
|
|
||||||
if !days_active.is_zero() {
|
if !days_active.is_zero() {
|
||||||
let (_pool_yield, apy) = pool.yield_and_apy(&period_start)?;
|
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)
|
Ok(Decimal::ZERO)
|
||||||
} else {
|
} else {
|
||||||
Ok(weighted_apy_sum / total_active_time)
|
Ok(weighted_apy_sum / total_weight)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,10 @@ impl PoolPeriod {
|
||||||
.unwrap_or_default())
|
.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> {
|
pub fn pool_yield(&self) -> Result<Decimal> {
|
||||||
let start_k_sr = self.start_k.sqrt().context("failed to sqrt start")?;
|
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")?;
|
let end_k_sr = self.end_k.sqrt().context("failed to sqrt end")?;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue