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

[MEX-715] Swap query cache optimization #1578

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
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
56 changes: 30 additions & 26 deletions src/modules/auto-router/services/auto-router.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,38 +334,42 @@ export class AutoRouterService {
}

private async getAllActivePairs() {
const pairAddresses = await this.routerAbi.pairsAddress();
const statesPromises = pairAddresses.map((address) =>
this.pairAbi.state(address),
const pairMetadata = await this.routerAbi.pairsMetadata();

const states = await this.pairService.getAllStates(
pairMetadata.map((pair) => pair.address),
);
const states = await Promise.all(statesPromises);
const activePairs: string[] = [];
states.forEach((value, index) => {
if (value === 'Active') activePairs.push(pairAddresses[index]);
});

const pairsPromises = activePairs.map((address) =>
this.getPair(address),
const activePairs = pairMetadata.filter(
(_pair, index) => states[index] === 'Active',
);

return await Promise.all(pairsPromises);
}
const pairAddresses: string[] = [];
let tokenIDs: string[] = [];
activePairs.forEach((pair) => {
pairAddresses.push(pair.address);
tokenIDs.push(...[pair.firstTokenID, pair.secondTokenID]);
});
tokenIDs = [...new Set(tokenIDs)];

private async getPair(pairAddress: string): Promise<PairModel> {
const [info, totalFeePercent, firstToken, secondToken] =
await Promise.all([
this.pairAbi.pairInfoMetadata(pairAddress),
this.pairAbi.totalFeePercent(pairAddress),
this.pairService.getFirstToken(pairAddress),
this.pairService.getSecondToken(pairAddress),
]);
const [allInfo, allTotalFeePercent, allTokens] = await Promise.all([
this.pairAbi.getAllPairsInfoMetadata(pairAddresses),
this.pairAbi.getAllPairsTotalFeePercent(pairAddresses),
this.tokenService.getAllTokensMetadata(tokenIDs),
]);

const tokenMap = new Map(
allTokens.map((token) => [token.identifier, token]),
);

return new PairModel({
address: pairAddress,
firstToken,
secondToken,
info,
totalFeePercent,
return activePairs.map((pair, index) => {
return new PairModel({
address: pair.address,
firstToken: tokenMap.get(pair.firstTokenID),
secondToken: tokenMap.get(pair.secondTokenID),
info: allInfo[index],
totalFeePercent: allTotalFeePercent[index],
});
});
}

Expand Down
7 changes: 7 additions & 0 deletions src/modules/pair/mocks/pair.abi.service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export class PairAbiServiceMock implements IPairAbiService {
async totalFeePercent(pairAddress: string): Promise<number> {
return PairsData(pairAddress).totalFeePercent;
}
async getAllPairsTotalFeePercent(
pairAddresses: string[],
): Promise<number[]> {
return pairAddresses.map(
(pairAddress) => PairsData(pairAddress).totalFeePercent,
);
}
specialFeePercent(pairAddress: string): Promise<number> {
throw new Error('Method not implemented.');
}
Expand Down
8 changes: 1 addition & 7 deletions src/modules/pair/services/pair.abi.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ export class PairAbiLoader {

public readonly totalFeePercentLoader = new DataLoader<string, number>(
async (addresses: string[]) => {
return getAllKeys<number>(
this.cacheService,
addresses,
'pair.totalFeePercent',
this.pairAbi.totalFeePercent.bind(this.pairAbi),
CacheTtlInfo.ContractState,
);
return this.pairAbi.getAllPairsTotalFeePercent(addresses);
},
{
cache: false,
Expand Down
12 changes: 12 additions & 0 deletions src/modules/pair/services/pair.abi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@ export class PairAbiService
return response.firstValue.valueOf().toNumber();
}

async getAllPairsTotalFeePercent(
pairAddresses: string[],
): Promise<number[]> {
return getAllKeys<number>(
this.cachingService,
pairAddresses,
'pair.totalFeePercent',
this.totalFeePercent.bind(this),
CacheTtlInfo.ContractState,
);
}

@ErrorLoggerAsync({
logArgs: true,
})
Expand Down
Loading