From bfbeb09c128b744c11bb7405b0068fec3535ebbb Mon Sep 17 00:00:00 2001 From: Tan Hoang Date: Thu, 26 Sep 2024 15:12:20 +0700 Subject: [PATCH] fix: job token price --- docker-compose.dev.yaml | 12 ----- src/constants/blockchain.constant.ts | 5 +++ src/modules/crawler/batch.tokenprice.ts | 60 ++++++++++++++++--------- src/modules/crawler/crawler.console.ts | 14 ------ src/shared/utils/address.ts | 18 ++++++++ 5 files changed, 63 insertions(+), 46 deletions(-) create mode 100644 src/shared/utils/address.ts diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml index ee18d51..4b8bc99 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.dev.yaml @@ -43,18 +43,6 @@ services: tty: true restart: always - depends_on: - - postgres - networks: - - myNetwork - user: node - crawl-token-mina: - image: mina-bridge:1.0.0 - command: > - sh -c "npm run console crawl-mina-token-contract" - tty: true - restart: always - depends_on: - postgres networks: diff --git a/src/constants/blockchain.constant.ts b/src/constants/blockchain.constant.ts index ff49b9c..89ff519 100644 --- a/src/constants/blockchain.constant.ts +++ b/src/constants/blockchain.constant.ts @@ -28,3 +28,8 @@ export enum EMinaChainEnviroment { TESTNET = 'testnet', MAINNET = 'mainnet', } + +export enum ECoinMarketCapTokenId { + ETH = 1027, + MINA = 8646, +} diff --git a/src/modules/crawler/batch.tokenprice.ts b/src/modules/crawler/batch.tokenprice.ts index 78875e8..1afb6f3 100644 --- a/src/modules/crawler/batch.tokenprice.ts +++ b/src/modules/crawler/batch.tokenprice.ts @@ -1,10 +1,13 @@ import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import axios from 'axios'; +import { isNumberString } from 'class-validator'; import { EAsset } from '../../constants/api.constant.js'; +import { ECoinMarketCapTokenId } from '../../constants/blockchain.constant.js'; import { EEnvKey } from '../../constants/env.constant.js'; import { TokenPriceRepository } from '../../database/repositories/token-price.repository.js'; +import { LoggerService } from '../../shared/modules/logger/logger.service.js'; import { TokenPrice } from './entities/index.js'; @Injectable() @@ -12,7 +15,9 @@ export class BatchJobGetPriceToken { constructor( private readonly configService: ConfigService, private readonly tokenPriceRepository: TokenPriceRepository, + private loggerService: LoggerService, ) {} + private readonly logger = this.loggerService.getLogger('CRAWL_TOKEN_PRICE'); public async handleGetPriceToken() { const apiKey = this.configService.get(EEnvKey.COINMARKET_KEY); @@ -23,25 +28,40 @@ export class BatchJobGetPriceToken { const result = await axios.get(apiUrl, { headers }); - result?.data?.data.forEach(async e => { - if (e.symbol == EAsset.MINA) { - const tokenMina = await this.tokenPriceRepository.getTokenPriceBySymbol(EAsset.MINA); - if (!tokenMina) { - this.tokenPriceRepository.save(new TokenPrice({ symbol: EAsset.MINA, priceUsd: e.quote.USD.price || 1 })); - } else { - tokenMina.priceUsd = e.quote.USD.price; - tokenMina.save(); - } - } - if (e.symbol == EAsset.ETH) { - const tokenMina = await this.tokenPriceRepository.getTokenPriceBySymbol(EAsset.ETH); - if (!tokenMina) { - this.tokenPriceRepository.save(new TokenPrice({ symbol: EAsset.ETH, priceUsd: e.quote.USD.price || 2300 })); - } else { - tokenMina.priceUsd = e.quote.USD.price; - tokenMina.save(); - } - } - }); + const MINA = result.data.data?.[ECoinMarketCapTokenId.MINA]; + const ETH = result.data.data?.[ECoinMarketCapTokenId.ETH]; + + let totalUpdated = 0; + + if (MINA) { + await this.updateTokenPrice(EAsset.MINA, MINA?.quote?.USD.price); + totalUpdated++; + } else { + this.logger.warn('Cannot get MINA token price from CoinMarketCap!'); + } + if (ETH) { + await this.updateTokenPrice(EAsset.ETH, ETH?.quote?.USD.price); + totalUpdated++; + } else { + this.logger.warn('Cannot get ETH token price from CoinMarketCap!'); + } + this.logger.info(`Total token updated = ${totalUpdated}`); + return; + } + private async updateTokenPrice(symbol: EAsset, newPrice: string) { + if (!isNumberString(newPrice.toString())) { + this.logger.warn('Invalid new price', newPrice); + return; + } + const toUpdateToken = await this.tokenPriceRepository.getTokenPriceBySymbol(symbol); + let oldPrice = '0'; + if (!toUpdateToken) { + await this.tokenPriceRepository.save(new TokenPrice({ symbol, priceUsd: newPrice })); + } else { + oldPrice = toUpdateToken.priceUsd; + toUpdateToken.priceUsd = newPrice; + await toUpdateToken.save(); + } + this.logger.info(`Updated price for ${symbol}. Old price: ${oldPrice}, new price: ${newPrice}.`); } } diff --git a/src/modules/crawler/crawler.console.ts b/src/modules/crawler/crawler.console.ts index cbc52fe..0eeb935 100644 --- a/src/modules/crawler/crawler.console.ts +++ b/src/modules/crawler/crawler.console.ts @@ -103,20 +103,6 @@ export class CrawlerConsole { } } - @Command({ - command: 'crawl-mina-token-contract', - description: 'crawl Mina Token Contract', - }) - async handleCrawlMinaToken() { - try { - while (true) { - await sleep(15); - } - } catch (error) { - this.logger.error(error); - } - } - @Command({ command: 'sender-mina-bridge-unlock', description: 'sender Mina Bridge unlock', diff --git a/src/shared/utils/address.ts b/src/shared/utils/address.ts new file mode 100644 index 0000000..ade6028 --- /dev/null +++ b/src/shared/utils/address.ts @@ -0,0 +1,18 @@ +import { toChecksumAddress } from 'web3-utils'; + +export const formatEthersAddress = (rawAddress: string) => { + try { + return toChecksumAddress(rawAddress); + } catch (error) { + console.log(`Cannot convert ${rawAddress} to ethers checksum address.`); + return rawAddress; + } +}; +export const formatMinaAddress = (rawAddress: string) => { + try { + return rawAddress; + } catch (error) { + console.log(`Cannot convert ${rawAddress} to ethers checksum address.`); + return rawAddress; + } +};