Skip to content

Commit 4f60773

Browse files
authored
fix: speed up verification page (#256)
Signed-off-by: Matt Rice <matthewcrice32@gmail.com>
1 parent 8644cf6 commit 4f60773

File tree

9 files changed

+515
-8
lines changed

9 files changed

+515
-8
lines changed

libs/src/oracle-sdk-v2/services/managedv2/gql/factory.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import type { ChainId, OOV2GraphEntity, OracleType } from "@shared/types";
22
import { parsePriceRequestGraphEntity } from "@shared/utils";
33
import type { Address } from "wagmi";
44
import type { Handlers, Service, ServiceFactory } from "../../../types";
5-
import { getPriceRequests } from "./queries";
5+
import { getPriceRequests, getRecentProposals } from "./queries";
66

77
export type Config = {
88
urls: string[];
99
chainId: ChainId;
1010
address: string;
1111
type: OracleType;
12+
enableFastVerifyQuery?: boolean;
13+
verifyQueryDaysBack?: number;
1214
};
1315

1416
export const Factory =
@@ -71,6 +73,44 @@ export const Factory =
7173
}
7274
throw err;
7375
}
76+
77+
async function fetchRecentForVerify({
78+
urls,
79+
chainId,
80+
address,
81+
type,
82+
verifyQueryDaysBack = 7,
83+
}: Config) {
84+
let err;
85+
for (const url of urls) {
86+
try {
87+
const requests = (await getRecentProposals(
88+
url,
89+
chainId,
90+
type,
91+
verifyQueryDaysBack,
92+
)) as OOV2GraphEntity[];
93+
94+
handlers.requests?.(
95+
requests.map((request) =>
96+
parsePriceRequestGraphEntity(
97+
request,
98+
chainId,
99+
address as Address,
100+
type,
101+
),
102+
),
103+
);
104+
105+
return;
106+
} catch (error) {
107+
err = error;
108+
console.warn(`Failed to fetch recent proposals from ${url}:`, error);
109+
}
110+
}
111+
throw err;
112+
}
113+
74114
// if we need to bring back incremental loading
75115
// async function fetchIncremental({ url, chainId, address, type }: Config) {
76116
// for await (const requests of getPriceRequestsIncremental(
@@ -91,7 +131,23 @@ export const Factory =
91131
// }
92132
// }
93133
async function tick() {
94-
await fetch(config);
134+
if (config.enableFastVerifyQuery) {
135+
// Run fast query first for immediate verify page data
136+
try {
137+
await fetchRecentForVerify(config);
138+
} catch (error) {
139+
console.warn(
140+
"Fast verify query failed, falling back to full query:",
141+
error,
142+
);
143+
}
144+
// Run full query in background (don't await, but handle errors)
145+
fetch(config).catch((err) => {
146+
console.warn("Background full query failed:", err);
147+
});
148+
} else {
149+
await fetch(config);
150+
}
95151
}
96152

97153
return {

libs/src/oracle-sdk-v2/services/managedv2/gql/queries.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,116 @@ function makeTimeBasedQuery(
284284

285285
return query;
286286
}
287+
288+
export async function getRecentProposals(
289+
url: string,
290+
chainId: ChainId,
291+
oracleType: OracleType,
292+
daysBack: number = 7,
293+
) {
294+
const chainName = chainsById[chainId];
295+
const queryName = makeQueryName(oracleType, chainName) + "RecentProposals";
296+
const cutoffTime = Math.floor(Date.now() / 1000) - daysBack * 24 * 60 * 60;
297+
298+
const result = await fetchRecentProposals(
299+
url,
300+
queryName,
301+
oracleType,
302+
cutoffTime,
303+
);
304+
return result;
305+
}
306+
307+
function makeRecentProposalsQuery(
308+
queryName: string,
309+
oracleType: OracleType,
310+
first: number,
311+
cutoffTime: number,
312+
lastProposalTime?: number,
313+
) {
314+
const whereClause = lastProposalTime
315+
? `where: { proposalTimestamp_gt: "${cutoffTime}", proposalTimestamp_lt: "${lastProposalTime}", proposalTimestamp_not: null }`
316+
: `where: { proposalTimestamp_gt: "${cutoffTime}", proposalTimestamp_not: null }`;
317+
318+
const query = gql`
319+
query ${queryName} {
320+
optimisticPriceRequests(
321+
orderBy: proposalTimestamp,
322+
orderDirection: desc,
323+
first: ${first},
324+
${whereClause}
325+
) {
326+
id
327+
identifier
328+
ancillaryData
329+
time
330+
requester
331+
currency
332+
reward
333+
finalFee
334+
proposer
335+
proposedPrice
336+
proposalExpirationTimestamp
337+
disputer
338+
settlementPrice
339+
settlementPayout
340+
settlementRecipient
341+
state
342+
requestTimestamp
343+
requestBlockNumber
344+
requestHash
345+
requestLogIndex
346+
proposalTimestamp
347+
proposalBlockNumber
348+
proposalHash
349+
proposalLogIndex
350+
disputeTimestamp
351+
disputeBlockNumber
352+
disputeHash
353+
disputeLogIndex
354+
settlementTimestamp
355+
settlementBlockNumber
356+
settlementHash
357+
settlementLogIndex
358+
customLiveness
359+
bond
360+
eventBased
361+
}
362+
}
363+
`;
364+
return query;
365+
}
366+
367+
async function fetchRecentProposals(
368+
url: string,
369+
queryName: string,
370+
oracleType: OracleType,
371+
cutoffTime: number,
372+
) {
373+
const result: (OOV1GraphEntity | OOV2GraphEntity)[] = [];
374+
const first = 1000;
375+
let lastProposalTime: number | undefined = undefined;
376+
377+
let requests = await fetchPriceRequests(
378+
url,
379+
makeRecentProposalsQuery(queryName, oracleType, first, cutoffTime),
380+
);
381+
382+
while (requests.length === first) {
383+
result.push(...requests);
384+
lastProposalTime = Number(requests[requests.length - 1].proposalTimestamp);
385+
requests = await fetchPriceRequests(
386+
url,
387+
makeRecentProposalsQuery(
388+
queryName,
389+
oracleType,
390+
first,
391+
cutoffTime,
392+
lastProposalTime,
393+
),
394+
);
395+
}
396+
397+
result.push(...requests);
398+
return result;
399+
}

libs/src/oracle-sdk-v2/services/oracles/factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export type GqlConfig = {
1212
chainId: ChainId;
1313
type: OracleType;
1414
address: string;
15+
enableFastVerifyQuery?: boolean;
16+
verifyQueryDaysBack?: number;
1517
};
1618
export type Config = GqlConfig[];
1719

libs/src/oracle-sdk-v2/services/oraclev1/gql/factory.ts

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import type { ChainId, OracleType } from "@shared/types";
22
import { parsePriceRequestGraphEntity } from "@shared/utils";
33
import type { Address } from "wagmi";
44
import type { Handlers, Service, ServiceFactory } from "../../../types";
5-
import { getPriceRequests } from "./queries";
5+
import { getPriceRequests, getRecentProposals } from "./queries";
66

77
export type Config = {
88
urls: string[];
99
chainId: ChainId;
1010
address: string;
1111
type: OracleType;
12+
enableFastVerifyQuery?: boolean;
13+
verifyQueryDaysBack?: number;
1214
};
1315

1416
export const Factory =
@@ -37,6 +39,42 @@ export const Factory =
3739
}
3840
throw err;
3941
}
42+
43+
async function fetchRecentForVerify({
44+
urls,
45+
chainId,
46+
address,
47+
type,
48+
verifyQueryDaysBack = 7,
49+
}: Config) {
50+
let err;
51+
for (const url of urls) {
52+
try {
53+
const requests = await getRecentProposals(
54+
url,
55+
chainId,
56+
type,
57+
verifyQueryDaysBack,
58+
);
59+
handlers?.requests?.(
60+
requests.map((request) =>
61+
parsePriceRequestGraphEntity(
62+
request,
63+
chainId,
64+
address as Address,
65+
type,
66+
),
67+
),
68+
);
69+
return;
70+
} catch (error) {
71+
err = error;
72+
console.warn(`Failed to fetch recent proposals from ${url}:`, error);
73+
}
74+
}
75+
throw err;
76+
}
77+
4078
// if we need to bring back incremental loading
4179
// async function fetchIncremental({ url, chainId, address, type }: Config) {
4280
// for await (const requests of getPriceRequestsIncremental(
@@ -57,7 +95,23 @@ export const Factory =
5795
// }
5896
// }
5997
async function tick() {
60-
await fetch(config);
98+
if (config.enableFastVerifyQuery) {
99+
// Run fast query first for immediate verify page data
100+
try {
101+
await fetchRecentForVerify(config);
102+
} catch (error) {
103+
console.warn(
104+
"Fast verify query failed, falling back to full query:",
105+
error,
106+
);
107+
}
108+
// Run full query in background (don't await, but handle errors)
109+
fetch(config).catch((err) => {
110+
console.warn("Background full query failed:", err);
111+
});
112+
} else {
113+
await fetch(config);
114+
}
61115
}
62116

63117
return {

0 commit comments

Comments
 (0)