forked from Cardinal-Cryptography/aleph-zero-dashboard
-
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.
Merge pull request Cardinal-Cryptography#81 from Cardinal-Cryptograph…
…y/L1-287-update-the-front-end-to-inflation-changes L1-287: Update the front-end to inflation changes
- Loading branch information
Showing
13 changed files
with
127 additions
and
66 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
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
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,22 @@ | ||
const typesBundle = { | ||
spec: { | ||
'aleph-node': { | ||
runtime: { | ||
AlephSessionApi: [ | ||
{ | ||
methods: { | ||
yearly_inflation: { | ||
description: 'Returns inflation from now to now + one year.', | ||
params: [], | ||
type: 'Perbill', | ||
}, | ||
}, | ||
version: 1, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default typesBundle; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,65 @@ | ||
// Copyright 2023 @paritytech/polkadot-staking-dashboard authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import BigNumber from 'bignumber.js'; | ||
import type BigNumber from 'bignumber.js'; | ||
import { useApi } from 'contexts/Api'; | ||
import { useNetworkMetrics } from 'contexts/Network'; | ||
import { useStaking } from 'contexts/Staking'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
const BIGNUMBER_THOUSAND = new BigNumber(1_000); | ||
const BIGNUMBER_MILLION = new BigNumber(1_000_000); | ||
const BIGNUMBER_BILLION = new BigNumber(1_000_000_000); | ||
|
||
export const useInflation = () => { | ||
const { network } = useApi(); | ||
const { metrics } = useNetworkMetrics(); | ||
const { staking } = useStaking(); | ||
const { params } = network; | ||
const { lastTotalStake } = staking; | ||
const { totalIssuance, auctionCounter } = metrics; | ||
|
||
const { | ||
auctionAdjust, | ||
auctionMax, | ||
maxInflation, | ||
stakeTarget, | ||
yearlyInflationInTokens, | ||
} = params; | ||
const calculateInflation = ( | ||
totalStaked: BigNumber, | ||
totalIssuance: BigNumber, | ||
yearlyInflationInPercentage: number | ||
) => { | ||
const stakedFraction = | ||
totalStaked.isZero() || totalIssuance.isZero() | ||
? 0 | ||
: totalStaked.dividedBy(totalIssuance).toNumber(); | ||
|
||
const baseStakedReturn = | ||
stakedFraction !== 0 ? yearlyInflationInPercentage / stakedFraction : 0; | ||
/* For Aleph Zero inflation is calculated based on yearlyInflationInTokens and totalIssuanceInTokens | ||
* We multiply stakedReturn by 0.9, as in case of Aleph Zero chain 10% of return goes to treasury | ||
*/ | ||
const stakedReturn = baseStakedReturn * 0.9; | ||
|
||
return { | ||
inflation: yearlyInflationInPercentage, | ||
stakedFraction, | ||
stakedReturn, | ||
}; | ||
}; | ||
|
||
const calculateInflation = ( | ||
totalStaked: BigNumber, | ||
numAuctions: BigNumber | ||
) => { | ||
const stakedFraction = | ||
totalStaked.isZero() || totalIssuance.isZero() | ||
? 0 | ||
: totalStaked | ||
.multipliedBy(BIGNUMBER_MILLION) | ||
.dividedBy(totalIssuance) | ||
.toNumber() / BIGNUMBER_MILLION.toNumber(); | ||
const idealStake = | ||
stakeTarget - | ||
Math.min(auctionMax, numAuctions.toNumber()) * auctionAdjust; | ||
const idealInterest = maxInflation / idealStake; | ||
const useYearlyInflation = () => { | ||
const { api } = useApi(); | ||
const [yearlyInflation, setYearlyInflation] = useState<number>(); | ||
|
||
const totalIssuanceInTokens = totalIssuance | ||
.div(BIGNUMBER_BILLION) | ||
.div(BIGNUMBER_THOUSAND); | ||
const getYearlyInflation = api?.call?.alephSessionApi?.yearlyInflation; | ||
|
||
const inflation = totalIssuanceInTokens.isZero() | ||
? 0 | ||
: 100 * (yearlyInflationInTokens / totalIssuanceInTokens.toNumber()); | ||
useEffect(() => { | ||
getYearlyInflation?.() | ||
.then((val) => setYearlyInflation(val.toNumber() / 1_000_000_000)) | ||
// eslint-disable-next-line no-console | ||
.catch(console.error); | ||
// `api` object can change in case of network change which should trigger refetch. | ||
}, [api]); | ||
|
||
let stakedReturn = stakedFraction ? inflation / stakedFraction : 0; | ||
stakedReturn *= 0.9; | ||
return yearlyInflation; | ||
}; | ||
|
||
return { | ||
idealInterest, | ||
idealStake, | ||
inflation, | ||
stakedFraction, | ||
stakedReturn, | ||
}; | ||
}; | ||
export const useInflation = () => { | ||
const { | ||
metrics: { totalIssuance }, | ||
} = useNetworkMetrics(); | ||
const { | ||
staking: { lastTotalStake }, | ||
} = useStaking(); | ||
const yearlyInflation = useYearlyInflation(); | ||
|
||
return calculateInflation(lastTotalStake, auctionCounter); | ||
return calculateInflation( | ||
lastTotalStake, | ||
totalIssuance, | ||
(yearlyInflation ?? 0) * 100 | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { | ||
ApiTypes, | ||
AugmentedCall, | ||
DecoratedCallBase, | ||
} from '@polkadot/api-base/types'; | ||
import type { Perbill } from '@polkadot/types/interfaces/runtime'; | ||
import type { Observable } from '@polkadot/types/types'; | ||
|
||
declare module '@polkadot/api-base/types/calls' { | ||
interface AugmentedCalls<ApiType extends ApiTypes> { | ||
/** 0xbc9d89904f5b923f/1 */ | ||
alephSessionApi?: { | ||
/** | ||
* The API to query account nonce (aka transaction index) | ||
* */ | ||
yearlyInflation?: AugmentedCall<ApiType, () => Observable<Perbill>>; | ||
/** | ||
* Generic call | ||
* */ | ||
[key: string]: DecoratedCallBase<ApiType> | undefined; | ||
}; | ||
} | ||
} |