Skip to content

Commit

Permalink
Features/refactor akashjs sdl (#1)
Browse files Browse the repository at this point in the history
* added cache to chainStats api endpoint

* use akashjs instead of custom sdl code

* fixes

* re-add types grant

* fix build

* update akashjs
  • Loading branch information
baktun14 authored Aug 16, 2023
1 parent 9d53822 commit 43fe222
Show file tree
Hide file tree
Showing 35 changed files with 165 additions and 1,566 deletions.
1 change: 1 addition & 0 deletions api/src/caching/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const cacheKeys = {
getProviderAttributesSchema: "getProviderAttributesSchema",
getTemplates: "getTemplates",
getAuditors: "getAuditors",
getChainStats: "getChainStats",
getMainnetNodes: "getMainnetNodes",
getTestnetNodes: "getTestnetNodes",
getSandboxNodes: "getSandboxNodes",
Expand Down
58 changes: 34 additions & 24 deletions api/src/providers/apiNodeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,47 @@ import axios from "axios";
import { Validator } from "@shared/dbSchemas/base";
import { Op } from "sequelize";
import { Provider, ProviderAttribute } from "@shared/dbSchemas/akash";
import { cacheKeys, cacheResponse } from "@src/caching/helpers";

const apiNodeUrl = process.env.Network === "testnet" ? "https://api.testnet-02.aksh.pw:443" : "https://rest.cosmos.directory/akash";
const betaTypeVersion = process.env.Network === "testnet" ? "v1beta3" : "v1beta2";

export async function getChainStats() {
const bondedTokensQuery = axios.get(`${apiNodeUrl}/cosmos/staking/v1beta1/pool`);
const supplyQuery = axios.get(`${apiNodeUrl}/cosmos/bank/v1beta1/supply`);
const communityPoolQuery = axios.get(`${apiNodeUrl}/cosmos/distribution/v1beta1/community_pool`);
const inflationQuery = axios.get(`${apiNodeUrl}/cosmos/mint/v1beta1/inflation`);
const distributionQuery = axios.get(`${apiNodeUrl}/cosmos/distribution/v1beta1/params`);

const [bondedTokensResponse, supplyResponse, communityPoolResponse, inflationResponse, distributionResponse] = await Promise.all([
bondedTokensQuery,
supplyQuery,
communityPoolQuery,
inflationQuery,
distributionQuery
]);

const communityPool = parseFloat(communityPoolResponse.data.pool.find((x) => x.denom === "uakt").amount);
const inflation = parseFloat(inflationResponse.data.inflation);
const communityTax = parseFloat(distributionResponse.data.params.community_tax);
const bondedTokens = parseInt(bondedTokensResponse.data.pool.bonded_tokens);
const totalSupply = parseInt(supplyResponse.data.supply.find((x) => x.denom === "uakt").amount);
const result: { communityPool: number; inflation: number; communityTax: number; bondedTokens: number; totalSupply: number } = await cacheResponse(
60 * 5, // 5 minutes
cacheKeys.getChainStats,
async () => {
const bondedTokensQuery = axios.get(`${apiNodeUrl}/cosmos/staking/v1beta1/pool`);
const supplyQuery = axios.get(`${apiNodeUrl}/cosmos/bank/v1beta1/supply`);
const communityPoolQuery = axios.get(`${apiNodeUrl}/cosmos/distribution/v1beta1/community_pool`);
const inflationQuery = axios.get(`${apiNodeUrl}/cosmos/mint/v1beta1/inflation`);
const distributionQuery = axios.get(`${apiNodeUrl}/cosmos/distribution/v1beta1/params`);

const [bondedTokensResponse, supplyResponse, communityPoolResponse, inflationResponse, distributionResponse] = await Promise.all([
bondedTokensQuery,
supplyQuery,
communityPoolQuery,
inflationQuery,
distributionQuery
]);

return {
communityPool: parseFloat(communityPoolResponse.data.pool.find((x) => x.denom === "uakt").amount),
inflation: parseFloat(inflationResponse.data.inflation),
communityTax: parseFloat(distributionResponse.data.params.community_tax),
bondedTokens: parseInt(bondedTokensResponse.data.pool.bonded_tokens),
totalSupply: parseInt(supplyResponse.data.supply.find((x) => x.denom === "uakt").amount)
};
},
true
);

return {
bondedTokens: bondedTokens,
totalSupply: totalSupply,
communityPool: communityPool,
inflation: inflation,
stakingAPR: (inflation * (1 - communityTax)) / (bondedTokens / totalSupply)
bondedTokens: result.bondedTokens,
totalSupply: result.totalSupply,
communityPool: result.communityPool,
inflation: result.inflation,
stakingAPR: (result.inflation * (1 - result.communityTax)) / (result.bondedTokens / result.totalSupply)
};
}

Expand Down
7 changes: 7 additions & 0 deletions deploy-web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ const moduleExports = {
sentry: {
hideSourceMaps: true
},
webpack: config => {
// Fixes npm packages that depend on `node:crypto` module
config.externals.push({
"node:crypto": "crypto"
});
return config;
},
redirects: async () => {
return [
{
Expand Down
14 changes: 7 additions & 7 deletions deploy-web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deploy-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"type-check": "tsc"
},
"dependencies": {
"@akashnetwork/akashjs": "^0.4.0",
"@akashnetwork/akashjs": "^0.4.9",
"@auth0/nextjs-auth0": "^1.9.1",
"@cosmjs/encoding": "^0.29.5",
"@cosmjs/stargate": "^0.29.5",
Expand Down
Loading

0 comments on commit 43fe222

Please sign in to comment.