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

Add market prices to cache #362

Merged
merged 7 commits into from
Mar 18, 2025
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 packages/api/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Query {
epochs(marketId: Int): [EpochType!]!
indexCandles(address: String!, chainId: Int!, epochId: String!, from: Int!, interval: Int!, to: Int!): [CandleType!]!
indexPriceAtTime(address: String!, chainId: Int!, epochId: String!, timestamp: Int!): CandleType
legacyMarketCandles(address: String!, chainId: Int!, epochId: String!, from: Int!, interval: Int!, to: Int!): [CandleType!]!
market(address: String!, chainId: Int!): MarketType
marketCandles(address: String!, chainId: Int!, epochId: String!, from: Int!, interval: Int!, to: Int!): [CandleType!]!
markets: [MarketType!]!
Expand Down
34 changes: 34 additions & 0 deletions packages/api/src/graphql/resolvers/CandleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,40 @@ export class CandleResolver {
@Arg('from', () => Int) from: number,
@Arg('to', () => Int) to: number,
@Arg('interval', () => Int) interval: number
): Promise<CandleType[]> {
const resourcePerformanceManager = ResourcePerformanceManager.getInstance();
const resourcePerformance =
resourcePerformanceManager.getResourcePerformanceFromChainAndAddress(
chainId,
address
);

if (!resourcePerformance) {
throw new Error(
`Resource performance not initialized for ${chainId}-${address}`
);
}

const prices = await resourcePerformance.getMarketPrices(
from,
to,
interval,
chainId,
address,
epochId
);

return prices;
}

@Query(() => [CandleType])
async legacyMarketCandles(
@Arg('chainId', () => Int) chainId: number,
@Arg('address', () => String) address: string,
@Arg('epochId', () => String) epochId: string,
@Arg('from', () => Int) from: number,
@Arg('to', () => Int) to: number,
@Arg('interval', () => Int) interval: number
): Promise<CandleType[]> {
try {
const market = await dataSource.getRepository(Market).findOne({
Expand Down
117 changes: 84 additions & 33 deletions packages/api/src/migrations/1741722842647-migration.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,89 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { MigrationInterface, QueryRunner } from 'typeorm';

export class Migration1741722842647 implements MigrationInterface {
name = 'Migration1741722842647'
name = 'Migration1741722842647';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE INDEX "IDX_02755ce1b56a981eef76c0b59b" ON "epoch" ("marketId") `);
await queryRunner.query(`CREATE INDEX "IDX_f89ec06faf22da268399ae6a9b" ON "epoch" ("epochId") `);
await queryRunner.query(`CREATE INDEX "IDX_187fa56af532560ce204719ea3" ON "resource_price" ("resourceId") `);
await queryRunner.query(`CREATE INDEX "IDX_5bbe200849d138539d19b7caa6" ON "resource_price" ("blockNumber") `);
await queryRunner.query(`CREATE INDEX "IDX_a369700ab879af9ef6061c6dbe" ON "resource_price" ("timestamp") `);
await queryRunner.query(`CREATE INDEX "IDX_82453de75cd894e19c42844e70" ON "resource" ("slug") `);
await queryRunner.query(`CREATE INDEX "IDX_58232d6050e212b4a0f7eb02da" ON "market" ("address") `);
await queryRunner.query(`CREATE INDEX "IDX_33f985ce349688238dfeb8560e" ON "market" ("chainId") `);
await queryRunner.query(`CREATE INDEX "IDX_5430e2d7fe1df2bcada2c12deb" ON "event" ("blockNumber") `);
await queryRunner.query(`CREATE INDEX "IDX_2c15918ff289396205521c5f3c" ON "event" ("timestamp") `);
await queryRunner.query(`CREATE INDEX "IDX_a9346cdd1ea1e53a6b87e409ad" ON "market_price" ("timestamp") `);
await queryRunner.query(`CREATE INDEX "IDX_1ebf6f07652ca11d9f4618b64a" ON "collateral_transfer" ("transactionHash") `);
await queryRunner.query(`CREATE INDEX "IDX_927edd2b828777f0052366195e" ON "position" ("positionId") `);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "public"."IDX_927edd2b828777f0052366195e"`);
await queryRunner.query(`DROP INDEX "public"."IDX_1ebf6f07652ca11d9f4618b64a"`);
await queryRunner.query(`DROP INDEX "public"."IDX_a9346cdd1ea1e53a6b87e409ad"`);
await queryRunner.query(`DROP INDEX "public"."IDX_2c15918ff289396205521c5f3c"`);
await queryRunner.query(`DROP INDEX "public"."IDX_5430e2d7fe1df2bcada2c12deb"`);
await queryRunner.query(`DROP INDEX "public"."IDX_33f985ce349688238dfeb8560e"`);
await queryRunner.query(`DROP INDEX "public"."IDX_58232d6050e212b4a0f7eb02da"`);
await queryRunner.query(`DROP INDEX "public"."IDX_82453de75cd894e19c42844e70"`);
await queryRunner.query(`DROP INDEX "public"."IDX_a369700ab879af9ef6061c6dbe"`);
await queryRunner.query(`DROP INDEX "public"."IDX_5bbe200849d138539d19b7caa6"`);
await queryRunner.query(`DROP INDEX "public"."IDX_187fa56af532560ce204719ea3"`);
await queryRunner.query(`DROP INDEX "public"."IDX_f89ec06faf22da268399ae6a9b"`);
await queryRunner.query(`DROP INDEX "public"."IDX_02755ce1b56a981eef76c0b59b"`);
}
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE INDEX "IDX_02755ce1b56a981eef76c0b59b" ON "epoch" ("marketId") `
);
await queryRunner.query(
`CREATE INDEX "IDX_f89ec06faf22da268399ae6a9b" ON "epoch" ("epochId") `
);
await queryRunner.query(
`CREATE INDEX "IDX_187fa56af532560ce204719ea3" ON "resource_price" ("resourceId") `
);
await queryRunner.query(
`CREATE INDEX "IDX_5bbe200849d138539d19b7caa6" ON "resource_price" ("blockNumber") `
);
await queryRunner.query(
`CREATE INDEX "IDX_a369700ab879af9ef6061c6dbe" ON "resource_price" ("timestamp") `
);
await queryRunner.query(
`CREATE INDEX "IDX_82453de75cd894e19c42844e70" ON "resource" ("slug") `
);
await queryRunner.query(
`CREATE INDEX "IDX_58232d6050e212b4a0f7eb02da" ON "market" ("address") `
);
await queryRunner.query(
`CREATE INDEX "IDX_33f985ce349688238dfeb8560e" ON "market" ("chainId") `
);
await queryRunner.query(
`CREATE INDEX "IDX_5430e2d7fe1df2bcada2c12deb" ON "event" ("blockNumber") `
);
await queryRunner.query(
`CREATE INDEX "IDX_2c15918ff289396205521c5f3c" ON "event" ("timestamp") `
);
await queryRunner.query(
`CREATE INDEX "IDX_a9346cdd1ea1e53a6b87e409ad" ON "market_price" ("timestamp") `
);
await queryRunner.query(
`CREATE INDEX "IDX_1ebf6f07652ca11d9f4618b64a" ON "collateral_transfer" ("transactionHash") `
);
await queryRunner.query(
`CREATE INDEX "IDX_927edd2b828777f0052366195e" ON "position" ("positionId") `
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`DROP INDEX "public"."IDX_927edd2b828777f0052366195e"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_1ebf6f07652ca11d9f4618b64a"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_a9346cdd1ea1e53a6b87e409ad"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_2c15918ff289396205521c5f3c"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_5430e2d7fe1df2bcada2c12deb"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_33f985ce349688238dfeb8560e"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_58232d6050e212b4a0f7eb02da"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_82453de75cd894e19c42844e70"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_a369700ab879af9ef6061c6dbe"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_5bbe200849d138539d19b7caa6"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_187fa56af532560ce204719ea3"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_f89ec06faf22da268399ae6a9b"`
);
await queryRunner.query(
`DROP INDEX "public"."IDX_02755ce1b56a981eef76c0b59b"`
);
}
}
72 changes: 47 additions & 25 deletions packages/api/src/performance/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { IntervalStore } from './types';
import * as fs from 'fs';
import * as path from 'path';

const FILE_VERSION = 1;

export async function saveStorageToFile(
storage: IntervalStore,
latestTimestamp: number,
latestResourceTimestamp: number,
latestMarketTimestamp: number,
resourceSlug: string,
resourceName: string,
sectionName: string
Expand Down Expand Up @@ -33,7 +36,9 @@ export async function saveStorageToFile(
filename,
JSON.stringify(
{
latestTimestamp,
fileVersion: FILE_VERSION,
latestResourceTimestamp,
latestMarketTimestamp,
store: storage,
},
(key, value) => (typeof value === 'bigint' ? value.toString() : value),
Expand All @@ -53,7 +58,8 @@ export async function loadStorageFromFile(
sectionName: string
): Promise<
| {
latestTimestamp: number;
latestResourceTimestamp: number;
latestMarketTimestamp: number;
store: IntervalStore;
}
| undefined
Expand All @@ -79,30 +85,46 @@ export async function loadStorageFromFile(
return undefined;
}

const fileContent = await fs.promises.readFile(filename, 'utf-8');
const storage = JSON.parse(fileContent, (key, value) => {
// Convert string numbers that might be bigints back to bigint
if (typeof value === 'string' && /^\d+$/.test(value)) {
try {
return BigInt(value);
} catch {
return value;
try {
const fileContent = await fs.promises.readFile(filename, 'utf-8');
const storage = JSON.parse(fileContent, (key, value) => {
// Convert string numbers that might be bigints back to bigint
if (typeof value === 'string' && /^\d+$/.test(value)) {
try {
return BigInt(value);
} catch {
return value;
}
}
return value;
}) as {
fileVersion: number;
latestResourceTimestamp: number;
latestMarketTimestamp: number;
store: IntervalStore;
};
console.timeEnd(
` ResourcePerformance - processResourceData.${resourceName}.${sectionName}.loadStorage`
);
console.log(` ResourcePerformance - -> Loaded storage from ${filename}`);
if (storage.fileVersion !== FILE_VERSION) {
console.log(
`!! Storage file ${filename} has an unsupported version -${storage.fileVersion}-. Expected -${FILE_VERSION}-`
);
return undefined;
}
return value;
}) as {
latestTimestamp: number;
store: IntervalStore;
};

console.timeEnd(
` ResourcePerformance - processResourceData.${resourceName}.${sectionName}.loadStorage`
);
console.log(` ResourcePerformance - -> Loaded storage from ${filename}`);
return {
latestTimestamp: storage.latestTimestamp,
store: storage.store,
};
return {
latestResourceTimestamp: storage.latestResourceTimestamp,
latestMarketTimestamp: storage.latestMarketTimestamp,
store: storage.store,
};
} catch (error) {
console.error(`!! Error loading storage from ${filename}: ${error}`);
console.timeEnd(
` ResourcePerformance - processResourceData.${resourceName}.${sectionName}.loadStorage`
);
return undefined;
}
}

export async function clearStorageFiles(): Promise<void> {
Expand Down
Loading