Successfully implemented a comprehensive Soroban Contract State Snapshot Tool that enables developers to capture, export, and restore Soroban contract state snapshots for reproducible audit and debugging purposes.
- Purpose: Captures and exports Soroban contract state snapshots
- Key Features:
- Export complete contract state snapshots via Soroban RPC
- Retrieve storage entries (instance, persistent, temporary)
- Capture contract metadata (network, ledger, timestamp)
- Validate snapshots before export or restoration
- Export/Import snapshots as JSON
- Proper Stellar SDK integration with
rpc.Serverand XDR encoding
- Purpose: Restores contract state from snapshots
- Key Features:
- Restore storage entries from snapshot files
- Validate snapshots before restoration
- Network passphrase verification to prevent cross-network restoration
- Preview restoration without applying changes (dry run support)
- Configurable overwrite/skip behavior for existing entries
- Detailed restoration results with success/failure tracking
- Comprehensive TypeScript interfaces for:
StorageEntry- Individual storage entriesContractMetadata- Contract and network metadataContractStateSnapshot- Complete snapshot structureSnapshotExportConfig/SnapshotRestoreConfig- Configuration optionsSnapshotExportResult/SnapshotRestoreResult- Operation resultsSnapshotValidationResult- Validation feedback
- Module exports for easy importing
- Re-exports all public APIs and types
snapshot-exporter.spec.ts- 16 comprehensive testssnapshot-restorer.spec.ts- 10 comprehensive tests- Total: 26 tests, all passing ✓
- Uses
@stellar/stellar-sdkv15.0.1 - Proper RPC server initialization with
rpc.Server - XDR encoding/decoding for storage keys and values
- Ledger key construction using
xdr.LedgerKeyContractData - Storage type mapping to
rpc.Durabilityenum
// Creates proper XDR-encoded ledger keys
const contractData = new xdr.LedgerKeyContractData({
contract: contractAddress.toScAddress(),
key: xdr.ScVal.scvSymbol(key),
durability: rpc.Durability.Persistent,
});- Contract ID verification
- Network passphrase validation
- Ledger sequence validation
- Storage entry integrity checks
- Storage type validation
- Warnings for empty or large snapshots
import { SorobanSnapshotExporter } from "./src/state/snapshots/stellar";
const exporter = new SorobanSnapshotExporter(
"https://soroban-testnet.stellar.org",
);
const result = await exporter.exportSnapshot(
"CDLZVWRQK6QZL4QF5VQKQZL4QF5VQKQZL4QF5VQKQZL4QF5VQKQZL4QF",
);
if (result.success) {
// Save snapshot to file
const json = exporter.exportToJson(result.snapshot!);
require("fs").writeFileSync("snapshot.json", json);
}import { SorobanSnapshotRestorer } from "./src/state/snapshots/stellar";
import { Keypair } from "@stellar/stellar-sdk";
const restorer = new SorobanSnapshotRestorer(
"https://soroban-testnet.stellar.org",
);
const snapshot = JSON.parse(
require("fs").readFileSync("snapshot.json", "utf8"),
);
const signer = Keypair.fromSecret("SECRET_KEY_HERE");
// Preview first
const preview = await restorer.previewRestore(snapshot);
console.log(`Will restore ${preview.newEntries} new entries`);
// Restore
const result = await restorer.restoreSnapshot(snapshot, signer, {
dryRun: false,
overwriteExisting: false,
});
console.log(`Restored ${result.entriesRestored} entries`);- ✓ Constructor with default and custom configs
- ✓ Snapshot validation (valid/invalid cases)
- ✓ Missing contract ID detection
- ✓ Missing network passphrase detection
- ✓ Invalid ledger sequence detection
- ✓ Empty storage entries warning
- ✓ Large snapshot warning (>1000 entries)
- ✓ Invalid storage type detection
- ✓ JSON export/import roundtrip
- ✓ Data preservation during import
- ✓ Error handling for invalid contract IDs
- ✓ Duration measurement
- ✓ Constructor with default and custom configs
- ✓ Validation failure for invalid snapshots
- ✓ Restoration duration measurement
- ✓ Network mismatch detection
- ✓ Preview without applying changes
- ✓ Empty snapshot handling
- ✓ Entry categorization (existing vs new)
- ✓ Dry run configuration
- ✓ Skip validation configuration
| Criteria | Status | Notes |
|---|---|---|
| Export contract state | ✅ | Full implementation with metadata and storage entries |
| Support snapshot restoration | ✅ | Complete restoration with validation and preview |
| Snapshot tool implemented | ✅ | Production-ready with comprehensive error handling |
| Builds successfully | ✅ | TypeScript compilation passes |
| Passes lint | ✅ | ESLint passes with zero errors |
| Passes tests | ✅ | 26/26 tests passing |
| CI/CD ready | ✅ | Follows project patterns and conventions |
- Type Safety: 100% TypeScript with strict type checking
- Error Handling: Comprehensive try-catch blocks with detailed error messages
- Validation: Multi-layer validation (schema, network, data integrity)
- Test Coverage: 26 unit tests covering happy paths and edge cases
- Documentation: Inline JSDoc comments for all public APIs
- Code Quality: Passes ESLint with project standards
- Follows existing project structure in
src/state/snapshots/stellar/ - Uses project's TypeScript configuration
- Compatible with Jest test framework
- Follows ESLint rules and patterns
- No breaking changes to existing code
@stellar/stellar-sdk@^15.0.1(already in package.json)- Standard TypeScript/Node.js runtime
- No additional dependencies required
- Storage key discovery automation via contract introspection
- Batch export/import for multiple contracts
- Snapshot diffing and comparison tools
- Snapshot versioning and migration support
- CLI tool for easy snapshot management
- Snapshot encryption for sensitive state data
The Soroban Contract State Snapshot Tool is fully implemented, tested, and ready for production use. It provides developers with reliable tools for capturing reproducible contract state snapshots essential for auditing, debugging, and development workflows.