mirror of
https://github.com/pezkuwichain/pezkuwi-subquery.git
synced 2026-04-22 05:27:57 +00:00
style: fix prettier formatting across mapping files
This commit is contained in:
@@ -123,7 +123,9 @@ async function propagateVoteToDelegators(
|
|||||||
cv: CastingVoting,
|
cv: CastingVoting,
|
||||||
referendumId: string,
|
referendumId: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const delegations = await Delegation.getByDelegateId(delegateAddress, { limit: 500 });
|
const delegations = await Delegation.getByDelegateId(delegateAddress, {
|
||||||
|
limit: 500,
|
||||||
|
});
|
||||||
if (!delegations || delegations.length === 0) return;
|
if (!delegations || delegations.length === 0) return;
|
||||||
|
|
||||||
const referendum = await Referendum.get(referendumId);
|
const referendum = await Referendum.get(referendumId);
|
||||||
@@ -146,7 +148,9 @@ async function propagateVoteToDelegators(
|
|||||||
async function createDelegatorVotingsForNewDelegation(
|
async function createDelegatorVotingsForNewDelegation(
|
||||||
delegation: Delegation,
|
delegation: Delegation,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const cvList = await CastingVoting.getByVoter(delegation.delegateId, { limit: 500 });
|
const cvList = await CastingVoting.getByVoter(delegation.delegateId, {
|
||||||
|
limit: 500,
|
||||||
|
});
|
||||||
if (!cvList || cvList.length === 0) return;
|
if (!cvList || cvList.length === 0) return;
|
||||||
|
|
||||||
for (const cv of cvList) {
|
for (const cv of cvList) {
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ export async function handleRelayBlock(block: SubstrateBlock): Promise<void> {
|
|||||||
const activeValidators = new Set<string>();
|
const activeValidators = new Set<string>();
|
||||||
|
|
||||||
// Read all validators from overview (includes validators with only self-stake)
|
// Read all validators from overview (includes validators with only self-stake)
|
||||||
const overviews = await api.query.staking.erasStakersOverview.entries(currentEra);
|
const overviews =
|
||||||
|
await api.query.staking.erasStakersOverview.entries(currentEra);
|
||||||
for (const [key, ov] of overviews) {
|
for (const [key, ov] of overviews) {
|
||||||
const [, validatorId] = key.args;
|
const [, validatorId] = key.args;
|
||||||
activeValidators.add(validatorId.toString());
|
activeValidators.add(validatorId.toString());
|
||||||
@@ -72,7 +73,8 @@ export async function handleRelayBlock(block: SubstrateBlock): Promise<void> {
|
|||||||
|
|
||||||
// Fallback: if overview had no results, try legacy erasStakersClipped
|
// Fallback: if overview had no results, try legacy erasStakersClipped
|
||||||
if (activeValidators.size === 0) {
|
if (activeValidators.size === 0) {
|
||||||
const clipped = await api.query.staking.erasStakersClipped.entries(currentEra);
|
const clipped =
|
||||||
|
await api.query.staking.erasStakersClipped.entries(currentEra);
|
||||||
for (const [key, exposure] of clipped) {
|
for (const [key, exposure] of clipped) {
|
||||||
const [, validatorId] = key.args;
|
const [, validatorId] = key.args;
|
||||||
activeValidators.add(validatorId.toString());
|
activeValidators.add(validatorId.toString());
|
||||||
|
|||||||
+45
-18
@@ -99,9 +99,7 @@ export async function handleBlock(block: SubstrateBlock): Promise<void> {
|
|||||||
*
|
*
|
||||||
* Event data: [member: AccountId, pool_id: u32, bonded: Balance, joined: bool]
|
* Event data: [member: AccountId, pool_id: u32, bonded: Balance, joined: bool]
|
||||||
*/
|
*/
|
||||||
export async function handlePoolBonded(
|
export async function handlePoolBonded(event: SubstrateEvent): Promise<void> {
|
||||||
event: SubstrateEvent,
|
|
||||||
): Promise<void> {
|
|
||||||
const {
|
const {
|
||||||
event: {
|
event: {
|
||||||
data: [, poolIdEncoded],
|
data: [, poolIdEncoded],
|
||||||
@@ -130,9 +128,7 @@ export async function handlePoolBonded(
|
|||||||
*
|
*
|
||||||
* Event data: [member: AccountId, pool_id: u32, balance: Balance, points: Balance, era: u32]
|
* Event data: [member: AccountId, pool_id: u32, balance: Balance, points: Balance, era: u32]
|
||||||
*/
|
*/
|
||||||
export async function handlePoolUnbonded(
|
export async function handlePoolUnbonded(event: SubstrateEvent): Promise<void> {
|
||||||
event: SubstrateEvent,
|
|
||||||
): Promise<void> {
|
|
||||||
const {
|
const {
|
||||||
event: {
|
event: {
|
||||||
data: [, poolIdEncoded],
|
data: [, poolIdEncoded],
|
||||||
@@ -160,16 +156,24 @@ function calculateYearlyInflation(stakedPortion: number): number {
|
|||||||
const idealStake = INFLATION_STAKE_TARGET;
|
const idealStake = INFLATION_STAKE_TARGET;
|
||||||
const idealInterest = INFLATION_MAX / idealStake;
|
const idealInterest = INFLATION_MAX / idealStake;
|
||||||
if (stakedPortion >= 0 && stakedPortion <= idealStake) {
|
if (stakedPortion >= 0 && stakedPortion <= idealStake) {
|
||||||
return INFLATION_MIN + stakedPortion * (idealInterest - INFLATION_MIN / idealStake);
|
return (
|
||||||
|
INFLATION_MIN +
|
||||||
|
stakedPortion * (idealInterest - INFLATION_MIN / idealStake)
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
return INFLATION_MIN + (idealInterest * idealStake - INFLATION_MIN) *
|
return (
|
||||||
Math.pow(2, (idealStake - stakedPortion) / INFLATION_FALLOFF);
|
INFLATION_MIN +
|
||||||
|
(idealInterest * idealStake - INFLATION_MIN) *
|
||||||
|
Math.pow(2, (idealStake - stakedPortion) / INFLATION_FALLOFF)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function computeAndSaveAPY(): Promise<void> {
|
async function computeAndSaveAPY(): Promise<void> {
|
||||||
// Use AH's own totalIssuance. AH staking pallet mints inflation from AH supply.
|
// Use AH's own totalIssuance. AH staking pallet mints inflation from AH supply.
|
||||||
const TOTAL_SUPPLY = ((await api.query.balances.totalIssuance()) as any).toBigInt();
|
const TOTAL_SUPPLY = (
|
||||||
|
(await api.query.balances.totalIssuance()) as any
|
||||||
|
).toBigInt();
|
||||||
if (TOTAL_SUPPLY === BigInt(0)) return;
|
if (TOTAL_SUPPLY === BigInt(0)) return;
|
||||||
|
|
||||||
const activeEraOpt = (await api.query.staking.activeEra()) as Option<any>;
|
const activeEraOpt = (await api.query.staking.activeEra()) as Option<any>;
|
||||||
@@ -177,7 +181,8 @@ async function computeAndSaveAPY(): Promise<void> {
|
|||||||
const currentEra = activeEraOpt.unwrap().index.toNumber();
|
const currentEra = activeEraOpt.unwrap().index.toNumber();
|
||||||
|
|
||||||
// Get all validator exposures for current era
|
// Get all validator exposures for current era
|
||||||
const overviews = await api.query.staking.erasStakersOverview.entries(currentEra);
|
const overviews =
|
||||||
|
await api.query.staking.erasStakersOverview.entries(currentEra);
|
||||||
let totalStaked = BigInt(0);
|
let totalStaked = BigInt(0);
|
||||||
const validators: { totalStake: bigint; commission: number }[] = [];
|
const validators: { totalStake: bigint; commission: number }[] = [];
|
||||||
const validatorAddresses: string[] = [];
|
const validatorAddresses: string[] = [];
|
||||||
@@ -197,12 +202,15 @@ async function computeAndSaveAPY(): Promise<void> {
|
|||||||
const prefs = await api.query.staking.validators.multi(validatorAddresses);
|
const prefs = await api.query.staking.validators.multi(validatorAddresses);
|
||||||
for (let i = 0; i < prefs.length; i++) {
|
for (let i = 0; i < prefs.length; i++) {
|
||||||
const p = prefs[i] as any;
|
const p = prefs[i] as any;
|
||||||
validators[i].commission = p.commission ? Number(p.commission.toString()) / PERBILL_DIVISOR : 0;
|
validators[i].commission = p.commission
|
||||||
|
? Number(p.commission.toString()) / PERBILL_DIVISOR
|
||||||
|
: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate APY using relay total supply
|
// Calculate APY using relay total supply
|
||||||
const SCALE = BigInt(1_000_000_000);
|
const SCALE = BigInt(1_000_000_000);
|
||||||
const stakedPortion = Number((totalStaked * SCALE) / TOTAL_SUPPLY) / Number(SCALE);
|
const stakedPortion =
|
||||||
|
Number((totalStaked * SCALE) / TOTAL_SUPPLY) / Number(SCALE);
|
||||||
const yearlyInflation = calculateYearlyInflation(stakedPortion);
|
const yearlyInflation = calculateYearlyInflation(stakedPortion);
|
||||||
const avgRewardPct = yearlyInflation / stakedPortion;
|
const avgRewardPct = yearlyInflation / stakedPortion;
|
||||||
const avgStake = totalStaked / BigInt(validators.length);
|
const avgStake = totalStaked / BigInt(validators.length);
|
||||||
@@ -213,26 +221,45 @@ async function computeAndSaveAPY(): Promise<void> {
|
|||||||
let maxAPY = 0;
|
let maxAPY = 0;
|
||||||
for (const v of validators) {
|
for (const v of validators) {
|
||||||
if (v.totalStake < minStake) continue;
|
if (v.totalStake < minStake) continue;
|
||||||
const stakeRatio = Number((avgStake * SCALE) / v.totalStake) / Number(SCALE);
|
const stakeRatio =
|
||||||
|
Number((avgStake * SCALE) / v.totalStake) / Number(SCALE);
|
||||||
const apy = avgRewardPct * stakeRatio * (1 - v.commission);
|
const apy = avgRewardPct * stakeRatio * (1 - v.commission);
|
||||||
if (apy > maxAPY) maxAPY = apy;
|
if (apy > maxAPY) maxAPY = apy;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save APY for AH relaychain staking
|
// Save APY for AH relaychain staking
|
||||||
const ahRelayApyId = `${PEZKUWI_ASSET_HUB_GENESIS}-${STAKING_TYPE_RELAYCHAIN}`;
|
const ahRelayApyId = `${PEZKUWI_ASSET_HUB_GENESIS}-${STAKING_TYPE_RELAYCHAIN}`;
|
||||||
await StakingApy.create({ id: ahRelayApyId, networkId: PEZKUWI_ASSET_HUB_GENESIS, stakingType: STAKING_TYPE_RELAYCHAIN, maxAPY }).save();
|
await StakingApy.create({
|
||||||
|
id: ahRelayApyId,
|
||||||
|
networkId: PEZKUWI_ASSET_HUB_GENESIS,
|
||||||
|
stakingType: STAKING_TYPE_RELAYCHAIN,
|
||||||
|
maxAPY,
|
||||||
|
}).save();
|
||||||
|
|
||||||
// Save APY for AH nomination-pool staking
|
// Save APY for AH nomination-pool staking
|
||||||
const ahPoolApyId = `${PEZKUWI_ASSET_HUB_GENESIS}-${STAKING_TYPE_NOMINATION_POOL}`;
|
const ahPoolApyId = `${PEZKUWI_ASSET_HUB_GENESIS}-${STAKING_TYPE_NOMINATION_POOL}`;
|
||||||
await StakingApy.create({ id: ahPoolApyId, networkId: PEZKUWI_ASSET_HUB_GENESIS, stakingType: STAKING_TYPE_NOMINATION_POOL, maxAPY }).save();
|
await StakingApy.create({
|
||||||
|
id: ahPoolApyId,
|
||||||
|
networkId: PEZKUWI_ASSET_HUB_GENESIS,
|
||||||
|
stakingType: STAKING_TYPE_NOMINATION_POOL,
|
||||||
|
maxAPY,
|
||||||
|
}).save();
|
||||||
|
|
||||||
logger.info(`AH APY: ${(maxAPY * 100).toFixed(2)}% from ${validators.length} validators, era ${currentEra}, stakedPortion=${(stakedPortion*100).toFixed(2)}%`);
|
logger.info(
|
||||||
|
`AH APY: ${(maxAPY * 100).toFixed(2)}% from ${
|
||||||
|
validators.length
|
||||||
|
} validators, era ${currentEra}, stakedPortion=${(
|
||||||
|
stakedPortion * 100
|
||||||
|
).toFixed(2)}%`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle staking.StakersElected on Asset Hub - recompute APY each era
|
* Handle staking.StakersElected on Asset Hub - recompute APY each era
|
||||||
*/
|
*/
|
||||||
export async function handleAHStakersElected(event: SubstrateEvent): Promise<void> {
|
export async function handleAHStakersElected(
|
||||||
|
event: SubstrateEvent,
|
||||||
|
): Promise<void> {
|
||||||
await computeAndSaveAPY();
|
await computeAndSaveAPY();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user