Các phương thức để thao tác thời gian và truy cập biến hệ thống.
Kích hoạt sysvar trước bằng client.svm.withSysvars() trước khi truy cập các
phương thức sysvar.
Thao Tác Đồng Hồ
warpToSlot
warpToSlot(slot: bigint): LiteSVM
Tiến đồng hồ đến một slot cụ thể.
client.svm.withSysvars();// Check current slotconst before = client.svm.getClock();console.log("Current slot:", before.slot);// Warp forwardclient.svm.warpToSlot(10000n);const after = client.svm.getClock();console.log("New slot:", after.slot);
getClock
getClock(): Clock
Lấy các giá trị sysvar đồng hồ hiện tại.
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
Ghi đè sysvar đồng hồ. Lấy đồng hồ, thay đổi các trường của nó, sau đó ghi lại.
Sử dụng khi bạn cần kiểm soát chính xác nhiều trường đồng hồ — ví dụ, đặt epoch
hoặc dấu thời gian unix, điều mà warpToSlot không cập nhật.
const clock = client.svm.getClock();clock.slot = 1000n;clock.epoch = 10n;clock.unixTimestamp = 1735689600n;client.svm.setClock(clock);
warpToSlot chỉ cập nhật slot. Sử dụng setClock khi chương trình của bạn
đọc epoch hoặc unix_timestamp từ sysvar Clock.
Thuộc Tính Clock
| Property | Type | Description |
|---|---|---|
slot | bigint | Số slot hiện tại |
epoch | bigint | Số epoch hiện tại |
unixTimestamp | bigint | Dấu thời gian Unix tính bằng giây |
epochStartTimestamp | bigint | Bắt đầu epoch hiện tại |
leaderScheduleEpoch | bigint | Epoch lịch trình leader |
Rent
getRent
getRent(): Rent
Lấy 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
Tính toán số lamport tối thiểu cần thiết để được miễn rent.
// Calculate for different data sizesfor (const size of [0n, 100n, 1000n]) {const min = client.svm.minimumBalanceForRentExemption(size);console.log(`${size} bytes: ${min} lamports`);}
Lịch epoch
getEpochSchedule
getEpochSchedule(): EpochSchedule
Lấy sysvar lịch 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);
Phần thưởng epoch
getEpochRewards
getEpochRewards(): EpochRewards
Lấy sysvar phần thưởng 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);
Thông tin slot
getSlotHashes
getSlotHashes(): SlotHash[]
Lấy các hash slot gần đây.
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
Lấy lịch sử slot.
const slotHistory = client.svm.getSlotHistory();console.log("Next slot:", slotHistory.nextSlot);
getLastRestartSlot / setLastRestartSlot
getLastRestartSlot(): bigintsetLastRestartSlot(slot: bigint): LiteSVM
Lấy hoặc đặt slot khởi động lại cuối cùng.
const lastRestart = client.svm.getLastRestartSlot();console.log("Last restart slot:", lastRestart);client.svm.setLastRestartSlot(500n);
Lịch Sử Stake
getStakeHistory
getStakeHistory(): StakeHistory
Lấy lịch sử stake.
const stakeHistory = client.svm.getStakeHistory();// Returns array of { epoch, entry: { effective, activating, deactivating } }
Kiểm Tra Logic Phụ Thuộc Thời Gian
Sử dụng warpToSlot để kiểm tra hành vi chương trình phụ thuộc thời gian:
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 slotclient.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?