forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Calculate the rolling average apy (MystenLabs#9880)
## Description Describe the changes or additions included in this PR. - Update the APY calculator using RollingAverageApy of the last 30 epochs Formuler ` // APY_e = (1 + epoch_rewards / stake)^365-1 // APY_e_30rollingaverage = average(APY_e,APY_e-1,…,APY_e-29);` - Query the previous epoch for each validator using `useGetValidatorsEvents` - Moved `useGetValidatorsEvents` to `@mysten/core` - Added `useGetRollingAverageApys` to `@mysten/core` - Update wallet and explorer to use the hook - Move event query param to the SDK `0x3::validator_set::ValidatorEpochInfoEvent` ## Test Plan How did you test the new or updated feature? --- 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) - [x] 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
1 parent
f4db061
commit 2e0ef59
Showing
15 changed files
with
201 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@mysten/sui.js": minor | ||
--- | ||
|
||
Added VALIDATORS_EVENTS_QUERY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useMemo } from 'react'; | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { useGetValidatorsEvents } from './useGetValidatorsEvents'; | ||
import { roundFloat } from '../utils/roundFloat'; | ||
|
||
// recentEpochRewards is list of the last 30 epoch rewards for a specific validator | ||
// APY_e = (1 + epoch_rewards / stake)^365-1 | ||
// APY_e_30rollingaverage = average(APY_e,APY_e-1,…,APY_e-29); | ||
|
||
const ROLLING_AVERAGE = 30; | ||
const DEFAULT_APY_DECIMALS = 4; | ||
|
||
// define the type parsedJson response | ||
type ParsedJson = { | ||
commission_rate: string; | ||
epoch: string; | ||
pool_staking_reward: string; | ||
pool_token_exchange_rate: { | ||
pool_token_amount: string; | ||
sui_amount: string; | ||
}; | ||
reference_gas_survey_quote: string; | ||
stake: string; | ||
storage_fund_staking_reward: string; | ||
tallying_rule_global_score: string; | ||
tallying_rule_reporters: string[]; | ||
validator_address: string; | ||
}; | ||
|
||
interface ApyGroups { | ||
[validatorAddress: string]: number[]; | ||
} | ||
|
||
export interface ApyByValidator { | ||
[validatorAddress: string]: number; | ||
} | ||
|
||
const calculateApy = (stake: string, poolStakingReward: string) => { | ||
const poolStakingRewardBigNumber = new BigNumber(poolStakingReward); | ||
const stakeBigNumber = new BigNumber(stake); | ||
// Calculate the ratio of pool_staking_reward / stake | ||
const ratio = poolStakingRewardBigNumber.dividedBy(stakeBigNumber); | ||
|
||
// Perform the exponentiation and subtraction using BigNumber | ||
const apy = ratio.plus(1).pow(365).minus(1); | ||
return apy.toNumber(); | ||
}; | ||
|
||
export function useGetRollingAverageApys(numberOfValidators: number | null) { | ||
// Set the limit to the number of validators * the rolling average | ||
// Order the response in descending order so that the most recent epoch are at the top | ||
const validatorEpochEvents = useGetValidatorsEvents({ | ||
limit: numberOfValidators ? numberOfValidators * ROLLING_AVERAGE : null, | ||
order: 'descending', | ||
}); | ||
|
||
const apyByValidator = | ||
useMemo<ApyByValidator | null>(() => { | ||
if ( | ||
!validatorEpochEvents?.data || | ||
!validatorEpochEvents?.data?.data | ||
) { | ||
return null; | ||
} | ||
const apyGroups: ApyGroups = {}; | ||
validatorEpochEvents.data.data.forEach(({ parsedJson }) => { | ||
const { stake, pool_staking_reward, validator_address } = | ||
parsedJson as ParsedJson; | ||
|
||
if (!apyGroups[validator_address]) { | ||
apyGroups[validator_address] = []; | ||
} | ||
const apyFloat = calculateApy(stake, pool_staking_reward); | ||
|
||
// If the APY is greater than 10000% or isNAN, set it to 0 | ||
apyGroups[validator_address].push( | ||
Number.isNaN(apyFloat) || apyFloat > 10_000 ? 0 : apyFloat | ||
); | ||
}); | ||
|
||
const apyByValidator: ApyByValidator = Object.entries( | ||
apyGroups | ||
).reduce((acc, [validatorAddr, apyArr]) => { | ||
const apys = apyArr | ||
.slice(0, ROLLING_AVERAGE) | ||
.map((entry) => entry); | ||
|
||
const avgApy = | ||
apys.reduce((sum, apy) => sum + apy, 0) / apys.length; | ||
acc[validatorAddr] = roundFloat(avgApy, DEFAULT_APY_DECIMALS); | ||
return acc; | ||
}, {} as ApyByValidator); | ||
// return object with validator address as key and APY as value | ||
// { '0x123': 0.1234, '0x456': 0.4567 } | ||
return apyByValidator; | ||
}, [validatorEpochEvents.data]) || null; | ||
|
||
return { | ||
...validatorEpochEvents, | ||
data: apyByValidator, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.