Skip to content

Commit

Permalink
Merge pull request #94 from sotatek-dev/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Sotatek-TanHoang authored Sep 26, 2024
2 parents 0d1613b + a3ec8dd commit 625ca5c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 46 deletions.
12 changes: 0 additions & 12 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions src/constants/blockchain.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ export enum EMinaChainEnviroment {
TESTNET = 'testnet',
MAINNET = 'mainnet',
}

export enum ECoinMarketCapTokenId {
ETH = 1027,
MINA = 8646,
}
60 changes: 40 additions & 20 deletions src/modules/crawler/batch.tokenprice.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
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()
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);
Expand All @@ -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}.`);
}
}
14 changes: 0 additions & 14 deletions src/modules/crawler/crawler.console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
18 changes: 18 additions & 0 deletions src/shared/utils/address.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};

0 comments on commit 625ca5c

Please sign in to comment.