snapshot()
Serializes an agent's memory bundle to JSON, encrypts it, uploads it to 0G Storage, and mints a provenance NFT on 0G Chain. Returns a SnapshotResult when the on-chain transaction confirms.
Signature
snapshot(bundle: MemoryBundle, parentTokenId?: bigint): Promise<SnapshotResult>
Parameters
| Parameter | Type | Description |
|---|---|---|
bundle | MemoryBundle | The agent's memory state. See MemoryBundle → |
parentTokenId | bigint (optional) | Token ID of a parent snapshot — for minting a fork. Pass 0n or omit for a root token. |
Returns
Promise<SnapshotResult>
interface SnapshotResult {
tokenId: bigint; // minted NFT token ID on MemoryRegistry
contentHash: `0x${string}`; // keccak256(plaintext JSON) — also the decryption key seed
storageUri: string; // "v2:0g://<rootHash>" — content address on 0G Storage
txHash: `0x${string}`; // on-chain transaction hash
timestamp: number; // Date.now() at time of call
}
Example
import type { MemoryBundle } from '@mnemos-sdk/sdk';
const bundle: MemoryBundle = {
data: {
trades: [{ pair: 'ETH/USDC', amount: 1.5, side: 'buy' }],
totalPnl: 42.0,
},
metadata: {
category: 'trading',
agentId: 'my-agent-v1',
version: '1.0.0',
createdAt: Date.now(),
tags: ['defi', 'yield'],
},
};
const result = await mnemos.snapshot(bundle);
console.log(result.tokenId); // 3n
console.log(result.storageUri); // "v2:0g://0xabc..."
console.log(result.txHash); // "0xdef..."
What Happens Internally
JSON.stringify(bundle)— serializes the bundlecontentHash = keccak256(plaintext)— derives both the on-chain identifier and the encryption key seed- NaCl secretbox encryption with a 24-byte random nonce (nonce || ciphertext stored together)
- Upload to 0G Storage via
Indexer.upload— returns a0g://<rootHash>URI MemoryRegistry.mintMemory(contentHash, "v2:" + uri)— mints the NFT- Waits for transaction receipt and parses the
MemoryMintedevent for the token ID
The call blocks until the transaction is confirmed on-chain (up to 120-second timeout, polling every 3 seconds).
Notes
tokenIdis extracted from theMemoryMintedevent log. Keep it — you need it for listing, forking, or loading.- The
contentHashis public on-chain. Anyone with thecontentHashcan derive the decryption key. See Privacy & Access → for the full explanation. - For recurring snapshots, use
autoSnapshot()instead of callingsnapshot()in a loop.
Next: autoSnapshot() →