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: fix dolar value calculation #186

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Changes from 1 commit
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
14 changes: 7 additions & 7 deletions src/indexer/services/coinmarketcap/coinmarketcap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ The Licensed Work is (c) 2023 Sygma
SPDX-License-Identifier: LGPL-3.0-only
*/

import path from "path"
import { MemoryCache } from "cache-manager"
import BigNumber from "bignumber.js"
import { logger } from "../../../utils/logger"

import { fetchRetry } from "../../../utils/helpers"
Expand All @@ -17,7 +17,7 @@ export type CoinMaketCapResponse = {
last_updated: string
quote: {
USD: {
price: BigNumber
price: number
last_updated: string
}
}
Expand All @@ -35,12 +35,12 @@ class CoinMarketCapService {
}

private async getValueConvertion(amount: string, tokenSymbol: string): Promise<CoinMaketCapResponse["quote"]["USD"]["price"]> {
const tokenValue: string | undefined = await this.memoryCache.get(tokenSymbol)
const tokenValue: number | undefined = await this.memoryCache.get(tokenSymbol)
if (tokenValue) {
return BigNumber(amount).times(BigNumber(tokenValue))
return Number(amount) * tokenValue
tcar121293 marked this conversation as resolved.
Show resolved Hide resolved
}

const url = `${this.coinMarketCapUrl}/v2/tools/price-conversion?amount=${amount}&symbol=${tokenSymbol}&convert=USD`
const url = path.join(this.coinMarketCapUrl, `/v2/tools/price-conversion?amount=1&symbol=${tokenSymbol}&convert=USD`)
mpetrun5 marked this conversation as resolved.
Show resolved Hide resolved
logger.debug(`Calling CoinMarketCap service with URL: ${url}`)
try {
const response = await fetchRetry(url, {
Expand All @@ -54,7 +54,7 @@ class CoinMarketCapService {
data: [res],
} = (await response.json()) as { data: CoinMaketCapResponse[] }
await this.memoryCache.set(tokenSymbol, res.quote.USD.price)
return BigNumber(amount).times(BigNumber(res.quote.USD.price))
return Number(amount) * res.quote.USD.price
} catch (err) {
if (err instanceof Error) {
logger.error(err.message)
Expand All @@ -65,7 +65,7 @@ class CoinMarketCapService {

public async getValueInUSD(amount: string, tokenSymbol: string): Promise<number> {
const convertedValue = await this.getValueConvertion(amount, tokenSymbol)
return convertedValue.toNumber()
return convertedValue
}
}

Expand Down
Loading