Skip to content

Commit

Permalink
historical price range ALL (#1534)
Browse files Browse the repository at this point in the history
* historical price range ALL

* skip OHLC

* string timestamp
  • Loading branch information
gmbronco authored Jan 28, 2025
1 parent 57ff22e commit 05bbbc0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tender-cooks-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'backend': minor
---

historical price range ALL
1 change: 1 addition & 0 deletions apps/api/gql/generated-schema-ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3327,6 +3327,7 @@ export const schema = gql`
}
enum GqlTokenChartDataRange {
ALL
NINETY_DAY
ONE_HUNDRED_EIGHTY_DAY
ONE_YEAR
Expand Down
8 changes: 7 additions & 1 deletion apps/api/gql/generated-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2232,7 +2232,13 @@ export interface GqlTokenCandlestickChartDataItem {
timestamp: Scalars['Int'];
}

export type GqlTokenChartDataRange = 'NINETY_DAY' | 'ONE_HUNDRED_EIGHTY_DAY' | 'ONE_YEAR' | 'SEVEN_DAY' | 'THIRTY_DAY';
export type GqlTokenChartDataRange =
| 'ALL'
| 'NINETY_DAY'
| 'ONE_HUNDRED_EIGHTY_DAY'
| 'ONE_YEAR'
| 'SEVEN_DAY'
| 'THIRTY_DAY';

export interface GqlTokenData {
__typename?: 'GqlTokenData';
Expand Down
1 change: 1 addition & 0 deletions apps/api/gql/schema/token.gql
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ enum GqlTokenChartDataRange {
NINETY_DAY
ONE_HUNDRED_EIGHTY_DAY
ONE_YEAR
ALL
}

type GqlTokenCandlestickChartDataItem {
Expand Down
37 changes: 37 additions & 0 deletions modules/token/lib/token-price.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,43 @@ export class TokenPriceService {
range: GqlTokenChartDataRange,
chain: Chain,
): Promise<PrismaTokenPrice[]> {
if (range === 'ALL') {
const rawRecords = await prisma.$queryRaw<
{
tokenAddress: string;
chain: Chain;
daily_timestamp: number;
price: number;
}[]
>`SELECT
"tokenAddress",
chain,
FLOOR("timestamp" / 86400) * 86400 AS daily_timestamp,
ROUND(AVG(price)::NUMERIC, 2) AS price
FROM "PrismaTokenPrice"
WHERE "tokenAddress" = ANY(${tokenAddresses})
AND "chain" = ${chain}::"Chain"
GROUP BY
"tokenAddress",
chain,
FLOOR("timestamp" / 86400) * 86400
ORDER BY
daily_timestamp DESC`;

const records = rawRecords.map((record) => ({
...record,
timestamp: record.daily_timestamp,
updatedAt: new Date(record.daily_timestamp * 1000),
updatedBy: '',
low: record.price, // not returned by the graphql query
high: record.price, // not returned by the graphql query
open: record.price, // not returned by the graphql query
close: record.price, // not returned by the graphql query
}));

return records;
}

const startTimestamp = this.getStartTimestampFromRange(range);

return prisma.prismaTokenPrice.findMany({
Expand Down

0 comments on commit 05bbbc0

Please sign in to comment.