Skip to content

Commit

Permalink
[bug fix] - change how we filter out outlying staking APY (MystenLabs…
Browse files Browse the repository at this point in the history
…#13995)

## Description 

This PR change how we filter out outlying staking APY. This fixes
incorrect APY number in mainnet.

The APY for each epoch are calculated using the following equation:
`APY_e = (ER_e+1 / ER_e) ^ 365`
The outlying APYs are filtered out, ensuring APY is within bound
`0<APY<0.1` , a large unstaking event could cause a huge APY value.

We then sum 30 past APYs and take the average.

## Test Plan 

unit test

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
patrickkuo authored Sep 29, 2023
1 parent c688e6e commit bf10fa5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 37 deletions.
117 changes: 80 additions & 37 deletions crates/sui-json-rpc/src/governance_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::sync::Arc;
use async_trait::async_trait;
use cached::proc_macro::cached;
use cached::SizedCache;
use itertools::Itertools;
use jsonrpsee::core::RpcResult;
use jsonrpsee::RpcModule;
use tracing::{info, instrument};
Expand Down Expand Up @@ -264,52 +265,94 @@ impl GovernanceReadApiServer for GovernanceReadApi {
.await
.map_err(Error::from)?;

let mut apys = vec![];

for rates in exchange_rate_table.into_iter().filter(|r| r.active) {
// we start the apy calculation from the epoch when the stake subsidy starts
let exchange_rates = rates
.rates
.into_iter()
.filter_map(|(epoch, rate)| {
if epoch >= system_state_summary.stake_subsidy_start_epoch
&& (1.0 / rate.rate()) < 1.2
{
Some(rate)
} else {
None
}
})
// we only need the last 30 + 1 days of data
.take(31)
.collect::<Vec<_>>();

// we need at least 2 data points to calculate apy
let average_apy = if exchange_rates.len() >= 2 {
// rates are sorted by epoch in descending order.
let er_e = &exchange_rates[1..];
// rate e+1
let er_e_1 = &exchange_rates[..exchange_rates.len() - 1];
let dp_count = er_e.len();
er_e.iter().zip(er_e_1).map(calculate_apy).sum::<f64>() / dp_count as f64
} else {
0.0
};
let apys = calculate_apys(
system_state_summary.stake_subsidy_start_epoch,
exchange_rate_table,
);

apys.push(ValidatorApy {
address: rates.address,
apy: average_apy,
});
}
Ok(ValidatorApys {
apys,
epoch: system_state_summary.epoch,
})
}
}

fn calculate_apys(
stake_subsidy_start_epoch: u64,
exchange_rate_table: Vec<ValidatorExchangeRates>,
) -> Vec<ValidatorApy> {
let mut apys = vec![];

for rates in exchange_rate_table.into_iter().filter(|r| r.active) {
// we start the apy calculation from the epoch when the stake subsidy starts
let exchange_rates = rates.rates.into_iter().filter_map(|(epoch, rate)| {
if epoch >= stake_subsidy_start_epoch {
Some(rate)
} else {
None
}
});

// we need at least 2 data points to calculate apy
let average_apy = if exchange_rates.clone().count() >= 2 {
// rates are sorted by epoch in descending order.
let er_e = exchange_rates.clone().dropping(1);
// rate e+1
let er_e_1 = exchange_rates.dropping_back(1);
let apys = er_e
.zip(er_e_1)
.map(calculate_apy)
.filter(|apy| *apy > 0.0 && *apy < 0.1)
.take(30)
.collect::<Vec<_>>();

let apy_counts = apys.len() as f64;
apys.iter().sum::<f64>() / apy_counts
} else {
0.0
};
apys.push(ValidatorApy {
address: rates.address,
apy: average_apy,
});
}
apys
}

#[test]
fn test_apys_calculation_filter_outliers() {
// staking pool exchange rates extracted from mainnet
let file =
std::fs::File::open("src/unit_tests/data/validator_exchange_rate/rates.json").unwrap();
let rates: BTreeMap<String, Vec<(u64, PoolTokenExchangeRate)>> =
serde_json::from_reader(file).unwrap();

let mut address_map = BTreeMap::new();

let exchange_rates = rates
.into_iter()
.map(|(validator, rates)| {
let address = SuiAddress::random_for_testing_only();
address_map.insert(address, validator);
ValidatorExchangeRates {
address,
pool_id: ObjectID::random(),
active: true,
rates,
}
})
.collect();

let apys = calculate_apys(20, exchange_rates);

for apy in apys {
println!("{}: {}", address_map[&apy.address], apy.apy);
assert!(apy.apy < 0.07)
}
}

// APY_e = (ER_e+1 / ER_e) ^ 365
fn calculate_apy((rate_e, rate_e_1): (&PoolTokenExchangeRate, &PoolTokenExchangeRate)) -> f64 {
fn calculate_apy((rate_e, rate_e_1): (PoolTokenExchangeRate, PoolTokenExchangeRate)) -> f64 {
(rate_e.rate() / rate_e_1.rate()).powf(365.0) - 1.0
}

Expand Down

Large diffs are not rendered by default.

0 comments on commit bf10fa5

Please sign in to comment.