Skip to content

Commit 48f13b8

Browse files
committed
feat: reduce number of order database upserts in order validation cronjob, reduce rpc calls
- lowers the amount of upserts done, by only updating orders that have their validity or error codes changed. Previously, it was updating all orders that we're checked, resulting in unnecessary cache invalidations - properly utilizes the checkMakerOrders method to prevent unnecessary calls to the RPC endpoint, which might have been related with our infura issues.
1 parent 3ffbb0d commit 48f13b8

File tree

1 file changed

+71
-31
lines changed

1 file changed

+71
-31
lines changed

src/services/SupabaseDataService.ts

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { DataDatabase as KyselyDataDatabase } from "../types/kyselySupabase
2222
import type { Database as DataDatabase } from "../types/supabaseData.js";
2323
import { BaseSupabaseService } from "./BaseSupabaseService.js";
2424
import { EvmClientFactory } from "../client/evmClient.js";
25+
import _ from "lodash";
2526

2627
@singleton()
2728
export class SupabaseDataService extends BaseSupabaseService<KyselyDataDatabase> {
@@ -247,50 +248,89 @@ export class SupabaseDataService extends BaseSupabaseService<KyselyDataDatabase>
247248
tokenIds: string[];
248249
chainId: number;
249250
}) {
250-
console.log("[marketplace-api] Validating orders by token IDs", tokenIds);
251+
console.log(
252+
"[marketplace-api] Validating orders by token IDs",
253+
tokenIds,
254+
"on chain",
255+
chainId,
256+
);
251257
const ordersToUpdate: {
252258
id: string;
253259
invalidated: boolean;
254260
validator_codes: OrderValidatorCode[];
255261
}[] = [];
256-
for (const tokenId of tokenIds) {
257-
// Fetch all orders for token ID from database
258-
const { data: matchingOrders, error } = await this.getOrdersByTokenId({
259-
tokenId,
260-
chainId,
261-
});
262-
263-
if (error) {
262+
const getOrdersResults = await Promise.all(
263+
tokenIds.map(async (tokenId) =>
264+
this.getOrdersByTokenId({
265+
tokenId,
266+
chainId,
267+
}),
268+
),
269+
);
270+
271+
if (getOrdersResults.some((res) => res.error)) {
272+
throw new Error(
273+
`[SupabaseDataService::validateOrderByTokenId] Error fetching orders: ${getOrdersResults.find((res) => res.error)?.error?.message}`,
274+
);
275+
}
276+
277+
const matchingOrders = getOrdersResults
278+
.flatMap((res) => res.data)
279+
.filter((x) => x !== null);
280+
281+
// Validate orders using logic in the SDK
282+
const hec = new HypercertExchangeClient(
283+
chainId,
284+
// @ts-expect-error Typing issue with provider
285+
EvmClientFactory.createEthersClient(chainId),
286+
);
287+
const validationResults = await hec.checkOrdersValidity(matchingOrders);
288+
289+
// Determine all orders that have changed validity or validator codes
290+
for (const order of matchingOrders) {
291+
const validationResult = validationResults.find(
292+
(result) => result.id === order.id,
293+
);
294+
295+
if (!validationResult) {
264296
throw new Error(
265-
`[SupabaseDataService::validateOrderByTokenId] Error fetching orders: ${error.message}`,
297+
`[SupabaseDataService::validateOrderByTokenId] No validation result found for order ${order.id}`,
266298
);
267299
}
268300

269-
if (!matchingOrders) {
270-
console.warn(
271-
`[SupabaseDataService::validateOrderByTokenId] No orders found for tokenId: ${tokenId}`,
272-
);
301+
const orderValidity = !order.invalidated;
302+
303+
if (validationResult.valid !== orderValidity) {
304+
ordersToUpdate.push({
305+
id: order.id,
306+
invalidated: !validationResult.valid,
307+
validator_codes: validationResult.validatorCodes,
308+
});
273309
continue;
274310
}
275311

276-
// Validate orders using logic in the SDK
277-
const hec = new HypercertExchangeClient(
278-
chainId,
279-
// @ts-expect-error Typing issue with provider
280-
EvmClientFactory.createEthersClient(chainId),
281-
);
282-
const validationResults = await hec.checkOrdersValidity(matchingOrders);
283-
284-
// Determine which orders to update in DB, and update them
285-
ordersToUpdate.push(
286-
...validationResults.map(({ validatorCodes, id, valid }) => ({
287-
id,
288-
invalidated: !valid,
289-
validator_codes: validatorCodes,
290-
})),
291-
);
312+
if (
313+
order.validator_codes === null &&
314+
validationResult.validatorCodes.every(
315+
(code) => code === OrderValidatorCode.ORDER_EXPECTED_TO_BE_VALID,
316+
)
317+
) {
318+
continue;
319+
}
320+
321+
if (!_.isEqual(validationResult.validatorCodes, order.validator_codes)) {
322+
ordersToUpdate.push({
323+
id: order.id,
324+
invalidated: !validationResult.valid,
325+
validator_codes: validationResult.validatorCodes,
326+
});
327+
}
292328
}
293-
console.log("[marketplace-api] Invalidating orders", ordersToUpdate);
329+
330+
console.log(
331+
"[marketplace-api] Updating orders from validation results",
332+
ordersToUpdate,
333+
);
294334
await this.updateOrders(ordersToUpdate);
295335
return ordersToUpdate;
296336
}

0 commit comments

Comments
 (0)