Skip to content

Commit b6a5c3d

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 5bba419 commit b6a5c3d

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);
251251
const ordersToUpdate: {
252252
id: string;
253253
invalidated: boolean;
254254
validator_codes: OrderValidatorCode[];
255255
}[] = [];
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) {
256+
const getOrdersResults = await Promise.all(
257+
tokenIds.map(async (tokenId) =>
258+
this.getOrdersByTokenId({
259+
tokenId,
260+
chainId,
261+
}),
262+
),
263+
);
264+
265+
if (getOrdersResults.some((res) => res.error)) {
266+
throw new Error(
267+
`[SupabaseDataService::validateOrderByTokenId] Error fetching orders: ${getOrdersResults.find((res) => res.error)?.error?.message}`,
268+
);
269+
}
270+
271+
const matchingOrders = getOrdersResults
272+
.flatMap((res) => res.data)
273+
.filter((x) => x !== null);
274+
275+
// Validate orders using logic in the SDK
276+
const hec = new HypercertExchangeClient(
277+
chainId,
278+
// @ts-expect-error Typing issue with provider
279+
EvmClientFactory.createEthersClient(chainId),
280+
);
281+
const validationResults = await hec.checkOrdersValidity(matchingOrders);
282+
283+
// Determine all orders that have changed validity or validator codes so we don't
284+
// update the order if it hasn't changed
285+
for (const order of matchingOrders) {
286+
const validationResult = validationResults.find(
287+
(result) => result.id === order.id,
288+
);
289+
290+
if (!validationResult) {
264291
throw new Error(
265-
`[SupabaseDataService::validateOrderByTokenId] Error fetching orders: ${error.message}`,
292+
`[SupabaseDataService::validateOrderByTokenId] No validation result found for order ${order.id}`,
266293
);
267294
}
268295

269-
if (!matchingOrders) {
270-
console.warn(
271-
`[SupabaseDataService::validateOrderByTokenId] No orders found for tokenId: ${tokenId}`,
272-
);
296+
const currentOrderIsValid = !order.invalidated;
297+
298+
// If the order validity has changed, we need to update the order and add the validator codes
299+
if (validationResult.valid !== currentOrderIsValid) {
300+
ordersToUpdate.push({
301+
id: order.id,
302+
invalidated: !validationResult.valid,
303+
validator_codes: validationResult.validatorCodes,
304+
});
273305
continue;
274306
}
275307

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-
);
308+
if (
309+
order.validator_codes === null &&
310+
validationResult.validatorCodes.every(
311+
(code) => code === OrderValidatorCode.ORDER_EXPECTED_TO_BE_VALID,
312+
)
313+
) {
314+
// Orders are added to the database by default with validator_codes set to null
315+
// The contract will return an array of ORDER_EXPECTED_TO_BE_VALID if the order is valid
316+
// In this special case we won't have to update the order
317+
continue;
318+
}
319+
320+
// If the validator codes have changed, we need to update the order
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+
"[SupabaseDataService::validateOrderByTokenId] Updating orders from validation results",
332+
ordersToUpdate,
333+
);
294334
await this.updateOrders(ordersToUpdate);
295335
return ordersToUpdate;
296336
}

0 commit comments

Comments
 (0)