-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The creation of functionality to obtain token prices
- Loading branch information
1 parent
d9e8cdc
commit 2172a8b
Showing
11 changed files
with
235 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export const TokensSymbols = [ | ||
'DOT', | ||
'ASTR', | ||
'USDT', | ||
'ACA', | ||
'aSEED', | ||
'LDOT', | ||
'GLMR', | ||
'IBTC', | ||
'INTR', | ||
'PHA', | ||
'BNC', | ||
'vDOT', | ||
'EQD', | ||
'HDX', | ||
'KSM', | ||
'tDOT', | ||
'lcDOT', | ||
'EQ', | ||
'MOVR', | ||
'SDN', | ||
'ETH', | ||
'USDC', | ||
'WBTC', | ||
'BUSD', | ||
'DAI', | ||
'MATIC', | ||
'BNB', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ObjectType, Field, ArgsType } from '@nestjs/graphql' | ||
|
||
@ObjectType() | ||
export class TokensPrice { | ||
@Field(() => [TokenInfo]) | ||
tokens = [] | ||
} | ||
|
||
@ObjectType() | ||
export class TokenInfo { | ||
@Field(() => String) | ||
name = '' | ||
|
||
@Field(() => String) | ||
symbol = '' | ||
|
||
@Field(() => Number) | ||
usd = 0 | ||
} | ||
|
||
@ArgsType() | ||
export class GetTokensArgs { | ||
@Field(() => [String]) | ||
tokens: string[] = [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export interface CoinMarketTokenPrice { | ||
[key: tokenSymbol]: TokenPrice[] | ||
} | ||
|
||
export interface TokenPrice { | ||
name: string | ||
symbol: string | ||
quote: { | ||
USD: { | ||
price: number | null | ||
percent_change_1h: number | ||
percent_change_24h: number | ||
percent_change_7d: number | ||
} | ||
} | ||
} | ||
type tokenSymbol = string | ||
|
||
export interface Tokens { | ||
tokens: Token[] | ||
} | ||
|
||
export interface Token { | ||
name: string | ||
symbol: string | ||
usd: number | null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { HttpModule } from '@nestjs/axios' | ||
import { Module } from '@nestjs/common' | ||
import { EnvModule } from 'src/env/env.module' | ||
//import { EnvService } from '../env/env.service' | ||
import { TokensResolver } from './tokens.resolver' | ||
import { TokensService } from './tokens.service' | ||
|
||
@Module({ | ||
imports: [HttpModule, EnvModule], | ||
providers: [TokensService, TokensResolver], | ||
exports: [TokensService], | ||
}) | ||
export class TokensModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Resolver, Query, Args } from '@nestjs/graphql' | ||
import { TokensPrice, GetTokensArgs } from './dtos/get-tokens.dto' | ||
import { TokensService } from './tokens.service' | ||
|
||
@Resolver() | ||
export class TokensResolver { | ||
constructor(private readonly tokensService: TokensService) {} | ||
|
||
@Query(() => TokensPrice) | ||
getTokenPrice(@Args() args: GetTokensArgs) { | ||
const tokens = this.tokensService.getTokensPrice(args.tokens) | ||
return { | ||
tokens, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { HttpService } from '@nestjs/axios' | ||
import { Injectable } from '@nestjs/common' | ||
import { Interval } from '@nestjs/schedule' | ||
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) | ||
private readonly logger: PinoLogger, | ||
private readonly httpService: HttpService, | ||
private readonly envService: EnvService, | ||
) {} | ||
|
||
getTokensPrice(symbol: string[]): Token[] { | ||
return this.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, | ||
}, | ||
params: { | ||
symbol: symbols.join(','), | ||
}, | ||
}) | ||
.pipe( | ||
map((res) => res.data.data), | ||
catchError((error: any) => { | ||
this.logger.error({ error }, 'Error fetching tokens prices') | ||
throw new Error(error?.response?.data.message || error.message) | ||
}), | ||
) | ||
|
||
const tokens = await firstValueFrom(response) | ||
return tokens | ||
} | ||
@Interval(5000) | ||
async updateTokenPrices() { | ||
try { | ||
const tokenPrices = await this.fetchTokensPrices(TokensSymbols) | ||
const newPrices: Token[] = [] | ||
const symbols: string[] = Object.keys(tokenPrices) | ||
if (symbols.length === 0) return | ||
symbols.forEach((element) => { | ||
const token = tokenPrices[element] as TokenPrice[] | ||
const { name, symbol, quote } = token[0] | ||
const price = quote.USD.price || 0 | ||
|
||
newPrices.push({ | ||
name: name, | ||
symbol: symbol, | ||
usd: price, | ||
}) | ||
}) | ||
this.tokens = newPrices | ||
} catch (error) { | ||
this.logger.error(JSON.stringify(error, null, 2), 'Error updating token prices') | ||
} | ||
} | ||
} |