Skip to content

Commit

Permalink
Merge pull request #26 from pendulum-chain/circulating-supply-endpoint
Browse files Browse the repository at this point in the history
Add circulating supply endpoint
  • Loading branch information
gianfra-t authored Mar 1, 2024
2 parents f13c046 + 17cfcff commit 5d50242
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/api/controllers/token.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const memcached = require("../../config/memcached");
const { executeApiCall } = require("../services/rpc.service");
const {ACCOUNTS_TO_SUBTRACT_AMPLITUDE, ACCOUNTS_TO_SUBTRACT_PENDULUM} = require("../utils/constants");

async function fetchTokenStats(network) {
console.log(`Fetching token stats for network ${network}`);
Expand All @@ -11,15 +12,35 @@ async function fetchTokenStats(network) {
let totalTransferable = BigInt(0);
let totalLocked = BigInt(0);
let totalReserved = BigInt(0);
let supplyToIgnore = BigInt(0);

accounts.forEach((entry) => {
const account = entry[0].toHuman()[0]
const balances = entry[1].toHuman().data;
const miscFrozen = BigInt(balances.miscFrozen.replace(/,/g, ""));
const feeFrozen = BigInt(balances.feeFrozen.replace(/,/g, ""));
const frozen = miscFrozen > feeFrozen ? miscFrozen : feeFrozen;
const free = BigInt(balances.free.replace(/,/g, ""));
const reserved = BigInt(balances.reserved.replace(/,/g, ""));

// We mantain the supplyToIgnore to subtract it from the total transferable
let accountsToSubtract = [];
if (network === "amplitude") {
accountsToSubtract = ACCOUNTS_TO_SUBTRACT_AMPLITUDE;
} else if (network === "pendulum") {
accountsToSubtract = ACCOUNTS_TO_SUBTRACT_PENDULUM;
} else {
console.error("Invalid network");
}

// We define the circulating supply as the total transferable (free - frozen) minus
// the total transferable of a set of predefined multisig accounts (https://github.com/pendulum-chain/tasks/issues/242)
// We keep track of the transferable of these accounts
// which will then be subtracted from the total transferable
if (accountsToSubtract.includes(String(account))) {
supplyToIgnore += free - frozen;
}

totalIssuance += free + reserved;
totalTransferable += free - frozen;
totalLocked += frozen;
Expand All @@ -43,6 +64,7 @@ async function fetchTokenStats(network) {
totalTransferable: format(totalTransferable),
totalLocked: format(totalLocked),
totalReserved: format(totalReserved),
totalCirculating: format(totalTransferable-supplyToIgnore),
};
}

Expand Down Expand Up @@ -148,3 +170,13 @@ exports.getTotalReserved = async (req, res, next) => {
res.json(stats.totalReserved);
});
};

/**
* Get token cirulating supply
* @public
*/
exports.getCirculatingSupply = async (req, res, next) => {
tryGetTokenStats({ req, res, next }, (stats) => {
res.json(stats.totalCirculating);
});
};
13 changes: 13 additions & 0 deletions src/api/routes/v1/stats.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,17 @@ router
*/
.get(controller.getTotalReserved);

router
.route("/circulatingsupply")
/**
* @api {get} token/stats/circulatingsupply Token circulating supply
* @apiDescription Get the current token stats for adjusted circulating supply
* @apiVersion 1.0.0
* @apiName ListTokenStatsCirculatingSupply
*
* @apiSuccess {Object[]} tokenStats List of token stats total adjusted circulating supply.
*/
.get(controller.getCirculatingSupply);

module.exports = router;

21 changes: 21 additions & 0 deletions src/api/utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const ACCOUNTS_TO_SUBTRACT_PENDULUM = [
"6cY3Zrb2gr1xt3BczzJ3xoMpF7UyrcGNfR3cjkjcF7auq2Y9",
"6gfLdZvfW2w6fDaPpVfUs53W8Aay17s1bjwcFaqDaBVt7Muo",
"6biLQnLREwRd9aSPiN9xxR2UDCPa1XL3ZSwqNUxNEr3QvGDk",
"6eiGivQB9dtQUMs1VpxATipDYrewWSr4kGsvgjELgqnvRYyx",
"6gKuTtzLBtgYyW3SP6jh7DnXbNU8fDVFG2AxHCLbGYqaspe7",
"6cGTMDwqhePxUw5Fgfz8zHhJUhW7xkLRASSt6cwUmbSL137U",
"6gB6fuaAazhhctdcEfUmQDqHkzYBsdSHFoe7TAv8wSNdAgMx",
"6cABSUjhvnGP6fHrs8LRtzbvrvG9o9AtA92aakSSYpPtjP58",
"6gKxy1JM3FeUU9SB9BtQEzH8XTTZBPmyEjYYJBVFae23je5h",
"6eS2NM9rpKgM7HKVc5aksfSfyMqLWbKmqiavxQ9wxfvDnN7Y",
"6cE1nHQu9XRH6pUSjMejCUzF39XqFx3HwgZNTMHNTZZw2c7M",
]

const ACCOUNTS_TO_SUBTRACT_AMPLITUDE = [
]

module.exports = {
ACCOUNTS_TO_SUBTRACT_PENDULUM,
ACCOUNTS_TO_SUBTRACT_AMPLITUDE,
}

0 comments on commit 5d50242

Please sign in to comment.