Skip to content

Commit

Permalink
Change in save tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Marielbamar21 committed Feb 22, 2024
1 parent 688f895 commit 2d75966
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
44 changes: 44 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
"@fastify/helmet": "^11.1.1",
"@nestjs/apollo": "^12.0.10",
"@nestjs/axios": "^3.0.1",
"@nestjs/cache-manager": "^2.2.1",
"@nestjs/common": "^10.2.8",
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.2.8",
"@nestjs/graphql": "^12.0.10",
"@nestjs/platform-fastify": "^10.2.8",
"@nestjs/schedule": "^4.0.1",
"axios": "^1.6.1",
"cache-manager": "^5.4.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"nestjs-pino": "^3.5.0",
Expand Down
2 changes: 2 additions & 0 deletions src/env/env.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class EnvService {
public readonly STEALTH_EX_BASE_URL: string
public readonly STEALTH_EX_API_KEY: string

public readonly COINMARKETCAP_BASE_URL: string
public readonly COINMARKETCAP_API_KEY: string

constructor(private readonly config: ConfigService) {
Expand All @@ -39,6 +40,7 @@ export class EnvService {
this.STEALTH_EX_BASE_URL = this.config.get<string>('STEALTH_EX_BASE_URL', '')
this.STEALTH_EX_API_KEY = this.config.get<string>('STEALTH_EX_API_KEY', '')

this.COINMARKETCAP_BASE_URL = this.config.get<string>('COINMARKETCAP_BASE_URL', '')
this.COINMARKETCAP_API_KEY = this.config.get<string>('COINMARKETCAP_API_KEY', '')
}

Expand Down
17 changes: 16 additions & 1 deletion src/tokens/tokens.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { HttpModule } from '@nestjs/axios'
import { CacheModule } from '@nestjs/cache-manager'
import { Module } from '@nestjs/common'
import { EnvModule } from 'src/env/env.module'
import { EnvService } from 'src/env/env.service'
import { TokensResolver } from './tokens.resolver'
import { TokensService } from './tokens.service'

@Module({
imports: [HttpModule, EnvModule],
imports: [
CacheModule.register(),
HttpModule.registerAsync({
imports: [EnvModule],
inject: [EnvService],
useFactory: async (env: EnvService) => ({
baseURL: env.COINMARKETCAP_BASE_URL,
headers: {
'X-CMC_PRO_API_KEY': env.COINMARKETCAP_API_KEY,
},
}),
}),
EnvModule,
],
providers: [TokensService, TokensResolver],
exports: [TokensService],
})
Expand Down
25 changes: 12 additions & 13 deletions src/tokens/tokens.service.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import { HttpService } from '@nestjs/axios'
import { Injectable } from '@nestjs/common'
import { Injectable, Inject } from '@nestjs/common'
import { Interval } from '@nestjs/schedule'
import { Cache, CACHE_MANAGER } from '@nestjs/cache-manager'

Check failure on line 4 in src/tokens/tokens.service.ts

View workflow job for this annotation

GitHub Actions / check

`@nestjs/cache-manager` import should occur before import of `@nestjs/common`
import { InjectPinoLogger, PinoLogger } from 'nestjs-pino'
import { catchError, firstValueFrom, map } from 'rxjs'
import { TokensSymbols } from './dtos/array-tokens-symbol'
import { Token, CoinMarketTokenPrice, TokenPrice } from './tokens.interface'
import { EnvService } from '../env/env.service'

@Injectable()
export class TokensService {
private tokens: Token[] = []
constructor(
@InjectPinoLogger(TokensService.name)
@Inject(CACHE_MANAGER)
private readonly logger: PinoLogger,
private readonly httpService: HttpService,
private readonly envService: EnvService,
private cacheManager: Cache,
) {}

getTokensPrice(symbol: string[]): Token[] {
return this.tokens.filter((element) => symbol.includes(element.symbol))
async getTokensPrice(symbol: string[]): Promise<Token[]> {
const tokens: Token[] | undefined = await this.cacheManager.get('tokens')
console.log(tokens)
return !tokens ? [] : tokens.filter((element) => symbol.includes(element.symbol))
}

async fetchTokensPrices(symbols: string[]): Promise<CoinMarketTokenPrice> {
const response = await this.httpService
.get('https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest', {
headers: {
'X-CMC_PRO_API_KEY': this.envService.COINMARKETCAP_API_KEY,
},
.get('/cryptocurrency/quotes/latest', {
params: {
symbol: symbols.join(','),
},
Expand All @@ -42,8 +41,8 @@ export class TokensService {
const tokens = await firstValueFrom(response)
return tokens
}
//It is suggested to use 23529 if switching to the Hobbyist plan
@Interval(259200)
//It is suggested to use 23529 if switching to the Hobbyist plan259200
@Interval(5000)
async updateTokenPrices() {
try {
const tokenPrices = await this.fetchTokensPrices(TokensSymbols)
Expand All @@ -61,7 +60,7 @@ export class TokensService {
usd: price,
})
})
this.tokens = newPrices
await this.cacheManager.set('tokens', newPrices)
} catch (error) {
this.logger.error(JSON.stringify(error, null, 2), 'Error updating token prices')
}
Expand Down

0 comments on commit 2d75966

Please sign in to comment.