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

Integrates shared cache, lib errors #8

Merged
merged 22 commits into from
Jan 30, 2023
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
9 changes: 8 additions & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ TESTNET_DELEGATION_API_ROOT_URI=https://api-delegation.qa.community.taraxa.io
COINGECKO_API_TARAXA_URI=https://api.coingecko.com/api/v3/simple/price?ids=taraxa&vs_currencies=usd
TESTNET_EXPLORER_API_ROOT_URI=https://explorer.testnet.taraxa.io
EXPLORER_API_ROOT_URI=https://explorer.mainnet.taraxa.io
GITHUB_ACCESS_TOKEN=<your_GH_token>
GITHUB_ACCESS_TOKEN=<your_GH_token>

PORT=3001

REDIS_HOST=127.0.0.1
REDIS_PORT=6739
REDIS_NAME=info-cache
REDIS_PASSWORD=pass
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ module.exports = {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'off',
},
};
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,4 @@ Temporary Items
# Local
.env
dist
/charts/taraxa-info-api/prod.secret.values.yaml
/charts/taraxa-info-api/prod.secret.values.yaml
4 changes: 4 additions & 0 deletions charts/taraxa-info-api/prod.values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ app:
coingeckoUri: https://api.coingecko.com/api/v3/simple/price?ids=taraxa&vs_currencies=usd
testnetExplorerUri: https://explorer.testnet.taraxa.io
mainnetExplorerUri: https://explorer.mainnet.taraxa.io
redisHost: 10.39.114.21
redisPort: 6379
redisPassword: pass
redisName: infocache
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need a password as the production redis is password-protected.


image:
tag: "latest"
Expand Down
3 changes: 3 additions & 0 deletions charts/taraxa-info-api/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ data:
COINGECKO_API_TARAXA_URI: {{ .Values.app.coingeckoUri | quote }}
TESTNET_EXPLORER_API_ROOT_URI: {{ .Values.app.testnetExplorerUri | quote }}
EXPLORER_API_ROOT_URI: {{ .Values.app.mainnetExplorerUri | quote }}
VargaElod23 marked this conversation as resolved.
Show resolved Hide resolved
REDIS_HOST: {{ .Values.app.redisHost | quote }}
REDIS_PORT: {{ .Values.app.redisPort | quote }}
REDIS_NAME: {{ .Values.app.redisName | quote }}
16 changes: 16 additions & 0 deletions charts/taraxa-info-api/templates/secrets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{- $secret := (lookup "v1" "Secret" .Release.Namespace .Release.Name) -}}
apiVersion: v1
kind: Secret
metadata:
name: {{ .Release.Name }}
labels:
{{- include "taraxa-info-api.labels" . | nindent 4 }}
type: Opaque
data:
{{ if $secret }}
{{- range $key, $value := $secret.data }}
{{ $key }}: {{ $value }}
{{- end }}
{{ else }}
auth: {{ .Values.app.redisPassword | quote }}
{{- end }}
4 changes: 4 additions & 0 deletions charts/taraxa-info-api/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ app:
coingeckoUri: https://api.coingecko.com/api/v3/simple/price?ids=taraxa&vs_currencies=usd
testnetExplorerUri: https://explorer.testnet.taraxa.io
mainnetExplorerUri: https://explorer.mainnet.taraxa.io
redisHost: 10.39.114.11
redisPort: 6379
serverPort: 3000
redisPassword: pass
redisName: infocache

nodeSelector:
cloud.google.com/gke-nodepool: taraxa-community-default
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^9.2.1",
"@nestjs/platform-express": "^9.2.1",
"@nestjs/schedule": "^2.1.0",
"@nestjs/swagger": "^6.1.4",
"cache-manager": "^3.4.4",
"cache-manager": "^5.1.4",
"cache-manager-redis-store": "^3.0.1",
"ethers": "^5.3.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
Expand All @@ -31,9 +33,12 @@
"devDependencies": {
"@nestjs/cli": "^7.6.0",
"@nestjs/schematics": "^7.3.1",
"@types/cache-manager": "^3.4.0",
"@types/cache-manager": "^4.0.2",
"@types/cache-manager-redis-store": "^2.0.1",
"@types/cron": "^2.0.0",
"@types/express": "^4.17.11",
"@types/node": "^14.14.36",
"@types/redis": "^4.0.11",
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"eslint": "^7.22.0",
Expand Down
22 changes: 20 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { CacheModule, CacheStore, Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { SwaggerModule } from '@nestjs/swagger';
import { redisStore } from 'cache-manager-redis-store';
import general from './config/general';
import { GitHubModule } from './github/github.module';
import { NodeModule } from './node/node.module';
Expand All @@ -13,6 +14,23 @@ import { TokenModule } from './token/token.module';
isGlobal: true,
load: [general],
}),
CacheModule.registerAsync({
isGlobal: true,
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
return {
store: await redisStore({
socket: {
host: configService.get<string>('redisHost'),
port: configService.get<number>('redisPort'),
},
name: `${configService.get<string>('redisName')}`,
password: configService.get<string>('redisPassword'),
}),
} as unknown as CacheStore;
},
}),
SwaggerModule,
TokenModule,
StakingModule,
Expand Down
4 changes: 4 additions & 0 deletions src/config/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ export default () => ({
coinGeckoTaraxaApi: process.env.COINGECKO_API_TARAXA_URI,
githubAccessToken: process.env.GITHUB_ACCESS_TOKEN,
graphQLGitHubURI: 'https://api.github.com/graphql',
redisHost: process.env.REDIS_HOST,
redisPort: process.env.REDIS_PORT,
redisPassword: process.env.REDIS_PASSWORD,
redisName: process.env.REDIS_NAME,
});
9 changes: 9 additions & 0 deletions src/github/github.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ import { GitHubService } from './github.service';
export class GitHubController {
constructor(private readonly ghService: GitHubService) {}

@Get()
@CacheTTL(36000)
async contributionData() {
const commitsThisMonth = (await this.ghService.commitsOfThisMonth())
.totalCommits;
return {
commitsThisMonth,
};
}
/**
* Returns the number of commits from the current month from the Taraxa-project organization
* @returns number of commits of current month
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ async function bootstrap() {
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);

SwaggerModule.setup('apidocs', app, document);
await app.listen(3000);

await app.listen(process.env.PORT || 3000);
}
bootstrap();
14 changes: 14 additions & 0 deletions src/node/node.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ import { NodeService } from './node.service';
export class NodeController {
constructor(private readonly nodeService: NodeService) {}

@Get()
async validatorData() {
const activeMainnet = (await this.nodeService.noActiveValidators())
.totalActive;
const activeTestnet = (await this.nodeService.noActiveValidators(true))
.totalActive;
const cumulativeCommission = await this.nodeService.cumulativeCommisson();
return {
activeMainnet,
activeTestnet,
cumulativeCommission,
};
}

/**
* Returns the active validators in the past week for the Taraxa Mainnet
* @returns active validator amount
Expand Down
22 changes: 19 additions & 3 deletions src/staking/staking.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,29 @@ export class StakingController {
private readonly delegationService: DelegationService,
) {}

@Get()
async stakingData() {
const totalStaked = await this.tokenService.totalStaked();
const totalDelegated = (await this.delegationService.totalDelegated())
.totalDelegated;
const avgValidatorCommission = (
await this.delegationService.averageWeightedCommission()
).averageWeightedCommission;
const avgStakingYield = 20 - (await this.avgValidatorCommission());
return {
totalStaked,
totalDelegated,
avgValidatorCommission,
avgStakingYield,
};
}
/**
* Returns the current TARA staked in the ecosystem
* @returns staked supply in ETH
*/
@Get('totalStake')
@CacheTTL(36000)
async totalSupply() {
async totalStaked() {
return await this.tokenService.totalStaked();
}

Expand All @@ -42,7 +58,7 @@ export class StakingController {
* Returns the current avegare weighted validator commission in the ecosystem
* @returns avegrage weighted validator commission
*/
@Get('AVC')
@Get('avc')
@CacheTTL(36000)
async avgValidatorCommission() {
return (await this.delegationService.averageWeightedCommission())
Expand All @@ -53,7 +69,7 @@ export class StakingController {
* Returns the current avegare staking validator yield in the ecosystem
* @returns avegare staking validator yield
*/
@Get('ASY')
@Get('asy')
@CacheTTL(36000)
async avgStakingYeild() {
return 20 - (await this.avgValidatorCommission());
Expand Down
2 changes: 1 addition & 1 deletion src/staking/staking.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HttpModule } from '@nestjs/axios';
import { Module, CacheModule } from '@nestjs/common';
import { CacheModule, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TokenModule } from 'src/token/token.module';
import { DelegationService } from './delegation.service';
Expand Down
15 changes: 9 additions & 6 deletions src/token/token.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import { TokenService } from './token.service';
@UseInterceptors(CacheInterceptor)
export class TokenController {
constructor(private readonly tokenService: TokenService) {}

@Get()
async getTokenData() {
return await this.tokenService.tokenData();
}

@Get('name')
@CacheTTL(36000)
async getName() {
Expand All @@ -28,9 +34,8 @@ export class TokenController {
* @returns price as float
*/
@Get('price')
@CacheTTL(30)
async getPrice() {
return await this.tokenService.getPrice();
return (await this.tokenService.marketDetails()).price;
}
@Get('decimals')
@CacheTTL(36000)
Expand Down Expand Up @@ -62,9 +67,8 @@ export class TokenController {
* @returns circulating supply in ETH
*/
@Get('totalCirculating')
@CacheTTL(30)
async totalInCirculation() {
return await this.tokenService.totalCirculation();
return (await this.tokenService.marketDetails()).circulatingSupply;
}
/**
* Returns the current TARA stakign ratio
Expand All @@ -80,8 +84,7 @@ export class TokenController {
* @returns market cap in 8-precision decimals as float
*/
@Get('mktCap')
@CacheTTL(30)
async mktCap() {
return await this.tokenService.mktCap();
return (await this.tokenService.marketDetails()).marketCap;
}
}
49 changes: 47 additions & 2 deletions src/token/token.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
CACHE_MANAGER,
ForbiddenException,
Inject,
Injectable,
InternalServerErrorException,
Logger,
Expand All @@ -10,6 +12,8 @@ import { BigNumber } from '@ethersproject/bignumber';
import * as Tara from './contracts/Tara.json';
import { HttpService } from '@nestjs/axios';
import { firstValueFrom, catchError, map } from 'rxjs';
import { Cache } from 'cache-manager';
import { MarketDetails } from '../utils/types';

@Injectable()
export class TokenService {
Expand All @@ -20,6 +24,7 @@ export class TokenService {
constructor(
private configService: ConfigService,
private readonly httpService: HttpService,
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {
this.ethersProvider = new ethers.providers.JsonRpcProvider(
this.configService.get<string>('provider'),
Expand Down Expand Up @@ -104,7 +109,47 @@ export class TokenService {
100
);
}
async mktCap() {
return Number(await this.totalCirculation()) * (await this.getPrice());
async marketDetails() {
const details = (await this.cacheManager.get('marketCap')) as MarketDetails;
if (details) {
return details;
} else {
try {
const price = await this.getPrice();
const circulatingSupply = await this.totalCirculation();
const marketCap = price * circulatingSupply;
const marketDetails = {
price,
circulatingSupply,
marketCap,
};
await this.cacheManager.set('marketCap', marketDetails as any, 30);
return marketDetails;
} catch (error) {
throw new InternalServerErrorException(
VargaElod23 marked this conversation as resolved.
Show resolved Hide resolved
'Fetching market details failed. Reason: ',
error,
);
}
}
}

async tokenData() {
const marketDetails = await this.marketDetails();
const name = await this.getName();
const symbol = await this.getSymbol();
const decimals = await this.getDecimals();
const totalSupply = await this.totalSupply();
const totalLocked = await this.totalLocked();
const stakingRatio = await this.stakingRatio();
return {
name,
symbol,
decimals,
totalSupply,
totalLocked,
stakingRatio,
...marketDetails,
};
}
}
6 changes: 6 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ export interface ValidatorData {
account: string;
info: ValidatorBasicInfo;
}

export interface MarketDetails {
price: number;
circulatingSupply: number;
marketCap: number;
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
"incremental": true,
"skipLibCheck": true
}
}
Loading