Methoden voor het manipuleren van tijd en het benaderen van systeemvariabelen.
Klokmanipulatie
warpToSlot
warpToSlot(slot: bigint): void
Zet de klok vooruit naar een specifiek slot.
// Check current slot
const before = client.svm.getClock();
console.log("Current slot:", before.slot);
// Warp forward 10,000 slots
client.svm.warpToSlot(before.slot + 10_000n);
const after = client.svm.getClock();
console.log("New slot:", after.slot);
getClock
Haal de huidige waarden van de clock-sysvar op.
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): void
Overschrijf de clock-sysvar. Haal de clock op, wijzig de velden en schrijf deze
daarna weer terug. Gebruik dit wanneer je nauwkeurige controle nodig hebt over meerdere clockvelden — bijvoorbeeld bij het instellen van de epoch of de unix-timestamp, wat warpToSlot niet
bijwerkt.
const clock = client.svm.getClock();
clock.slot = 1000n;
clock.epoch = 10n;
clock.unixTimestamp = 1735689600n;
client.svm.setClock(clock);
warpToSlot werkt alleen slot bij. Gebruik setClock wanneer je programma
epoch of unix_timestamp uit de Clock-sysvar leest.
Clock-eigenschappen
Rent
getRent
Haal de rent-sysvar op.
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
Bereken het minimale aantal lamports dat nodig is voor rent-vrijstelling.
// 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-schema
getEpochSchedule
getEpochSchedule(): EpochSchedule
Haal de epoch-schema-sysvar op.
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-beloningen
getEpochRewards
getEpochRewards(): EpochRewards
Haal de epoch-beloningen-sysvar op.
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
);
getSlotHashes
getSlotHashes(): SlotHash[]
Haal recente slot-hashes op.
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
Haal de slot-geschiedenis op.
const slotHistory = client.svm.getSlotHistory();
console.log("Next slot:", slotHistory.nextSlot);
getLastRestartSlot / setLastRestartSlot
getLastRestartSlot(): bigint
setLastRestartSlot(slot: bigint): void
Haal het laatste herstart-slot op of stel dit in.
const lastRestart = client.svm.getLastRestartSlot();
console.log("Last restart slot:", lastRestart);
client.svm.setLastRestartSlot(500n);
Stake-geschiedenis
getStakeHistory
getStakeHistory(): StakeHistory
Haal de stake-geschiedenis op.
const stakeHistory = client.svm.getStakeHistory();
// Returns the StakeHistory sysvar (not a plain array)
Tijdafhankelijke logica testen
Gebruik warpToSlot om tijdafhankelijk programmagedrag te testen:
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());
// LiteSVM starts at a recent mainnet slot, so set the unlock relative to it
const unlockSlot = client.svm.getClock().slot + 10_000n;
// 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 ...
}