Skip to content

Commit

Permalink
Add extra info for mex routes (#1305)
Browse files Browse the repository at this point in the history
* Added additional info for /tokens , /mex/tokens and /mex/pairs routes (#1304)

* Added additional info for tokens and mex/tokens routes

* Added trades count computing and fields description

* Coding style improvements

* Unused import removed

* test

* Deleted endline

* added newline

* Revert "added newline"

This reverts commit 62afe3f.

* Verify signature

* Revert changes

* try again signature

* Revert changes

---------

Co-authored-by: GuticaStefan <123564494+GuticaStefan@users.noreply.github.com>
Co-authored-by: GuticaStefan <stefan.gutica@gmail.com>
Co-authored-by: Nicolae Mogage <nicolaemogage1311@gmail.com>
Co-authored-by: Nicolae Mogage <mogagenicolae@yahoo.com>
  • Loading branch information
5 people authored Aug 20, 2024
1 parent f42ed99 commit dfbef83
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/endpoints/mex/entities/mex.pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,20 @@ export class MexPair {
@Field(() => String, { description: "Mex pair exchange details.", nullable: true })
@ApiProperty({ type: String, example: 'jungledex' })
exchange: MexPairExchange | undefined;

@Field(() => Boolean, { description: 'Mex pair farms details.', nullable: true })
@ApiProperty({ type: Boolean, nullable: true })
hasFarms: boolean | undefined = undefined;

@Field(() => Boolean, { description: 'Mex pair dual farms details.', nullable: true })
@ApiProperty({ type: Boolean, nullable: true })
hasDualFarms: boolean | undefined = undefined;

@Field(() => Number, { description: 'Mex pair trades count.', nullable: true })
@ApiProperty({ type: Number, nullable: true })
tradesCount: number | undefined = undefined;

@Field(() => Number, { description: 'Mex pair deploy date in unix time.', nullable: true })
@ApiProperty({ type: Number, nullable: true })
deployedAt: number | undefined = undefined;
}
8 changes: 8 additions & 0 deletions src/endpoints/mex/entities/mex.token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ export class MexToken {
@Field(() => Float, { description: "Mex token previous24hPrice." })
@ApiProperty({ type: Number, example: 0.000206738758250580 })
previous24hPrice: number = 0;

@Field(() => Float, { description: "Mex token previous24hVolume." })
@ApiProperty({ type: Number, example: 0.000206738758250580 })
previous24hVolume: number | undefined = 0;

@Field(() => Number, { description: 'Mex token trades count.', nullable: true })
@ApiProperty({ type: Number, nullable: true })
tradesCount: number | undefined = 0;
}
12 changes: 12 additions & 0 deletions src/endpoints/mex/mex.pair.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export class MexPairService {
type
lockedValueUSD
volumeUSD24h
hasFarms
hasDualFarms
tradesCount
deployedAt
__typename
}
}
Expand Down Expand Up @@ -185,6 +189,10 @@ export class MexPairService {
quoteName: pair.secondToken.name,
totalValue: Number(pair.lockedValueUSD),
volume24h: Number(pair.volumeUSD24h),
hasFarms: pair.hasFarms,
hasDualFarms: pair.hasDualFarms,
tradesCount: Number(pair.tradesCount),
deployedAt: Number(pair.deployedAt),
state,
type,
exchange,
Expand All @@ -209,6 +217,10 @@ export class MexPairService {
quoteName: pair.firstToken.name,
totalValue: Number(pair.lockedValueUSD),
volume24h: Number(pair.volumeUSD24h),
hasFarms: pair.hasFarms,
hasDualFarms: pair.hasDualFarms,
tradesCount: Number(pair.tradesCount),
deployedAt: Number(pair.deployedAt),
state,
type,
exchange,
Expand Down
18 changes: 17 additions & 1 deletion src/endpoints/mex/mex.token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class MexTokenService {
@Inject(forwardRef(() => MexFarmService))
private readonly mexFarmService: MexFarmService,
private readonly mexSettingsService: MexSettingsService,
private readonly graphQlService: GraphQlService
private readonly graphQlService: GraphQlService,
) { }

async refreshMexTokens(): Promise<void> {
Expand Down Expand Up @@ -183,6 +183,8 @@ export class MexTokenService {
wegldToken.name = pair.baseName;
wegldToken.price = pair.basePrice;
wegldToken.previous24hPrice = pair.basePrevious24hPrice;
wegldToken.previous24hVolume = pair.volume24h;
wegldToken.tradesCount = this.computeTradesCountForMexToken(wegldToken, filteredPairs);
mexTokens.push(wegldToken);
}

Expand All @@ -191,6 +193,8 @@ export class MexTokenService {
continue;
}

mexToken.tradesCount = this.computeTradesCountForMexToken(mexToken, filteredPairs);

mexTokens.push(mexToken);
}

Expand All @@ -205,6 +209,8 @@ export class MexTokenService {
name: pair.quoteName,
price: pair.quotePrice,
previous24hPrice: pair.quotePrevious24hPrice,
previous24hVolume: pair.volume24h,
tradesCount: 0,
};
}

Expand All @@ -215,6 +221,8 @@ export class MexTokenService {
name: pair.baseName,
price: pair.basePrice,
previous24hPrice: pair.basePrevious24hPrice,
previous24hVolume: pair.volume24h,
tradesCount: 0,
};
}

Expand All @@ -225,6 +233,8 @@ export class MexTokenService {
name: pair.quoteName,
price: pair.quotePrice,
previous24hPrice: pair.quotePrevious24hPrice,
previous24hVolume: pair.volume24h,
tradesCount: 0,
};
}

Expand Down Expand Up @@ -275,4 +285,10 @@ export class MexTokenService {
return [];
}
}

private computeTradesCountForMexToken(mexToken: MexToken, filteredPairs: MexPair[]): number {
const pairs = filteredPairs.filter(x => x.baseId === mexToken.id || x.quoteId === mexToken.id);
const computeResult = pairs.sum(pair => pair.tradesCount ?? 0);
return computeResult;
}
}
4 changes: 4 additions & 0 deletions src/endpoints/tokens/entities/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,8 @@ export class Token {
@Field(() => Number, { description: 'If the liquidity to market cap ratio is less than 0.5%, we consider it as low liquidity and display threshold percent .', nullable: true })
@ApiProperty({ type: Number, nullable: true })
lowLiquidityThresholdPercent: number | undefined = undefined;

@Field(() => Number, { description: 'Mex pair trades count.', nullable: true })
@ApiProperty({ type: Number, nullable: true })
tradesCount: number | undefined = undefined;
}
1 change: 1 addition & 0 deletions src/endpoints/tokens/token.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class TokenController {
@Query('includeMetaESDT', new ParseBoolPipe) includeMetaESDT?: boolean,
@Query('mexPairType', new ParseEnumArrayPipe(MexPairType)) mexPairType?: MexPairType[],
): Promise<TokenDetailed[]> {

return await this.tokenService.getTokens(
new QueryPagination({ from, size }),
new TokenFilter({ type, search, name, identifier, identifiers, includeMetaESDT, sort, order, mexPairType })
Expand Down
28 changes: 28 additions & 0 deletions src/endpoints/tokens/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ export class TokenService {
await this.applyMexLiquidity(tokens.filter(x => x.type !== TokenType.MetaESDT));
await this.applyMexPrices(tokens.filter(x => x.type !== TokenType.MetaESDT));
await this.applyMexPairType(tokens.filter(x => x.type !== TokenType.MetaESDT));
await this.applyMexPairTradesCount(tokens.filter(x => x.type !== TokenType.MetaESDT));

await this.cachingService.batchApplyAll(
tokens,
Expand Down Expand Up @@ -955,4 +956,31 @@ export class TokenService {
this.logger.error(error);
}
}

private async applyMexPairTradesCount(tokens: TokenDetailed[]): Promise<void> {
if (!tokens.length) {
return;
}

try {
const pairs = await this.mexPairService.getAllMexPairs();
const filteredPairs = pairs.filter(x => x.state === MexPairState.active);

if (!filteredPairs.length) {
return;
}

for (const token of tokens) {
const tokenPairs = filteredPairs.filter(x => x.baseId === token.identifier || x.quoteId === token.identifier);

if (tokenPairs.length > 0) {
token.tradesCount = tokenPairs.sum(tokenPair => tokenPair.tradesCount ?? 0);
}
}

} catch (error) {
this.logger.error('Could not apply mex trades count');
this.logger.error(error);
}
}
}

0 comments on commit dfbef83

Please sign in to comment.