Skip to content

Commit b83f428

Browse files
arcoravenfarhanW3
andauthored
feat: Add subscription delay envvar (#513)
* feat: Add subscription delay envvar * removed +1 of blockNumber --------- Co-authored-by: farhanW3 <farhan@thirdweb.com>
1 parent f445632 commit b83f428

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

src/utils/env.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export const env = createEnv({
6767
SDK_BATCH_TIME_LIMIT: z.coerce.number().default(0),
6868
SDK_BATCH_SIZE_LIMIT: z.coerce.number().default(100),
6969
ENABLE_KEYPAIR_AUTH: boolSchema("false"),
70+
CONTRACT_SUBSCRIPTIONS_DELAY_SECONDS: z.coerce
71+
.number()
72+
.nonnegative()
73+
.default(0),
7074
},
7175
clientPrefix: "NEVER_USED",
7276
client: {},
@@ -88,6 +92,8 @@ export const env = createEnv({
8892
SDK_BATCH_TIME_LIMIT: process.env.SDK_BATCH_TIME_LIMIT,
8993
SDK_BATCH_SIZE_LIMIT: process.env.SDK_BATCH_SIZE_LIMIT,
9094
ENABLE_KEYPAIR_AUTH: process.env.ENABLE_KEYPAIR_AUTH,
95+
CONTRACT_SUBSCRIPTIONS_DELAY_SECONDS:
96+
process.env.CONTRACT_SUBSCRIPTIONS_DELAY_SECONDS,
9197
},
9298
onValidationError: (error: ZodError) => {
9399
console.error(

src/worker/indexers/chainIndexerRegistry.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import cron from "node-cron";
22
import { getConfig } from "../../utils/cache/getConfig";
3+
import { env } from "../../utils/env";
34
import { getBlockTimeSeconds } from "../../utils/indexer/getBlockTime";
45
import { logger } from "../../utils/logger";
56
import { createChainIndexerTask } from "../tasks/chainIndexer";
@@ -18,6 +19,8 @@ export const addChainIndexer = async (chainId: number) => {
1819

1920
let processStarted = false;
2021
const config = await getConfig();
22+
23+
// Estimate block time.
2124
const blockTimeSeconds = await getBlockTimeSeconds(chainId);
2225

2326
const blocksIn5Seconds = Math.round((1 / blockTimeSeconds) * 5);
@@ -26,7 +29,17 @@ export const addChainIndexer = async (chainId: number) => {
2629
blocksIn5Seconds * 4,
2730
);
2831

29-
const handler = await createChainIndexerTask(chainId, maxBlocksToIndex);
32+
// Compute block offset based on delay.
33+
// Example: 10s delay with a 3s block time = 4 blocks offset
34+
const toBlockOffset = env.CONTRACT_SUBSCRIPTIONS_DELAY_SECONDS
35+
? Math.ceil(env.CONTRACT_SUBSCRIPTIONS_DELAY_SECONDS / blockTimeSeconds)
36+
: 0;
37+
38+
const handler = await createChainIndexerTask({
39+
chainId,
40+
maxBlocksToIndex,
41+
toBlockOffset,
42+
});
3043

3144
const cronSchedule = createScheduleSeconds(
3245
Math.max(Math.round(blockTimeSeconds), 1),

src/worker/tasks/chainIndexer.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,13 @@ const indexTransactionReceipts = async ({
268268
}
269269
};
270270

271-
export const createChainIndexerTask = async (
272-
chainId: number,
273-
maxBlocksToIndex: number,
274-
) => {
271+
export const createChainIndexerTask = async (args: {
272+
chainId: number;
273+
maxBlocksToIndex: number;
274+
toBlockOffset: number;
275+
}) => {
276+
const { chainId, maxBlocksToIndex, toBlockOffset } = args;
277+
275278
const chainIndexerTask = async () => {
276279
try {
277280
await prisma.$transaction(
@@ -287,7 +290,8 @@ export const createChainIndexerTask = async (
287290
const sdk = await getSdk({ chainId });
288291

289292
const provider = sdk.getProvider();
290-
const currentBlockNumber = await provider.getBlockNumber();
293+
const currentBlockNumber =
294+
(await provider.getBlockNumber()) - toBlockOffset;
291295

292296
// check if up-to-date
293297
if (lastIndexedBlock >= currentBlockNumber) {
@@ -296,8 +300,8 @@ export const createChainIndexerTask = async (
296300

297301
// limit max block numbers
298302
let toBlockNumber = currentBlockNumber;
299-
if (currentBlockNumber - (lastIndexedBlock + 1) > maxBlocksToIndex) {
300-
toBlockNumber = lastIndexedBlock + 1 + maxBlocksToIndex;
303+
if (currentBlockNumber - lastIndexedBlock > maxBlocksToIndex) {
304+
toBlockNumber = lastIndexedBlock + maxBlocksToIndex;
301305
}
302306

303307
const subscribedContracts = await getContractSubscriptionsByChainId(
@@ -315,14 +319,14 @@ export const createChainIndexerTask = async (
315319
indexContractEvents({
316320
pgtx,
317321
chainId,
318-
fromBlockNumber: lastIndexedBlock + 1,
322+
fromBlockNumber: lastIndexedBlock,
319323
toBlockNumber,
320324
subscribedContractAddresses,
321325
}),
322326
indexTransactionReceipts({
323327
pgtx,
324328
chainId,
325-
fromBlockNumber: lastIndexedBlock + 1,
329+
fromBlockNumber: lastIndexedBlock,
326330
toBlockNumber,
327331
subscribedContractAddresses,
328332
}),

0 commit comments

Comments
 (0)