Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restruturing #17

Merged
merged 17 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .commitlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"provider",
"deployment",
"certificate",
"dx"
"dx",
"stats"
]
]
}
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ apps/provider-proxy/.env
apps/stats-web/.env
.env.*.local
.env.local
.env.test

# IDE files
.idea
.vscode
.vscode-test

# Data Folder
data
data
testdata
test-results
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
</div>

- [Quick Start](#quick-start)
- [Apps Configuration](./doc/apps-configuration.md)
- [Services](#services)
- [Monitoring](#monitoring)
- [Example SQL Queries](#example-sql-queries)
Expand Down
17 changes: 0 additions & 17 deletions apps/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,6 @@

You can make sure the api is working by accessing the status endpoint: `http://localhost:3080/status`

## Environment Variables

This app utilizes `.env*` files to manage environment variables. The list of environment variables can be found in the `env/.env.sample` file. These files are included in version control and should only contain non-sensitive values. Sensitive values are provided by the deployment system.

### Important Notes:
- **Sensitive Values**: The only env file that's ignored by Git is `env/.env.local`, which is intended for sensitive values used in development.
- **Loading Order**: Environment files are loaded in a specific order, depending on two environment variables: `DEPLOYMENT_ENV` and `NETWORK`.

### Loading Order:
1. `env/.env.local` - Contains sensitive values for development.
2. `env/.env` - Default values applicable to all environments.
3. `env/.env.${DEPLOYMENT_ENV}` - Values specific to the deployment environment.
4. `env/.env.${NETWORK}` - Values specific to the network.

### Additional Details:
- **Variable Precedence**: If a variable is already set in the environment, it will not be overridden by values in the `.env*` files. This behavior is critical when adjusting the loading order of these files.

## Testing

Project is configured to use [Jest](https://jestjs.io/) for testing. It is intended to be covered with unit and functional tests where applicable.
Expand Down
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@akashnetwork/akash-api": "^1.3.0",
"@akashnetwork/akashjs": "^0.10.0",
"@akashnetwork/database": "*",
"@akashnetwork/env-loader": "*",
"@akashnetwork/http-sdk": "*",
"@casl/ability": "^6.7.1",
"@chain-registry/assets": "^1.64.79",
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/console.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "reflect-metadata";
import "./dotenv";
import "@akashnetwork/env-loader";
import "./open-telemetry";

import { context, trace } from "@opentelemetry/api";
Expand Down
23 changes: 0 additions & 23 deletions apps/api/src/dotenv.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "reflect-metadata";
import "@akashnetwork/env-loader";
import "./open-telemetry";
import "./dotenv";

async function bootstrap() {
/* eslint-disable @typescript-eslint/no-var-requires */
Expand Down
40 changes: 27 additions & 13 deletions apps/api/src/routes/v1/dashboardData.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";

import { LoggerService } from "@src/core/services/logger/logger.service";
import { getBlocks } from "@src/services/db/blocksService";
import { getNetworkCapacity } from "@src/services/db/providerStatusService";
import { getDashboardData, getProviderGraphData } from "@src/services/db/statsService";
import { getTransactions } from "@src/services/db/transactionsService";
import { getChainStats } from "@src/services/external/apiNodeService";
import { createLoggingExecutor } from "@src/utils/logging";


const logger = new LoggerService({ context: "Dashboard" });
const runOrLog = createLoggingExecutor(logger)

const route = createRoute({
method: "get",
Expand Down Expand Up @@ -144,25 +150,33 @@ const route = createRoute({
});

export default new OpenAPIHono().openapi(route, async c => {
const chainStatsQuery = await getChainStats();
const dashboardData = await getDashboardData();
const networkCapacity = await getNetworkCapacity();
const networkCapacityStats = await getProviderGraphData("count");
const latestBlocks = await getBlocks(5);
const latestTransactions = await getTransactions(5);

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
}),
runOrLog(getNetworkCapacity),
runOrLog(() => getProviderGraphData("count")),
runOrLog(() => getBlocks(5)),
runOrLog(() => getTransactions(5))
]);
const chainStats = {
height: latestBlocks[0].height,
transactionCount: latestBlocks[0].totalTransactionCount,
...chainStatsQuery
};
...chainStatsQuery,
height: latestBlocks && latestBlocks.length > 0 ? latestBlocks[0].height : undefined,
transactionCount: latestBlocks && latestBlocks.length > 0 ? latestBlocks[0].totalTransactionCount : undefined,
}

return c.json({
chainStats,
...dashboardData,
now,
compare,
networkCapacity,
networkCapacityStats,
latestBlocks,
latestTransactions
});
});
});
79 changes: 61 additions & 18 deletions apps/api/src/services/external/apiNodeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fetch from "node-fetch";
import { Op } from "sequelize";

import { cacheKeys, cacheResponse } from "@src/caching/helpers";
import { LoggerService } from "@src/core/services/logger/logger.service";
import { getTransactionByAddress } from "@src/services/db/transactionsService";
import {
CosmosGovProposalResponse,
Expand All @@ -28,45 +29,87 @@ import { CosmosMintInflationResponse } from "@src/types/rest/cosmosMintInflation
import { CosmosStakingPoolResponse } from "@src/types/rest/cosmosStakingPoolResponse";
import { coinToAsset } from "@src/utils/coin";
import { apiNodeUrl, averageBlockCountInAMonth, betaTypeVersion, betaTypeVersionMarket } from "@src/utils/constants";
import { createLoggingExecutor } from "@src/utils/logging";
import { getDeploymentRelatedMessages } from "../db/deploymentService";
import { getProviderList } from "../db/providerStatusService";

export async function getChainStats() {
const logger = new LoggerService({ context: "ApiNode" })
const runOrLog = createLoggingExecutor(logger)

const result = await cacheResponse(
60 * 5, // 5 minutes
cacheKeys.getChainStats,
async () => {
const bondedTokensQuery = axios.get<CosmosStakingPoolResponse>(`${apiNodeUrl}/cosmos/staking/v1beta1/pool`);
const supplyQuery = axios.get<CosmosBankSupplyResponse>(`${apiNodeUrl}/cosmos/bank/v1beta1/supply?pagination.limit=1000`);
const communityPoolQuery = axios.get<CosmosDistributionCommunityPoolResponse>(`${apiNodeUrl}/cosmos/distribution/v1beta1/community_pool`);
const inflationQuery = axios.get<CosmosMintInflationResponse>(`${apiNodeUrl}/cosmos/mint/v1beta1/inflation`);
const distributionQuery = axios.get<CosmosDistributionParamsResponse>(`${apiNodeUrl}/cosmos/distribution/v1beta1/params`);

const [bondedTokensResponse, supplyResponse, communityPoolResponse, inflationResponse, distributionResponse] = await Promise.all([
bondedTokensQuery,
supplyQuery,
communityPoolQuery,
inflationQuery,
distributionQuery
const bondedTokensAsPromised = await runOrLog(async () => {
const bondedTokensQuery = await axios.get<CosmosStakingPoolResponse>(
`${apiNodeUrl}/cosmos/staking/v1beta1/pool`
);
return parseInt(bondedTokensQuery.data.pool.bonded_tokens);
});

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

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: 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)
communityPool,
inflation,
communityTax,
bondedTokens,
totalSupply,
};
},
true
);

let stakingAPR: number | undefined;
if (result.bondedTokens && result.bondedTokens > 0 && result.inflation && result.communityTax && result.totalSupply) {
stakingAPR = result.inflation * (1 - result.communityTax) * result.totalSupply / result.bondedTokens
}

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

Expand Down
12 changes: 12 additions & 0 deletions apps/api/src/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getSentry } from "@src/core/providers/sentry.provider";
import { LoggerService } from "@src/core/services/logger/logger.service";

export const createLoggingExecutor = (logger: LoggerService) => async <T>(cb: () => Promise<T>, defaultValue?: T): Promise<T> => {
try {
return await cb();
} catch (error) {
logger.error({ event: `Failed to fetch ${cb.name}`, error });
getSentry().captureException(error);
return defaultValue;
}
}
33 changes: 0 additions & 33 deletions apps/deploy-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,3 @@
4. Start the app with `npm run dev`.

The website should be accessible: [http://localhost:3000/](http://localhost:3000/)

## Environment Variables

### Overview
Environment variables in this Next.js app follow the standard Next.js behavior, as documented in the [Next.js environment variables documentation](https://nextjs.org/docs/basic-features/environment-variables). This means that files like `.env.local` or `.env.production` will be automatically loaded based on the environment in which the app is running.

However, we have extended this functionality to support more granular environment-specific configurations. Environment variables are stored in the `./env` directory, where multiple `.env` files exist for different deployment environments (stages):

- `.env` - Loaded for any environment
- `.env.production` - Loaded for the production stage
- `.env.staging` - Loaded for the staging stage

### How Environment Variables Are Loaded
We use **dotenvx** to manage and load environment variables. This allows us to take advantage of its features, such as **variable interpolation** (i.e., using other environment variables within variable values).

### Validation with Zod
Environment variables are validated using **Zod** schemas, ensuring that all required variables are present and have valid values. The validation logic can be found in the file `src/config/env-config.schema.ts`.

We use two separate Zod schemas:
- **Static Build-Time Schema**: Validates variables at build time. If any variables are missing or invalid during the build process, the build will fail.
- **Dynamic Server Runtime Schema**: Validates variables at server startup. If any variables are missing or invalid at this stage, the server will fail to start.

This validation ensures that both build and runtime configurations are secure and complete before the app runs.

### App Configuration
App configurations, including environment variables, are located in the `src/config` directory. In our setup:
- **Environment configs** are handled separately from **hardcoded configs**.
- Hardcoded configs are organized by domain to maintain a clear structure and separation of concerns.

### Sample Environment Variables
All environment variables required for the app, along with their expected structure and types, can be found in the `env/.env.sample` file. This sample file serves as a template for setting up your environment variables and ensures that all necessary variables are accounted for in each environment.

By organizing environment variables and configuration this way, we ensure a consistent, safe, and scalable approach to managing different deployment environments.
2 changes: 2 additions & 0 deletions apps/deploy-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@akashnetwork/akashjs": "^0.10.0",
"@akashnetwork/env-loader": "*",
"@akashnetwork/http-sdk": "*",
"@akashnetwork/network-store": "*",
"@akashnetwork/ui": "*",
"@auth0/nextjs-auth0": "^3.5.0",
"@chain-registry/types": "^0.41.3",
Expand All @@ -42,6 +43,7 @@
"@nivo/core": "^0.87.0",
"@nivo/line": "^0.87.0",
"@nivo/pie": "^0.87.0",
"@octokit/openapi-types": "^22.2.0",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-slot": "^1.0.2",
"@react-spring/web": "^9.3.1",
Expand Down
Loading