Skip to content

Commit

Permalink
Add query token mint count
Browse files Browse the repository at this point in the history
  • Loading branch information
opcatdev committed Oct 31, 2024
1 parent be02034 commit 8d5946a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/tracker/src/routes/collection/collection.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
25 changes: 25 additions & 0 deletions packages/tracker/src/routes/token/token.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
8 changes: 7 additions & 1 deletion packages/tracker/src/routes/token/token.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
40 changes: 40 additions & 0 deletions packages/tracker/src/routes/token/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -41,6 +42,8 @@ export class TokenService {
private readonly txOutRepository: Repository<TxOutEntity>,
@InjectRepository(TxEntity)
private readonly txRepository: Repository<TxEntity>,
@InjectRepository(TokenMintEntity)
private readonly tokenMintRepository: Repository<TokenMintEntity>,
) {}

async getTokenInfoByTokenIdOrTokenAddress(
Expand Down Expand Up @@ -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,
};
}
}

0 comments on commit 8d5946a

Please sign in to comment.