// Store the last processed slotlet lastProcessedSlot = getLastProcessedSlot();// Check if the slot is still within the replay windowconst currentSlot = await getCurrentSlot();const maxReplaySlot = currentSlot - 3000;if (lastProcessedSlot < maxReplaySlot) { console.warn('Disconnection too long, some data may be lost'); lastProcessedSlot = maxReplaySlot;}const subscriptionRequest: SubscribeRequest = { // ... your subscription config fromSlot: lastProcessedSlot.toString()};await subscribe(config, subscriptionRequest, async (data) => { // Process data and update last processed slot await processData(data); lastProcessedSlot = data.slot; saveLastProcessedSlot(lastProcessedSlot); });
使用最近的上下文启动
使用最近几分钟的上下文启动您的应用程序:
Report incorrect code
Copy
Ask AI
// Get a slot from 10 minutes ago (within the 20-minute window)const currentSlot = await getCurrentSlot();const startSlot = currentSlot - 1500; // ~10 minutes agoconst subscriptionRequest: SubscribeRequest = { // ... your subscription config fromSlot: startSlot.toString()};
使用最近数据进行测试
使用最近的历史数据进行测试(限于最近20分钟):
Report incorrect code
Copy
Ask AI
// Test with data from the last 5 minutesconst currentSlot = await getCurrentSlot();const testStartSlot = currentSlot - 750; // ~5 minutes agoconst testEndSlot = currentSlot - 150; // ~1 minute agoconst subscriptionRequest: SubscribeRequest = { // ... your subscription config fromSlot: testStartSlot.toString()};// Stop processing when reaching test end slotawait subscribe(config, subscriptionRequest, async (data) => { if (data.slot >= testEndSlot) { // Stop processing return; } await processTestData(data); });