跳转到主要内容
永不遗漏:LaserStream 的历史重播确保您可以从断线中恢复,并补充过去 20 分钟区块链活动中缺失的数据。

什么是历史重播?

历史重播是 LaserStream 的一项功能,允许您重播过去最多 3000 个槽位的最近区块链数据(大约 20 分钟的区块链活动)。这对于处理断线和确保实时应用程序中的数据连续性特别有用。
时间窗口有限:历史重播目前仅限于最近 3000 个槽位(大约 20 分钟的区块链活动)。您无法重播过去任意时间点的数据。

处理断线

恢复在短暂断线期间丢失的数据(最多 20 分钟)

引导应用程序

从最近 20 分钟的上下文启动应用程序

分析最近事件

查看最近的交易和账户变更

使用最近数据进行测试

使用真实的最近数据进行测试和开发

工作原理

1

指定起始点

使用 fromSlot 参数设置您的重播起始点(必须在最近 3000 个槽位内)
2

流式传输历史数据

LaserStream 从您指定的槽位开始传送所有事件
3

赶上实时

历史数据流传输直到您达到当前槽位
4

继续实时流式传输

无缝过渡到实时数据流
自动重新连接LaserStream SDK 自动处理重新连接和重播。无需额外代码!

快速开始

  • gRPC
  • WebSocket (即将推出)
import { subscribe, CommitmentLevel, LaserstreamConfig, SubscribeRequest } from 'helius-laserstream';

const subscriptionRequest: SubscribeRequest = {
  transactions: {
    client: {
      accountInclude: ['TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'],
      vote: false,
      failed: false
    }
  },
  commitment: CommitmentLevel.CONFIRMED,
  accounts: {},
  slots: {},
  blocks: {},
  blocksMeta: {},
  entry: {},
  accountsDataSlice: [],
  fromSlot: '224339000' // Start replay from this slot (must be within last 3000 slots)
};

const config: LaserstreamConfig = {
  apiKey: 'YOUR_API_KEY',
  endpoint: 'https://laserstream-mainnet-ewr.helius-rpc.com', // Choose your closest region
};

await subscribe(config, subscriptionRequest, 
  async (data) => {
    console.log('Received data:', data);
  }, 
  async (error) => {
    console.error('Error:', error);
  }
);

配置选项

fromSlot
string
required
开始重播的槽号。必须在重播窗口内(当前槽的最后3000个槽)。示例: "224339000"重要: 槽号必须足够新,以便在20分钟的重播窗口内。

用例

当您的应用程序在短暂断开连接(20分钟内)后重新连接时,可以使用历史重播以确保没有数据丢失:
// Store the last processed slot
let lastProcessedSlot = getLastProcessedSlot();

// Check if the slot is still within the replay window
const 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);
  }
);
使用最近几分钟的上下文启动您的应用程序:
// Get a slot from 10 minutes ago (within the 20-minute window)
const currentSlot = await getCurrentSlot();
const startSlot = currentSlot - 1500; // ~10 minutes ago

const subscriptionRequest: SubscribeRequest = {
  // ... your subscription config
  fromSlot: startSlot.toString()
};
使用最近的历史数据进行测试(限于最近20分钟):
// Test with data from the last 5 minutes
const currentSlot = await getCurrentSlot();
const testStartSlot = currentSlot - 750; // ~5 minutes ago
const testEndSlot = currentSlot - 150; // ~1 minute ago

const subscriptionRequest: SubscribeRequest = {
  // ... your subscription config
  fromSlot: testStartSlot.toString()
};

// Stop processing when reaching test end slot
await subscribe(config, subscriptionRequest, 
  async (data) => {
    if (data.slot >= testEndSlot) {
      // Stop processing
      return;
    }
    await processTestData(data);
  }
);

下一步

I