Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SERVICES-2578] Caching improvements for tokens queries #1464

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/modules/tokens/services/token.compute.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { PendingExecutor } from 'src/utils/pending.executor';
import { CacheService } from '@multiversx/sdk-nestjs-cache';
import { TokenService } from './token.service';
import { computeValueUSD } from 'src/utils/token.converters';
import { getAllKeys } from 'src/utils/get.many.utils';

@Injectable()
export class TokenComputeService implements ITokenComputeService {
Expand Down Expand Up @@ -189,6 +190,15 @@ export class TokenComputeService implements ITokenComputeService {
return priceSoFar;
}

async getAllTokensPriceDerivedEGLD(tokenIDs: string[]): Promise<string[]> {
return getAllKeys(
this.cachingService,
tokenIDs,
'token.tokenPriceDerivedEGLD',
this.tokenPriceDerivedEGLD.bind(this),
);
}

@ErrorLoggerAsync({
logArgs: true,
})
Expand Down Expand Up @@ -222,6 +232,15 @@ export class TokenComputeService implements ITokenComputeService {
.toFixed();
}

async getAllTokensPriceDerivedUSD(tokenIDs: string[]): Promise<string[]> {
return getAllKeys(
this.cachingService,
tokenIDs,
'token.tokenPriceDerivedUSD',
this.tokenPriceDerivedUSD.bind(this),
);
}

@ErrorLoggerAsync({
logArgs: true,
})
Expand All @@ -243,6 +262,15 @@ export class TokenComputeService implements ITokenComputeService {
return values24h[0]?.value ?? undefined;
}

async getAllTokensPrevious24hPrice(tokenIDs: string[]): Promise<string[]> {
return getAllKeys(
this.cachingService,
tokenIDs,
'token.tokenPrevious24hPrice',
this.tokenPrevious24hPrice.bind(this),
);
}

@ErrorLoggerAsync({
logArgs: true,
})
Expand All @@ -265,6 +293,15 @@ export class TokenComputeService implements ITokenComputeService {
return values7d[0]?.value ?? undefined;
}

async getAllTokensPrevious7dPrice(tokenIDs: string[]): Promise<string[]> {
return getAllKeys(
this.cachingService,
tokenIDs,
'token.tokenPrevious7dPrice',
this.tokenPrevious7dPrice.bind(this),
);
}

@ErrorLoggerAsync({
logArgs: true,
})
Expand Down Expand Up @@ -388,6 +425,15 @@ export class TokenComputeService implements ITokenComputeService {
return valuesLast2Days.current;
}

async getAllTokensVolumeUSD24h(tokenIDs: string[]): Promise<string[]> {
return getAllKeys(
this.cachingService,
tokenIDs,
'token.tokenVolumeUSD24h',
this.tokenVolumeUSD24h.bind(this),
);
}

@ErrorLoggerAsync({
logArgs: true,
})
Expand All @@ -405,6 +451,17 @@ export class TokenComputeService implements ITokenComputeService {
return valuesLast2Days.previous;
}

async getAllTokensPrevious24hVolumeUSD(
tokenIDs: string[],
): Promise<string[]> {
return getAllKeys(
this.cachingService,
tokenIDs,
'token.tokenPrevious24hVolumeUSD',
this.tokenPrevious24hVolumeUSD.bind(this),
);
}

@ErrorLoggerAsync({
logArgs: true,
})
Expand Down Expand Up @@ -503,6 +560,15 @@ export class TokenComputeService implements ITokenComputeService {
).toFixed();
}

async getAllTokensLiquidityUSD(tokenIDs: string[]): Promise<string[]> {
return getAllKeys<string>(
this.cachingService,
tokenIDs,
'token.tokenLiquidityUSD',
this.tokenLiquidityUSD.bind(this),
);
}

@ErrorLoggerAsync({
logArgs: true,
})
Expand Down Expand Up @@ -533,6 +599,15 @@ export class TokenComputeService implements ITokenComputeService {
return undefined;
}

async getAllTokensCreatedAt(tokenIDs: string[]): Promise<string[]> {
return getAllKeys(
this.cachingService,
tokenIDs,
'token.tokenCreatedAt',
this.tokenCreatedAt.bind(this),
);
}

@ErrorLoggerAsync({
logArgs: true,
})
Expand Down Expand Up @@ -674,4 +749,13 @@ export class TokenComputeService implements ITokenComputeService {

return trendingScore.toFixed();
}

async getAllTokensTrendingScore(tokenIDs: string[]): Promise<string[]> {
return getAllKeys(
this.cachingService,
tokenIDs,
'token.tokenTrendingScore',
this.tokenTrendingScore.bind(this),
);
}
}
31 changes: 12 additions & 19 deletions src/modules/tokens/services/token.filtering.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ export class TokenFilteringService {
return tokenIDs;
}

const filteredIDs = [];
for (const tokenID of tokenIDs) {
const tokenType = await this.tokenService.getEsdtTokenType(tokenID);
const tokenTypes = await this.tokenService.getAllEsdtTokensType(
tokenIDs,
);

if (tokenType === tokensFilter.type) {
filteredIDs.push(tokenID);
}
}
return filteredIDs;
return tokenIDs.filter(
(_, index) => tokenTypes[index] === tokensFilter.type,
);
}

async tokensBySearchTerm(
Expand Down Expand Up @@ -84,17 +82,12 @@ export class TokenFilteringService {
return tokenIDs;
}

const filteredIDs = [];
for (const tokenID of tokenIDs) {
const liquidity = await this.tokenCompute.tokenLiquidityUSD(
tokenID,
);
const tokensLiquidityUSD =
await this.tokenCompute.getAllTokensLiquidityUSD(tokenIDs);

const liquidityBN = new BigNumber(liquidity);
if (liquidityBN.gte(tokensFilter.minLiquidity)) {
filteredIDs.push(tokenID);
}
}
return filteredIDs;
return tokenIDs.filter((_, index) => {
const liquidity = new BigNumber(tokensLiquidityUSD[index]);
return liquidity.gte(tokensFilter.minLiquidity);
});
}
}
64 changes: 10 additions & 54 deletions src/modules/tokens/services/token.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,21 @@ export class TokenLoader {

public readonly tokenTypeLoader = new DataLoader<string, string>(
async (tokenIDs: string[]) => {
return await getAllKeys(
this.cacheService,
tokenIDs,
'token.getEsdtTokenType',
this.tokenService.getEsdtTokenType.bind(this.tokenService),
);
return await this.tokenService.getAllEsdtTokensType(tokenIDs);
},
);

public readonly tokenPriceDerivedEGLDLoader = new DataLoader<
string,
string
>(async (tokenIDs: string[]) => {
return await getAllKeys(
this.cacheService,
tokenIDs,
'token.tokenPriceDerivedEGLD',
this.tokenCompute.tokenPriceDerivedEGLD.bind(this.tokenCompute),
);
return await this.tokenCompute.getAllTokensPriceDerivedEGLD(tokenIDs);
});

public readonly tokenPriceDerivedUSDLoader = new DataLoader<string, string>(
async (tokenIDs: string[]) => {
return await getAllKeys(
this.cacheService,
return await this.tokenCompute.getAllTokensPriceDerivedUSD(
tokenIDs,
'token.tokenPriceDerivedUSD',
this.tokenCompute.tokenPriceDerivedUSD.bind(this.tokenCompute),
);
},
);
Expand All @@ -53,67 +40,41 @@ export class TokenLoader {
string,
string
>(async (tokenIDs: string[]) => {
return await getAllKeys(
this.cacheService,
tokenIDs,
'token.tokenPrevious24hPrice',
this.tokenCompute.tokenPrevious24hPrice.bind(this.tokenCompute),
);
return await this.tokenCompute.getAllTokensPrevious24hPrice(tokenIDs);
});

public readonly tokenPrevious7dPriceLoader = new DataLoader<string, string>(
async (tokenIDs: string[]) => {
return await getAllKeys(
this.cacheService,
return await this.tokenCompute.getAllTokensPrevious7dPrice(
tokenIDs,
'token.tokenPrevious7dPrice',
this.tokenCompute.tokenPrevious7dPrice.bind(this.tokenCompute),
);
},
);

public readonly tokenVolumeUSD24hLoader = new DataLoader<string, string>(
async (tokenIDs: string[]) => {
return await getAllKeys(
this.cacheService,
tokenIDs,
'token.tokenVolumeUSD24h',
this.tokenCompute.tokenVolumeUSD24h.bind(this.tokenCompute),
);
return await this.tokenCompute.getAllTokensVolumeUSD24h(tokenIDs);
},
);

public readonly tokenPrevious24hVolumeUSDLoader = new DataLoader<
string,
string
>(async (tokenIDs: string[]) => {
return await getAllKeys(
this.cacheService,
return await this.tokenCompute.getAllTokensPrevious24hVolumeUSD(
tokenIDs,
'token.tokenPrevious24hVolumeUSD',
this.tokenCompute.tokenPrevious24hVolumeUSD.bind(this.tokenCompute),
);
});

public readonly tokenLiquidityUSDLoader = new DataLoader<string, string>(
async (tokenIDs: string[]) => {
return await getAllKeys(
this.cacheService,
tokenIDs,
'token.tokenLiquidityUSD',
this.tokenCompute.tokenLiquidityUSD.bind(this.tokenCompute),
);
return await this.tokenCompute.getAllTokensLiquidityUSD(tokenIDs);
},
);

public readonly tokenCreatedAtLoader = new DataLoader<string, string>(
async (tokenIDs: string[]) => {
return await getAllKeys(
this.cacheService,
tokenIDs,
'token.tokenCreatedAt',
this.tokenCompute.tokenCreatedAt.bind(this.tokenCompute),
);
return await this.tokenCompute.getAllTokensCreatedAt(tokenIDs);
},
);

Expand Down Expand Up @@ -142,12 +103,7 @@ export class TokenLoader {

public readonly tokenTrendingScoreLoader = new DataLoader<string, string>(
async (tokenIDs: string[]) => {
return await getAllKeys(
this.cacheService,
tokenIDs,
'token.tokenTrendingScore',
this.tokenCompute.tokenTrendingScore.bind(this.tokenCompute),
);
return await this.tokenCompute.getAllTokensTrendingScore(tokenIDs);
},
);
}
Loading
Loading