跳转到主要内容
不漏掉任何一步:LaserStream的历史重播确保您可以从断开连接中恢复,并补充过去24小时的区块链活动中遗漏的数据。

什么是历史重播?

历史重放是LaserStream的一项功能,可以重放过去最多216,000个插槽的最近区块链数据(大约24小时的区块链活动)。这对于处理断线并确保实时应用中的数据连续性很有用。
时间窗口有限:历史重播目前限制在过去24小时的区块链活动中。您无法重播过去任意时间点的数据。

处理断开连接

恢复短暂断开连接时丢失的数据(最多24小时)

启动应用程序

使用最近24小时的上下文启动应用程序

分析最近事件

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

使用最近数据测试

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

工作原理

1

指定起始点

使用 fromSlot 参数设置重放的起始点(必须在最近~216,000个插槽内)
2

流式传输历史数据

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

赶超至实时

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

继续实时流传输

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

快速入门

有兴趣尝试 LaserStream 吗?申请 2 天试用;我们会审查每个申请。
import { subscribe, CommitmentLevel, LaserstreamConfig, SubscribeRequest } from 'helius-laserstream';

// Pick a slot within the last ~216,000 slots (≈24 h). For a real start
// value, call `getSlot` first and subtract however far back you want to replay.
const fromSlot = 419_800_000;

const subscriptionRequest: SubscribeRequest = {
  transactions: {
    "token-filter": { // user-defined label for this filter
      accountInclude: ['TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'],
      vote: false,
      failed: false
    }
  },
  commitment: CommitmentLevel.CONFIRMED,
  accounts: {},
  slots: {},
  blocks: {},
  blocksMeta: {},
  entry: {},
  accountsDataSlice: [],
  fromSlot, // u64 slot number; must fall inside the replay window
};

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
number
必填
要开始重播的槽号,作为 u64。必须在重播窗口内(当前槽的最后约216,000个槽)。示例: currentSlot - 1000重要: 槽必须足够最近,以便在24小时重播窗口内。如果您传递的槽号早于窗口,LaserStream将拒绝请求并发出Operation was attempted past the valid range

用例

当您的应用在短暂断开(不到24小时)后重新连接时,您可以使用历史重播来确保不会遗漏任何数据。getCurrentSlot 调用 Helius RPC;lastProcessedSlot 在内存中保留在下方 - 持久化它以满足您应用的需求(Redis,Postgres,文件等)。
async function getCurrentSlot(): Promise<number> {
  const r = await fetch(`https://mainnet.helius-rpc.com/?api-key=${process.env.HELIUS_API_KEY}`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'getSlot', params: [{ commitment: 'confirmed' }] }),
  });
  const { result } = await r.json();
  return result as number;
}

// Load the last slot you processed from wherever you store it.
let lastProcessedSlot = Number(process.env.LAST_PROCESSED_SLOT ?? 0);

// Check if it's still within the replay window
const currentSlot = await getCurrentSlot();
const maxReplaySlot = currentSlot - 216_000;

if (lastProcessedSlot < maxReplaySlot) {
  console.warn('Disconnection too long, some data may be lost');
  lastProcessedSlot = maxReplaySlot;
}

const subscriptionRequest: SubscribeRequest = {
  // ... your subscription config
  fromSlot: lastProcessedSlot, // pass as number, not string
};

await subscribe(config, subscriptionRequest,
  async (data) => {
    // your handler here
    if (data.transaction?.slot) {
      lastProcessedSlot = Number(data.transaction.slot);
      // persist `lastProcessedSlot` here so the next reconnect picks up
    }
  }
);
从最近几分钟的上下文启动您的应用程序:
// Get a slot from 10 minutes ago (within the 24-hour window)
const currentSlot = await getCurrentSlot();
const startSlot = currentSlot - 1500; // ~10 minutes ago

const subscriptionRequest: SubscribeRequest = {
  // ... your subscription config
  fromSlot: startSlot, // u64 number
};
使用最近的历史数据进行测试(限最近24小时):
// 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, // u64 number
};

// Stop processing when reaching the test end slot
const stream = await subscribe(config, subscriptionRequest,
  async (data) => {
    const slot = Number(data.transaction?.slot ?? data.account?.slot ?? 0);
    if (slot >= testEndSlot) {
      stream.cancel();
      return;
    }
    // your test handler here
  }
);

下一步

LaserStream gRPC

了解更多关于gRPC流功能和特性

LaserStream试用

申请2天LaserStream试用,以便在升级订阅之前进行测试

SDK文档

查看完整的SDK文档

联系支持

获取实施帮助