Solana 文档LiteSVMTypeScriptAPI 参考

时间与系统变量

用于操作时间和访问系统变量的方法。

请先通过 client.svm.withSysvars() 启用系统变量,再访问系统变量 相关方法。

时钟操作

warpToSlot

warpToSlot(slot: bigint): LiteSVM

将时钟推进到指定的 slot。

client.svm.withSysvars();
// Check current slot
const before = client.svm.getClock();
console.log("Current slot:", before.slot);
// Warp forward
client.svm.warpToSlot(10000n);
const after = client.svm.getClock();
console.log("New slot:", after.slot);

getClock

getClock(): Clock

获取当前时钟系统变量的值。

const clock = client.svm.getClock();
console.log("Slot:", clock.slot);
console.log("Epoch:", clock.epoch);
console.log("Unix timestamp:", clock.unixTimestamp);
console.log("Epoch start timestamp:", clock.epochStartTimestamp);
console.log("Leader schedule epoch:", clock.leaderScheduleEpoch);

setClock

setClock(clock: Clock): LiteSVM

覆盖时钟系统变量。先获取时钟,修改其字段,然后写回。当您需要精确控制多个时钟字段时使用此方法——例如,设置 epoch 或 Unix 时间戳,这些是 warpToSlot 无法更新的。

const clock = client.svm.getClock();
clock.slot = 1000n;
clock.epoch = 10n;
clock.unixTimestamp = 1735689600n;
client.svm.setClock(clock);

warpToSlot 仅更新 slot。当您的程序需要从 Clock 系统变量中读取 epochunix_timestamp 时,请使用 setClock

时钟属性

属性类型描述
slotbigint当前 slot 编号
epochbigint当前 epoch 编号
unixTimestampbigintUnix 时间戳(秒)
epochStartTimestampbigint当前 epoch 的起始时间
leaderScheduleEpochbigintLeader 调度 epoch

Rent

getRent

getRent(): Rent

获取 rent 系统变量。

const rent = client.svm.getRent();
console.log("Lamports per byte-year:", rent.lamportsPerByteYear);
console.log("Exemption threshold:", rent.exemptionThreshold);
console.log("Burn percent:", rent.burnPercent);

minimumBalanceForRentExemption

minimumBalanceForRentExemption(dataLen: bigint): bigint

计算rent豁免所需的最低 lamport 数量。

// Calculate for different data sizes
for (const size of [0n, 100n, 1000n]) {
const min = client.svm.minimumBalanceForRentExemption(size);
console.log(`${size} bytes: ${min} lamports`);
}

Epoch 计划

getEpochSchedule

getEpochSchedule(): EpochSchedule

获取 epoch 计划 sysvar。

const epochSchedule = client.svm.getEpochSchedule();
console.log("Slots per epoch:", epochSchedule.slotsPerEpoch);
console.log(
"Leader schedule slot offset:",
epochSchedule.leaderScheduleSlotOffset
);
console.log("Warmup:", epochSchedule.warmup);
console.log("First normal epoch:", epochSchedule.firstNormalEpoch);
console.log("First normal slot:", epochSchedule.firstNormalSlot);

Epoch 奖励

getEpochRewards

getEpochRewards(): EpochRewards

获取 epoch 奖励 sysvar。

const rewards = client.svm.getEpochRewards();
console.log("Active:", rewards.active);
console.log("Total rewards:", rewards.totalRewards);
console.log("Distributed rewards:", rewards.distributedRewards);
console.log("Total points:", rewards.totalPoints);
console.log("Num partitions:", rewards.numPartitions);
console.log(
"Distribution starting block height:",
rewards.distributionStartingBlockHeight
);

Slot 信息

getSlotHashes

getSlotHashes(): SlotHash[]

获取最近的 slot 哈希值。

const slotHashes = client.svm.getSlotHashes();
console.log("Slot hashes count:", slotHashes.length);
if (slotHashes.length > 0) {
console.log("First hash:", {
slot: slotHashes[0].slot,
hash: slotHashes[0].hash
});
}

getSlotHistory

getSlotHistory(): SlotHistory

获取 slot 历史记录。

const slotHistory = client.svm.getSlotHistory();
console.log("Next slot:", slotHistory.nextSlot);

getLastRestartSlot / setLastRestartSlot

getLastRestartSlot(): bigint
setLastRestartSlot(slot: bigint): LiteSVM

获取或设置最后重启的slot。

const lastRestart = client.svm.getLastRestartSlot();
console.log("Last restart slot:", lastRestart);
client.svm.setLastRestartSlot(500n);

质押历史

getStakeHistory

getStakeHistory(): StakeHistory

获取质押历史。

const stakeHistory = client.svm.getStakeHistory();
// Returns array of { epoch, entry: { effective, activating, deactivating } }

测试时间相关逻辑

使用warpToSlot测试时间相关的程序行为:

import { createClient, generateKeyPairSigner } from "@solana/kit";
import { litesvm } from "@solana/kit-plugin-litesvm";
import { signer } from "@solana/kit-plugin-signer";
async function testTimeLockedVault() {
const mySigner = await generateKeyPairSigner();
const client = createClient().use(signer(mySigner)).use(litesvm());
client.svm.withSysvars();
const unlockSlot = 10000n;
// Setup: Create time-locked account
// ... set up your account state ...
// Test 1: Try to withdraw before unlock (should fail)
const beforeClock = client.svm.getClock();
console.log("Current slot:", beforeClock.slot);
// ... attempt withdrawal, expect failure ...
// Warp time forward past unlock slot
client.svm.warpToSlot(unlockSlot + 1n);
// Test 2: Try to withdraw after unlock (should succeed)
const afterClock = client.svm.getClock();
console.log("New slot:", afterClock.slot);
// ... attempt withdrawal, expect success ...
}

Is this page helpful?

Table of Contents

Edit Page
©️ 2026 Solana 基金会版权所有