Documentation SolanaLiteSVMTypeScriptRéférence API

Temps & Sysvars

Méthodes pour manipuler le temps et accéder aux variables système.

Activez d'abord les sysvars avec client.svm.withSysvars() avant d'accéder aux méthodes des sysvars.

Manipulation de l'horloge

warpToSlot

warpToSlot(slot: bigint): LiteSVM

Avancer l'horloge jusqu'à un slot spécifique.

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

Obtenir les valeurs actuelles du sysvar d'horloge.

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

Écraser le sysvar d'horloge. Récupérez l'horloge, modifiez ses champs, puis réécrivez-la. Utilisez ceci lorsque vous avez besoin d'un contrôle précis sur plusieurs champs de l'horloge — par exemple, pour définir l'epoch ou l'horodatage Unix, que warpToSlot ne met pas à jour.

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

warpToSlot met uniquement à jour slot. Utilisez setClock lorsque votre programme lit epoch ou unix_timestamp depuis le sysvar Clock.

Propriétés de l'horloge

PropertyTypeDescription
slotbigintNuméro du slot actuel
epochbigintNuméro de l'epoch actuel
unixTimestampbigintHorodatage Unix en secondes
epochStartTimestampbigintDébut de l'epoch actuel
leaderScheduleEpochbigintEpoch du calendrier des leaders

Rent

getRent

getRent(): Rent

Obtenir le sysvar 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

Calculez le minimum de lamports nécessaires pour l'exemption de rent.

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

Calendrier des epoch

getEpochSchedule

getEpochSchedule(): EpochSchedule

Récupérez la sysvar du calendrier des epoch.

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);

Récompenses d'epoch

getEpochRewards

getEpochRewards(): EpochRewards

Récupérez la sysvar des récompenses d'epoch.

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
);

Informations sur les slot

getSlotHashes

getSlotHashes(): SlotHash[]

Récupérez les hachages de slot récents.

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

Récupérez l'historique des slot.

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

getLastRestartSlot / setLastRestartSlot

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

Obtenir ou définir le dernier slot de redémarrage.

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

Historique des stakes

getStakeHistory

getStakeHistory(): StakeHistory

Obtenir l'historique des stakes.

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

Tester la logique dépendante du temps

Utilisez warpToSlot pour tester le comportement d'un programme dépendant du temps :

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?

© 2026 Fondation Solana. Tous droits réservés.