Skip to content

Commit

Permalink
roi calc (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
EugenWay authored Jan 10, 2024
1 parent 47c1711 commit b1abc2f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/calculators/index.js
Original file line number Diff line number Diff line change
@@ -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';
16 changes: 16 additions & 0 deletions src/calculators/staking-roi.js
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 10 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
totalStaking,
totalSupply,
totalVesting,
stakingRoi
} from './calculators/index.js';

const app = express();
Expand Down Expand Up @@ -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}`));
}

0 comments on commit b1abc2f

Please sign in to comment.