Skip to content

Commit

Permalink
feat: improve speed to get stats
Browse files Browse the repository at this point in the history
  • Loading branch information
forbesus committed Oct 2, 2024
1 parent 8928a68 commit ee40d6b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
19 changes: 9 additions & 10 deletions apps/api/src/routes/v1/dashboardData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,25 @@ const route = createRoute({
});

export default new OpenAPIHono().openapi(route, async c => {
const chainStatsQuery = await runOrLog(
getChainStats,
{
const [{ now, compare }, chainStatsQuery, networkCapacity, networkCapacityStats, latestBlocks, latestTransactions] = await Promise.all([
runOrLog(getDashboardData),
runOrLog(getChainStats, {
bondedTokens: undefined,
totalSupply: undefined,
communityPool: undefined,
inflation: undefined,
stakingAPR: undefined
}
);
const { now, compare } = await runOrLog(getDashboardData)
const networkCapacity = await runOrLog(getNetworkCapacity);
const networkCapacityStats = await runOrLog(() => getProviderGraphData("count"));
const latestBlocks = await runOrLog(() => getBlocks(5));
}),
runOrLog(getNetworkCapacity),
runOrLog(() => getProviderGraphData("count")),
runOrLog(() => getBlocks(5)),
runOrLog(() => getTransactions(5))
]);
const chainStats = {
...chainStatsQuery,
height: latestBlocks && latestBlocks.length > 0 ? latestBlocks[0].height : undefined,
transactionCount: latestBlocks && latestBlocks.length > 0 ? latestBlocks[0].totalTransactionCount : undefined,
}
const latestTransactions = await runOrLog(() => getTransactions(5));

return c.json({
chainStats,
Expand Down
28 changes: 18 additions & 10 deletions apps/api/src/services/external/apiNodeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,44 +41,52 @@ export async function getChainStats() {
60 * 5, // 5 minutes
cacheKeys.getChainStats,
async () => {
const bondedTokens = await runOrLog(async () => {
const bondedTokensAsPromised = await runOrLog(async () => {
const bondedTokensQuery = await axios.get<CosmosStakingPoolResponse>(
`${apiNodeUrl}/cosmos/staking/v1beta1/pool`
);
return parseInt(bondedTokensQuery.data.pool.bonded_tokens);
})
});

const totalSupply = await runOrLog(async () => {
const totalSupplyAsPromised = await runOrLog(async () => {
const supplyQuery = await axios.get<CosmosBankSupplyResponse>(
`${apiNodeUrl}/cosmos/bank/v1beta1/supply?pagination.limit=1000`
);
return parseInt(
supplyQuery.data.supply.find((x) => x.denom === "uakt")?.amount || "0"
);
})
});

const communityPool = await runOrLog(async () => {
const communityPoolAsPromised = await runOrLog(async () => {
const communityPoolQuery = await axios.get<CosmosDistributionCommunityPoolResponse>(
`${apiNodeUrl}/cosmos/distribution/v1beta1/community_pool`
);
return parseFloat(
communityPoolQuery.data.pool.find((x) => x.denom === "uakt")?.amount || "0"
);
})
});

const inflation = await runOrLog(async () => {
const inflationAsPromised = await runOrLog(async () => {
const inflationQuery = await axios.get<CosmosMintInflationResponse>(
`${apiNodeUrl}/cosmos/mint/v1beta1/inflation`
);
return parseFloat(inflationQuery.data.inflation || "0");
})
});

const communityTax = await runOrLog(async () => {
const communityTaxAsPromised = await runOrLog(async () => {
const distributionQuery = await axios.get<CosmosDistributionParamsResponse>(
`${apiNodeUrl}/cosmos/distribution/v1beta1/params`
);
return parseFloat(distributionQuery.data.params.community_tax || "0");
})
});

const [bondedTokens, totalSupply, communityPool, inflation, communityTax] = await Promise.all([
bondedTokensAsPromised,
totalSupplyAsPromised,
communityPoolAsPromised,
inflationAsPromised,
communityTaxAsPromised
]);

return {
communityPool,
Expand Down

0 comments on commit ee40d6b

Please sign in to comment.