Skip to content
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
1 change: 1 addition & 0 deletions src/api/controllers/LiquidityPoolController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ export class LiquidityPoolController extends BaseApiController {
tick.close = 1 / tick.close;
tick.high = 1 / tick.high;
tick.low = 1 / tick.low;
tick.volume = 1 / tick.volume;
});
}

Expand Down
40 changes: 3 additions & 37 deletions src/jobs/UpdateLiquidityPoolTvlJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class UpdateLiquidityPoolTvlJob extends BaseJob {

return (
this._liquidityPoolState.liquidityPool.tokenA
? this.updateNonAdaPoolTvl(this._liquidityPoolState.liquidityPool)
? this.updateNonAdaPoolTvl()
: this.updateAdaPoolTvl(this._liquidityPoolState.liquidityPool)
).finally(() => {
queue.dispatch(new UpdateLiquidityPoolTicks(this._liquidityPoolState));
Expand All @@ -48,42 +48,8 @@ export class UpdateLiquidityPoolTvlJob extends BaseJob {
});
}

private async updateNonAdaPoolTvl(liquidityPool: LiquidityPool): Promise<any> {
const retrieveLiquidityPool: any = (token: Asset) => {
return dbService.query((manager: EntityManager) => {
return manager.createQueryBuilder(LiquidityPool, 'pools')
.leftJoinAndSelect('pools.tokenA', 'tokenA')
.leftJoinAndSelect('pools.tokenB', 'tokenB')
.leftJoinAndSelect('pools.latestState', 'latestState')
.where('pools.tokenA IS NULL')
.andWhere('pools.tokenB.id = :tokenBId', {
tokenBId: token.id,
})
.orderBy('latestState.tvl', 'DESC')
.limit(1)
.getOne();
});
};

const tokenAPool: LiquidityPool | null = await retrieveLiquidityPool(liquidityPool.tokenA);
const tokenBPool: LiquidityPool | null = await retrieveLiquidityPool(liquidityPool.tokenB);

if (! tokenAPool || ! tokenBPool || ! tokenAPool.latestState || ! tokenBPool.latestState) {
this._liquidityPoolState.tvl = 0;

return dbService.transaction((manager: EntityManager) => manager.save(this._liquidityPoolState));
}

const tokenADecimals: number = liquidityPool.tokenA?.decimals ?? 0;
const tokenBDecimals: number = liquidityPool.tokenB.decimals;

const poolAPrice: number = tokenAPool.latestState.reserveB !== 0 ? (tokenAPool.latestState.reserveA / 10**6) / (tokenAPool.latestState.reserveB / 10**tokenADecimals) : 0;
const poolBPrice: number = tokenBPool.latestState.reserveB !== 0 ? (tokenBPool.latestState.reserveA / 10**6) / (tokenBPool.latestState.reserveB / 10**tokenBDecimals) : 0;

const reserveAValue: number = (Math.min(this._liquidityPoolState.reserveA, tokenAPool.latestState.reserveB) / 10**tokenADecimals) * poolAPrice;
const reserveBValue: number = (Math.min(this._liquidityPoolState.reserveB, tokenBPool.latestState.reserveB) / 10**tokenBDecimals) * poolBPrice;

this._liquidityPoolState.tvl = Math.floor((reserveAValue + reserveBValue) * 10**6);
private async updateNonAdaPoolTvl(): Promise<any> {
this._liquidityPoolState.tvl = 2 * this._liquidityPoolState.reserveA;

return dbService.transaction((manager: EntityManager) => {
return manager.save(this._liquidityPoolState);
Expand Down