From 8d5946a5baf1952ba4033074ee6a0194ffddad47 Mon Sep 17 00:00:00 2001 From: opcatdev Date: Thu, 31 Oct 2024 18:03:30 +0800 Subject: [PATCH] Add query token mint count --- .../collection/collection.controller.ts | 25 ++++++++++++ .../src/routes/token/token.controller.ts | 25 ++++++++++++ .../tracker/src/routes/token/token.module.ts | 8 +++- .../tracker/src/routes/token/token.service.ts | 40 +++++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) diff --git a/packages/tracker/src/routes/collection/collection.controller.ts b/packages/tracker/src/routes/collection/collection.controller.ts index 67229dc..d885f7a 100644 --- a/packages/tracker/src/routes/collection/collection.controller.ts +++ b/packages/tracker/src/routes/collection/collection.controller.ts @@ -258,4 +258,29 @@ export class CollectionController { return errorResponse(e); } } + + @Get(':collectionIdOrAddr/mintCount') + @ApiTags('collection') + @ApiOperation({ + summary: 'Get collection mint count by collection id or collection address', + }) + @ApiParam({ + name: 'collectionIdOrAddr', + required: true, + type: String, + description: 'collection id or collection address', + }) + async getTokenMintCount( + @Param('collectionIdOrAddr') collectionIdOrAddr: string, + ) { + try { + const mintCount = await this.tokenService.getTokenMintCount( + collectionIdOrAddr, + TokenTypeScope.NonFungible, + ); + return okResponse(mintCount); + } catch (e) { + return errorResponse(e); + } + } } diff --git a/packages/tracker/src/routes/token/token.controller.ts b/packages/tracker/src/routes/token/token.controller.ts index d142bd4..c9bee7a 100644 --- a/packages/tracker/src/routes/token/token.controller.ts +++ b/packages/tracker/src/routes/token/token.controller.ts @@ -107,4 +107,29 @@ export class TokenController { return errorResponse(e); } } + + @Get(':tokenIdOrTokenAddr/mintCount') + @ApiTags('token') + @ApiOperation({ + summary: 'Get token mint count by token id or token address', + }) + @ApiParam({ + name: 'tokenIdOrTokenAddr', + required: true, + type: String, + description: 'token id or token address', + }) + async getTokenMintCount( + @Param('tokenIdOrTokenAddr') tokenIdOrTokenAddr: string, + ) { + try { + const mintCount = await this.tokenService.getTokenMintCount( + tokenIdOrTokenAddr, + TokenTypeScope.Fungible, + ); + return okResponse(mintCount); + } catch (e) { + return errorResponse(e); + } + } } diff --git a/packages/tracker/src/routes/token/token.module.ts b/packages/tracker/src/routes/token/token.module.ts index 1ab936e..9116882 100644 --- a/packages/tracker/src/routes/token/token.module.ts +++ b/packages/tracker/src/routes/token/token.module.ts @@ -6,10 +6,16 @@ import { TokenInfoEntity } from '../../entities/tokenInfo.entity'; import { TxOutEntity } from '../../entities/txOut.entity'; import { TxEntity } from '../../entities/tx.entity'; import { CommonModule } from '../../services/common/common.module'; +import { TokenMintEntity } from '../../entities/tokenMint.entity'; @Module({ imports: [ - TypeOrmModule.forFeature([TokenInfoEntity, TxOutEntity, TxEntity]), + TypeOrmModule.forFeature([ + TokenInfoEntity, + TxOutEntity, + TxEntity, + TokenMintEntity, + ]), CommonModule, ], providers: [TokenService], diff --git a/packages/tracker/src/routes/token/token.service.ts b/packages/tracker/src/routes/token/token.service.ts index be4e36b..674afdd 100644 --- a/packages/tracker/src/routes/token/token.service.ts +++ b/packages/tracker/src/routes/token/token.service.ts @@ -19,6 +19,7 @@ import { LRUCache } from 'lru-cache'; import { TxEntity } from '../../entities/tx.entity'; import { CommonService } from '../../services/common/common.service'; import { TokenTypeScope } from '../../common/types'; +import { TokenMintEntity } from '../../entities/tokenMint.entity'; @Injectable() export class TokenService { @@ -41,6 +42,8 @@ export class TokenService { private readonly txOutRepository: Repository, @InjectRepository(TxEntity) private readonly txRepository: Repository, + @InjectRepository(TokenMintEntity) + private readonly tokenMintRepository: Repository, ) {} async getTokenInfoByTokenIdOrTokenAddress( @@ -294,4 +297,41 @@ export class TokenService { } return balances; } + + async getTokenMintCount( + tokenIdOrTokenAddr: string, + scope: TokenTypeScope.Fungible | TokenTypeScope.NonFungible, + ): Promise<{ + count: string; + trackerBlockHeight: number; + }> { + const lastProcessedHeight = + await this.commonService.getLastProcessedBlockHeight(); + const tokenInfo = await this.getTokenInfoByTokenIdOrTokenAddress( + tokenIdOrTokenAddr, + scope, + ); + let count = '0'; + if (tokenInfo && tokenInfo.tokenPubKey && lastProcessedHeight) { + const where = { + tokenPubKey: tokenInfo.tokenPubKey, + blockHeight: LessThanOrEqual(lastProcessedHeight), + }; + if (scope === TokenTypeScope.Fungible) { + const r = await this.tokenMintRepository + .createQueryBuilder() + .select('SUM(token_amount)', 'count') + .where(where) + .getRawOne(); + count = r?.count || '0'; + } else { + const r = await this.tokenMintRepository.count({ where }); + count = (r || 0).toString(); + } + } + return { + count, + trackerBlockHeight: lastProcessedHeight, + }; + } }