diff --git a/.changeset/tender-cooks-compare.md b/.changeset/tender-cooks-compare.md new file mode 100644 index 000000000..57b7cf9a7 --- /dev/null +++ b/.changeset/tender-cooks-compare.md @@ -0,0 +1,5 @@ +--- +'backend': minor +--- + +historical price range ALL diff --git a/apps/api/gql/generated-schema-ast.ts b/apps/api/gql/generated-schema-ast.ts index 7aff0e323..812d62000 100644 --- a/apps/api/gql/generated-schema-ast.ts +++ b/apps/api/gql/generated-schema-ast.ts @@ -3327,6 +3327,7 @@ export const schema = gql` } enum GqlTokenChartDataRange { + ALL NINETY_DAY ONE_HUNDRED_EIGHTY_DAY ONE_YEAR diff --git a/apps/api/gql/generated-schema.ts b/apps/api/gql/generated-schema.ts index 828c69c7a..295e00082 100644 --- a/apps/api/gql/generated-schema.ts +++ b/apps/api/gql/generated-schema.ts @@ -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'; diff --git a/apps/api/gql/schema/token.gql b/apps/api/gql/schema/token.gql index f847bf9ac..35625a4aa 100644 --- a/apps/api/gql/schema/token.gql +++ b/apps/api/gql/schema/token.gql @@ -344,6 +344,7 @@ enum GqlTokenChartDataRange { NINETY_DAY ONE_HUNDRED_EIGHTY_DAY ONE_YEAR + ALL } type GqlTokenCandlestickChartDataItem { diff --git a/modules/token/lib/token-price.service.ts b/modules/token/lib/token-price.service.ts index 182f4dbae..a29d6f8b7 100644 --- a/modules/token/lib/token-price.service.ts +++ b/modules/token/lib/token-price.service.ts @@ -157,6 +157,43 @@ export class TokenPriceService { range: GqlTokenChartDataRange, chain: Chain, ): Promise { + 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({