Skip to content

Commit

Permalink
Merge pull request #1 from gear-foundation/do-calculate-staking
Browse files Browse the repository at this point in the history
calculate staking
  • Loading branch information
EugenWay authored Sep 19, 2023
2 parents 37d5744 + 0f95598 commit 5c4130d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 28 deletions.
59 changes: 43 additions & 16 deletions src/calculators/circulation-supply.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { api, deriveAddr, getBalances } from '../node.js';
import { api, getBalances } from '../node.js';
import {
AIRDROP_POOL,
DEVELOPER_PROJECTS_GRANTS_POOL,
Expand All @@ -8,9 +8,13 @@ import {
PROTOCOL_DEVELOPMENT_POOL,
PROTOCOL_RESERVE_POOL,
VALIDATOR_INCENTIVES_POOL,
MARKET_POOL,
CUSTODY,
} from '../consts.js';
import { totalSupply } from './total-supply.js';

const STAKING_HEX = '0x7374616b696e6720';

async function getKeys(prefix) {
const result = await api.rpc.state.getKeysPaged(prefix, 1000, prefix);
if (result.length === 1000) {
Expand All @@ -21,7 +25,7 @@ async function getKeys(prefix) {
}

// Get all vested tokens from the chain
async function vestingTotal() {
export async function totalVesting() {
const prefix = api.query.vesting.vesting.keyPrefix();
const keys = await getKeys(prefix);

Expand All @@ -41,30 +45,53 @@ async function vestingTotal() {
return Number(totalVesting);
}

export async function totalStaking() {
const prefix = api.query.balances.locks.keyPrefix();
const keys = await getKeys(prefix);
const allLocks = await api.rpc.state.queryStorageAt(keys);

let total = BigInt(0);

for (const lock of allLocks) {
const withType = api.registry.createType('PalletBalancesBalanceLock', lock.toU8a().slice(2));

if (withType.id.toHex() == STAKING_HEX) {
total += withType.amount.toBigInt();
}
}
const totalStaking = total / BigInt(10 ** 12);
return Number(totalStaking);
}

// Get all pool addresses
// Each pool additionally has 10 derived accounts (indexes from 1 to 10)
function getPoolAddresses(poolAddr) {
return [poolAddr, ...deriveAddr(poolAddr)];
}
// function getPoolAddresses(poolAddr) {
// return [poolAddr, ...deriveAddr(poolAddr)];
// }

// Circulation Supply = Total Supply - (Vesting + Pools);
export async function circulationSupply() {
const addresses = [
...getPoolAddresses(EDUCATION_BOOTCAMP_PR_EVENT_POOL),
...getPoolAddresses(PROTOCOL_RESERVE_POOL),
...getPoolAddresses(FOUNDATION_AND_ECOSYSTEM_DEVELOPMENT_POOL),
...getPoolAddresses(PROTOCOL_DEVELOPMENT_POOL),
...getPoolAddresses(VALIDATOR_INCENTIVES_POOL),
...getPoolAddresses(DEVELOPER_PROJECTS_GRANTS_POOL),
...getPoolAddresses(AIRDROP_POOL),
EDUCATION_BOOTCAMP_PR_EVENT_POOL,
PROTOCOL_RESERVE_POOL,
FOUNDATION_AND_ECOSYSTEM_DEVELOPMENT_POOL,
PROTOCOL_DEVELOPMENT_POOL,
VALIDATOR_INCENTIVES_POOL,
DEVELOPER_PROJECTS_GRANTS_POOL,
AIRDROP_POOL,
INFLATION_OFFSETTING_POOL,
MARKET_POOL,
...CUSTODY,
];

const [vesting, pools, supply] = await Promise.all([vestingTotal(), getBalances(addresses), totalSupply()]);
console.log(addresses);
console.log(pools);
const [supply, vesting, staking, pools] = await Promise.all([
totalSupply(),
totalVesting(),
totalStaking(),
getBalances(addresses),
]);

const total = pools.reduce((accumulator, current) => accumulator + current, 0) + vesting;
const total = pools.reduce((accumulator, current) => accumulator + current, 0) + vesting + staking;

return supply - total;
}
22 changes: 11 additions & 11 deletions src/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export const INITIAL_BALANCE_INFLATION_OFFSETTING_POOL = 1_000_000_000;
// Custody accounts

export const CUSTODY = [
kGhBnmPPy7yyyDG7ixg4JVsuMmGTt7wsKYstmUhRB4RkK1ZRq,
kGkfMUpUwQ99tP3Swu1zen5gU44Yf5NjK1xSsnwi5CRaik8Kz,
kGjj96gJyxuPpG2BekrcaAMBHwfHrArzv9a2urP5GfpLoAoqu,
kGiGZ5Sa6hrxiZxZ8xD95thnnkdtfK9yy54NB7Ad4oF8DzyLp,
kGjd29r3qa9rnUQSGYHh3TiFqu8tYv6FX5am6nQ5pMRirwm7d,
kGhKd4t4oSpH8KpiTYcx1NSVvvRf2bSz16z3yVaYfNYfdcYba,
kGgvxgM2sJF7fWUze3fNWBWzy5momsyna7XF8MFzAWPhj2WpU,
kGi7MJ14UdQZx9bqXZ41NC5j21iBNNCTM2wgdMMKAHkCjtFeG,
kGga7DgxzLLTqn9WjtEZW5pkxYVnBPS4Rt6xK3Adqs1iKN42z,
kGiVY7G1mJkqaAjKzLnRmwCy5GcvuGQvG5mUtojhVHdLfBd1P,
]
'kGhBnmPPy7yyyDG7ixg4JVsuMmGTt7wsKYstmUhRB4RkK1ZRq',
'kGkfMUpUwQ99tP3Swu1zen5gU44Yf5NjK1xSsnwi5CRaik8Kz',
'kGjj96gJyxuPpG2BekrcaAMBHwfHrArzv9a2urP5GfpLoAoqu',
'kGiGZ5Sa6hrxiZxZ8xD95thnnkdtfK9yy54NB7Ad4oF8DzyLp',
'kGjd29r3qa9rnUQSGYHh3TiFqu8tYv6FX5am6nQ5pMRirwm7d',
'kGhKd4t4oSpH8KpiTYcx1NSVvvRf2bSz16z3yVaYfNYfdcYba',
'kGgvxgM2sJF7fWUze3fNWBWzy5momsyna7XF8MFzAWPhj2WpU',
'kGi7MJ14UdQZx9bqXZ41NC5j21iBNNCTM2wgdMMKAHkCjtFeG',
'kGga7DgxzLLTqn9WjtEZW5pkxYVnBPS4Rt6xK3Adqs1iKN42z',
'kGiVY7G1mJkqaAjKzLnRmwCy5GcvuGQvG5mUtojhVHdLfBd1P',
];
26 changes: 25 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import express from 'express';
import cors from 'cors';

import config from './config.js';
import { circulationSupply, tokensSentFromInflationPool, totalSupply } from './calculators/index.js';
import {
circulationSupply,
tokensSentFromInflationPool,
totalStaking,
totalSupply,
totalVesting,
} from './calculators/index.js';

const app = express();

Expand All @@ -26,6 +32,24 @@ app.get('/api/total', async (req, res) => {
});
});

app.get('/api/total-staking', async (req, res) => {
totalStaking()
.then((result) => res.json(result))
.catch((err) => {
console.error(err);
res.sendStatus(500);
});
});

app.get('/api/total-vesting', async (req, res) => {
totalVesting()
.then((result) => res.json(result))
.catch((err) => {
console.error(err);
res.sendStatus(500);
});
});

app.get('/api/circulating-supply', async (req, res) => {
circulationSupply()
.then((result) => res.json(result))
Expand Down

0 comments on commit 5c4130d

Please sign in to comment.