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-2465] Fix tokens previous 7 days price #1374

Merged
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
1 change: 1 addition & 0 deletions src/modules/tokens/models/tokens.filter.args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum TokensSortableFields {
PREVIOUS_24H_PRICE = 'previous_24h_price',
PREVIOUS_7D_PRICE = 'previous_7d_price',
PREVIOUS_24H_VOLUME = 'previous_24h_volume',
PRICE_CHANGE_7D = 'price_change_7d',
PRICE_CHANGE_24H = 'price_change_24h',
VOLUME_CHANGE_24H = 'volume_change_24h',
TRADES_COUNT_CHANGE_24H = 'trades_count_change_24h',
Expand Down
16 changes: 16 additions & 0 deletions src/modules/tokens/services/token.compute.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,22 @@ export class TokenComputeService implements ITokenComputeService {
return currentPriceBN.dividedBy(previous24hPrice).toNumber();
}

async computeTokenPriceChange7d(tokenID: string): Promise<number> {
const [currentPrice, previous7dPrice] = await Promise.all([
this.tokenPriceDerivedUSD(tokenID),
this.tokenPrevious7dPrice(tokenID),
]);

const currentPriceBN = new BigNumber(currentPrice);
const previous7dPriceBN = new BigNumber(previous7dPrice);

if (previous7dPriceBN.isZero()) {
return 0;
}

return currentPriceBN.dividedBy(previous7dPriceBN).toNumber();
}

@ErrorLoggerAsync({
logArgs: true,
})
Expand Down
7 changes: 7 additions & 0 deletions src/modules/tokens/services/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ export class TokenService {
),
);
break;
case TokensSortableFields.PRICE_CHANGE_7D:
sortFieldData = await Promise.all(
tokenIDs.map((tokenID) =>
this.tokenCompute.computeTokenPriceChange7d(tokenID),
),
);
break;
case TokensSortableFields.PRICE_CHANGE_24H:
sortFieldData = await Promise.all(
tokenIDs.map((tokenID) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class TimescaleDBQueryService implements AnalyticsQueryInterface {
.select("time_bucket_gapfill('1 day', time) as day")
.addSelect(
`locf(last(last, time) ${
start ? `, (${previousValue.getQuery()})` : ''
start || time ? `, (${previousValue.getQuery()})` : ''
}) as last`,
)
.where('series = :series', { series })
Expand Down
Loading