diff --git a/src/calculators/index.js b/src/calculators/index.js index 1914de0..1dcf7a1 100644 --- a/src/calculators/index.js +++ b/src/calculators/index.js @@ -1,3 +1,4 @@ export * from './circulation-supply.js'; export * from './tokens-sent-from-inflation-pool.js'; export * from './total-supply.js'; +export * from './staking-roi.js'; \ No newline at end of file diff --git a/src/calculators/staking-roi.js b/src/calculators/staking-roi.js new file mode 100644 index 0000000..db658bc --- /dev/null +++ b/src/calculators/staking-roi.js @@ -0,0 +1,16 @@ +import { api } from '../node.js'; +import { totalSupply } from './total-supply.js'; +import { DECIMALS } from '../consts.js'; + +// ROI = era_payout / total_staked * eras_per_year +// era_payout = total_issuance * inflation_per_era + +export async function stakingRoi() { + const lastEra = (await api.query.staking.currentEra()).toHuman() - 1; + + const inflationPerEra = (await api.query.staking.erasValidatorReward(lastEra)).toJSON(); + const totalStaked = await api.query.staking.erasTotalStake(lastEra); + const totalIssuance = await totalSupply(); + + return (totalIssuance * (inflationPerEra / 10 ** DECIMALS)) / (totalStaked / 10 ** DECIMALS) * 730; +} diff --git a/src/server.js b/src/server.js index d5dcdac..53cf9a5 100644 --- a/src/server.js +++ b/src/server.js @@ -8,6 +8,7 @@ import { totalStaking, totalSupply, totalVesting, + stakingRoi } from './calculators/index.js'; const app = express(); @@ -59,6 +60,15 @@ app.get('/api/circulating-supply', async (req, res) => { }); }); +app.get('/api/roi', async (req, res) => { + stakingRoi() + .then((result) => res.json(result)) + .catch((err) => { + console.error(err); + res.sendStatus(500); + }); +}); + export function runServer() { app.listen(config.server.port, () => console.log(`App is running on port ${config.server.port}`)); }