Skip to content

Commit

Permalink
Merge pull request #25 from pendulum-chain/24-create-totaltransferabl…
Browse files Browse the repository at this point in the history
…etotallockedtotalreserved-api

24 create total transferable, total locked, totalreserved endpoints
  • Loading branch information
gianfra-t authored Sep 28, 2023
2 parents a680b03 + cca4172 commit f13c046
Show file tree
Hide file tree
Showing 4 changed files with 7,300 additions and 10,114 deletions.
44 changes: 32 additions & 12 deletions src/api/controllers/token.controller.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
const { ApiPromise, WsProvider } = require("@polkadot/api");
const memcached = require("../../config/memcached");
const { amplitudeWss, pendulumWss } = require("../../config/vars");
const { executeApiCall } = require("../services/rpc.service");

async function fetchTokenStats(network) {
console.log(`Fetching token stats for network ${network}`);

const websocketUrl = network === "amplitude" ? amplitudeWss : pendulumWss;

const wsProvider = new WsProvider(websocketUrl);
console.log(`Connecting to node ${websocketUrl}...`);
const api = await ApiPromise.create({
provider: wsProvider,
noInitWarn: true,
});
console.log(`Connected to node ${websocketUrl}`);

const accounts = await api.query.system.account.entries();
const accounts = await executeApiCall(network, api => api.query.system.account.entries());

let totalIssuance = BigInt(0);
let totalTransferable = BigInt(0);
Expand Down Expand Up @@ -128,3 +118,33 @@ exports.getTotalIssuance = async (req, res, next) => {
res.json(stats.totalIssuance);
});
};

/**
* Get token totalTransferable
* @public
*/
exports.getTotalTransferable = async (req, res, next) => {
tryGetTokenStats({ req, res, next }, (stats) => {
res.json(stats.totalTransferable);
});
};

/**
* Get token totalLocked
* @public
*/
exports.getTotalLocked = async (req, res, next) => {
tryGetTokenStats({ req, res, next }, (stats) => {
res.json(stats.totalLocked);
});
};

/**
* Get token totalReserved
* @public
*/
exports.getTotalReserved = async (req, res, next) => {
tryGetTokenStats({ req, res, next }, (stats) => {
res.json(stats.totalReserved);
});
};
38 changes: 37 additions & 1 deletion src/api/routes/v1/stats.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ router
*/
.get(controller.get);

router
router
.route("/totalIssuance")
/**
* @api {get} token/stats/totalIssuance List token stats totalIssuance
Expand All @@ -27,4 +27,40 @@ router
*/
.get(controller.getTotalIssuance);

router
.route("/totalTransferable")
/**
* @api {get} token/stats/totalTransferable List token stats totalTransferable
* @apiDescription Get the current token stats totalTransferable
* @apiVersion 1.0.0
* @apiName ListTokenStatsTotalTransferable
*
* @apiSuccess {Object[]} tokenStats List of token stats totalTransferable.
*/
.get(controller.getTotalTransferable);

router
.route("/totalLocked")
/**
* @api {get} token/stats/totalLoked List token stats totalLocked
* @apiDescription Get the current token stats totalLocked
* @apiVersion 1.0.0
* @apiName ListTokenStatsTotalLocked
*
* @apiSuccess {Object[]} tokenStats List of token stats totalLocked.
*/
.get(controller.getTotalLocked);

router
.route("/totalReserved")
/**
* @api {get} token/stats/totalReserved List token stats totalReserved
* @apiDescription Get the current token stats totalReserved
* @apiVersion 1.0.0
* @apiName ListTokenStatsTotalReserved
*
* @apiSuccess {Object[]} tokenStats List of token stats totalReserved.
*/
.get(controller.getTotalReserved);

module.exports = router;
48 changes: 48 additions & 0 deletions src/api/services/rpc.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { ApiPromise, WsProvider } = require("@polkadot/api");
const { amplitudeWss, pendulumWss } = require("../../config/vars");

const apiInstanceDict = {};

const connectApi = async (socketUrl) => {

const wsProvider = new WsProvider(socketUrl);
return ApiPromise.create({ provider: wsProvider, noInitWarn: true });

}

const getApi = async (network) => {

const websocketUrl = network === "amplitude" ? amplitudeWss : pendulumWss;

if (!apiInstanceDict[network]) {
console.log(`Connecting to node ${websocketUrl}...`);
apiInstanceDict[network] = await connectApi(websocketUrl);
console.log(`Connected to node ${websocketUrl}`);
}

return apiInstanceDict[network];
};

const executeApiCall = async (network, apiCall) => {

const apiInstance = await getApi(network);

const websocketUrl = network === "amplitude" ? amplitudeWss : pendulumWss;

try {
return await apiCall(apiInstance);
} catch (initialError) {
try {
console.log(`Attempting to reconnect to node ${websocketUrl}...`);
apiInstanceDict[network] = await connectApi(websocketUrl);
console.log(`Reconnected to node ${websocketUrl}`);

return await apiCall(apiInstance);
} catch (reconnectError) {
console.error('Failed to reconnect and execute the API call', reconnectError);
throw initialError;
}
}
};

exports.executeApiCall = executeApiCall;
Loading

0 comments on commit f13c046

Please sign in to comment.