Snapshots
Snapshots in SuiPulse provide an efficient way to capture and manage stream states at specific points in time.
Overview
A snapshot is a point-in-time capture of a stream's state, including:
- Current data
- Metadata
- Version information
- Creation timestamp
Creating Snapshots
import { SuiPulse, Network } from "@suipulse/sdk";
import { Ed25519Keypair } from "@mysten/sui.js/keypairs/ed25519";
// Initialize SuiPulse
const keypair = Ed25519Keypair.fromSecretKey(/* your private key */);
const suiPulse = new SuiPulse(keypair, Network.MAINNET);
// Create a snapshot
const result = await suiPulse.createSnapshot(streamId, {
metadata: "Snapshot metadata",
});
// Get snapshot ID from the event
const snapshotId = result.events.find(
(event) => event.type === `${suiPulse.packageId}::storage::SnapshotCreated`
)?.parsedJson.snapshot_id;
Managing Snapshots
Update Snapshot
await suiPulse.updateSnapshot(snapshotId, {
data: new Uint8Array(Buffer.from("Updated snapshot data")),
});
Transfer Snapshot
await suiPulse.transferSnapshot(snapshotId, recipientAddress);
Get Snapshot Data
const snapshotData = await suiPulse.getSnapshotData(snapshotId);
console.log("Snapshot data:", snapshotData);
Get Snapshot Stream ID
const streamId = await suiPulse.getSnapshotStreamId(snapshotId);
Snapshot Events
// Subscribe to snapshot creation events
const unsubscribe = await suiPulse.subscribeToEvents(
"SnapshotCreated",
(event) => {
console.log("New snapshot created:", event.parsedJson.snapshot_id);
}
);
Best Practices
-
Snapshot Frequency
- Create snapshots at meaningful state changes
- Consider storage costs
- Plan for data retention
-
Data Management
- Keep snapshot metadata concise
- Use appropriate data formats
- Handle large data efficiently
-
Access Control
- Set appropriate permissions
- Monitor snapshot access
- Implement proper transfer controls
Use Cases
- State rollback
- Data backup
- Historical analysis
- Audit trails
- Version control
Next Steps
- Learn about Data Streams
- Explore Access Control
- Check out Analytics