Skip to content

Commit

Permalink
fix: use correct swaps table for volume
Browse files Browse the repository at this point in the history
  • Loading branch information
grinry committed Nov 12, 2024
1 parent 57c54b1 commit 611c4b8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
24 changes: 12 additions & 12 deletions src/controllers/sdex/volume.utils.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import { bignumber } from 'mathjs';

import { swapRepository } from 'database/repository/swap-repository';
import { swapRepositoryV2 } from 'database/repository/swap-repository-v2';
import { areAddressesEqual } from 'utils/compare';

export async function prepareSdexVolume(chainId: number, days = 1) {
const last24hSwaps = await swapRepository.loadSwaps(days, chainId);
const last24hSwaps = await swapRepositoryV2.loadSwaps(days, chainId);

const result: {
token: string;
volume: string;
}[] = [];

last24hSwaps.map((swap) => {
const baseIndex = result.findIndex((s) => areAddressesEqual(s.token, swap.baseId));
const baseIndex = result.findIndex((s) => areAddressesEqual(s.token, swap.base.address));
if (baseIndex < 0) {
result.push({
token: swap.baseId,
volume: bignumber(swap.baseFlow).abs().toString(),
token: swap.base.address,
volume: swap.baseAmount,
});
} else {
result[baseIndex] = {
token: swap.baseId,
volume: bignumber(swap.baseFlow).abs().plus(result[baseIndex].volume).toString(),
token: swap.base.address,
volume: bignumber(swap.baseAmount).plus(result[baseIndex].volume).toString(),
};
}

const quoteIndex = result.findIndex((s) => areAddressesEqual(s.token, swap.quoteId));
const quoteIndex = result.findIndex((s) => areAddressesEqual(s.token, swap.quote.address));
if (quoteIndex < 0) {
result.push({
token: swap.quoteId,
volume: bignumber(swap.quoteFlow).abs().toString(),
token: swap.quote.address,
volume: bignumber(swap.quoteAmount).toString(),
});
} else {
result[quoteIndex] = {
token: swap.quoteId,
volume: bignumber(swap.quoteFlow).abs().plus(result[quoteIndex].volume).toString(),
token: swap.quote.address,
volume: bignumber(swap.quoteAmount).plus(result[quoteIndex].volume).toString(),
};
}
});
Expand Down
17 changes: 9 additions & 8 deletions src/database/repository/swap-repository-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ export const swapRepositoryV2 = {
orderBy: desc(swapsTableV2.block),
}),
loadSwaps: (days = 1, chainId?: number) =>
db
.select()
.from(swapsTableV2)
.where(
and(
chainId ? eq(swapsTableV2.chainId, chainId) : undefined,
gte(swapsTableV2.tickAt, dayjs().subtract(days, 'days').toDate()),
),
db.query.swapsTableV2.findMany({
with: {
base: true,
quote: true,
},
where: and(
chainId ? eq(swapsTableV2.chainId, chainId) : undefined,
gte(swapsTableV2.tickAt, dayjs().subtract(days, 'days').toDate()),
),
}),
};

0 comments on commit 611c4b8

Please sign in to comment.