Skip to content

Commit

Permalink
refactor: include pair and tokens in getWithinHeightSorted
Browse files Browse the repository at this point in the history
  • Loading branch information
tmrdlt committed Apr 22, 2024
1 parent 14514a5 commit 332316d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class PairLiquidityInfoHistoryV2DbService {
});
}

getWithinHeightSorted(heightLimit: number) {
getWithinHeightSortedWithPair(heightLimit: number) {
return this.prisma.pairLiquidityInfoHistoryV2.findMany({
where: {
height: {
Expand All @@ -49,6 +49,14 @@ export class PairLiquidityInfoHistoryV2DbService {
{ transactionIndex: 'asc' },
{ logIndex: 'asc' },
],
include: {
pair: {
include: {
token0: true,
token1: true,
},
},
},
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import {
contractAddrToAccountAddr,
} from '@/clients/sdk-client.model';
import { SdkClientService } from '@/clients/sdk-client.service';
import { PairDbService } from '@/database/pair/pair-db.service';
import { PairLiquidityInfoHistoryV2DbService } from '@/database/pair-liquidity-info-history/pair-liquidity-info-history-v2-db.service';
import { decimalToBigInt } from '@/lib/utils';

@Injectable()
export class PairLiquidityInfoHistoryValidatorV2Service {
constructor(
private pairLiquidityInfoHistoryDb: PairLiquidityInfoHistoryV2DbService,
private pairDb: PairDbService,
private mdwClient: MdwHttpClientService,
private sdkClient: SdkClientService,
) {}
Expand All @@ -34,7 +32,7 @@ export class PairLiquidityInfoHistoryValidatorV2Service {
// and take the last entry of every microBlock to get the final reserve in that microBlock
const liquidityEntriesWithinHeightSorted = map(
groupBy(
await this.pairLiquidityInfoHistoryDb.getWithinHeightSorted(
await this.pairLiquidityInfoHistoryDb.getWithinHeightSortedWithPair(
currentHeight - this.VALIDATION_WINDOW_BLOCKS,
),
'microBlockHash',
Expand All @@ -50,16 +48,14 @@ export class PairLiquidityInfoHistoryValidatorV2Service {
let mdwReserve0: bigint | undefined;
let mdwReserve1: bigint | undefined;

const pairWithTokens = (await this.pairDb.get(liquidityEntry?.pairId))!;

try {
// reserve0 is the balance of the pair contract's account of token0
mdwReserve0 = BigInt(
(
await this.mdwClient.getAccountBalanceForContractAtMicroBlockHash(
pairWithTokens.token0.address as ContractAddress,
liquidityEntry.pair.token0.address as ContractAddress,
contractAddrToAccountAddr(
pairWithTokens.address as ContractAddress,
liquidityEntry.pair.address as ContractAddress,
),
liquidityEntry.microBlockHash,
)
Expand All @@ -70,9 +66,9 @@ export class PairLiquidityInfoHistoryValidatorV2Service {
mdwReserve1 = BigInt(
(
await this.mdwClient.getAccountBalanceForContractAtMicroBlockHash(
pairWithTokens.token1.address as ContractAddress,
liquidityEntry.pair.token1.address as ContractAddress,
contractAddrToAccountAddr(
pairWithTokens.address as ContractAddress,
liquidityEntry.pair.address as ContractAddress,
),
liquidityEntry.microBlockHash,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('PairLiquidityInfoHistoryV2DbService', () => {
await prismaService.token.createMany({ data: [token1, token2, token3] });
await prismaService.pair.createMany({ data: [pair1, pair2, pair3] });
await prismaService.pairLiquidityInfoHistoryV2.createMany({
data: [historyEntry1, historyEntry2, historyEntry3, historyEntry4],
data: [historyEntry4, historyEntry2, historyEntry3, historyEntry1],
});
});

Expand Down Expand Up @@ -106,9 +106,9 @@ describe('PairLiquidityInfoHistoryV2DbService', () => {
});
});

describe('getWithinHeightSorted', () => {
describe('getWithinHeightSortedWithPair', () => {
it('should correctly return all entries greater or equal a given height limit sorted ascending', async () => {
const result = await service.getWithinHeightSorted(200002);
const result = await service.getWithinHeightSortedWithPair(200002);
expect(result.map((e) => e.id)).toEqual([
historyEntry2.id,
historyEntry3.id,
Expand All @@ -122,8 +122,8 @@ describe('PairLiquidityInfoHistoryV2DbService', () => {
await service.deleteFromMicroBlockTime(3000000000003n);
const result = await prismaService.pairLiquidityInfoHistoryV2.findMany();
expect(result.map((e) => e.id)).toEqual([
historyEntry1.id,
historyEntry2.id,
historyEntry1.id,
]);
});
});
Expand Down

0 comments on commit 332316d

Please sign in to comment.