Saving and Restoring Snapshots

The Luzid snapshot API allows you to save snapshots, restore and delete them. It also supports grouping multiple snapshots to perform operations on them in batch.

Here we go through all methods to manage snapshots first and provide a full example leveraging all of them at the bottom of this doc.

Save Account Snapshot

In the below we create a snapshot with the following properties:

  • name: 'Luzid Snapshot Example'
  • description: 'Snapshotting the account we just cloned'

We include the following accounts:

  • [accAddr] (an array of account addresses)

Finally we add the snapshot to the'example:snapshot' group.

import { LuzidSdk } from '@luzid/sdk'
const luzid = new LuzidSdk()

const res = await luzid.snapshot.createSnapshot(
  'Luzid Snapshot Example',
  [accAddr],
  {
    group: 'example:snapshot',
    description: 'Snapshotting the account we just cloned'
  }
)
console.log('\nCreated snapshot', res)
const snapshotId = res.snapshotId

Listing Snapshots

We can then obtain a list of snapshots we saved as follows:

const snapshots = await luzid.snapshot.listSnapshots()
console.log('\nSnapshots:', snapshots)

Getting List of snapshotable Accounts

In order to get the addresses of all accounts we can snapshot we can use the following method:

const accounts = await luzid.snapshot.getSnaphotableAccounts({ includeProgramAccounts: true })
console.log('\nAccounts we can snapshot:', accounts)

the result includes information about each account like pubkey, modified slot and balance.

Restoring a Snapshot

In order to restore accounts from a snapshot we provide the snapshotId and the addresses of the accounts we want to restore.

const res = await luzid.snapshot.restoreAccountsFromSnapshot(snapshotId, {
  accounts: [accAddr]
})

Deleting Snapshots

We can delete one specific snapshot by providing its snapshotId:

const deletedSnapshotId = await luzid.snapshot.deleteSnapshot(snapshotId)

Alternatively we can delete all snapshots in a group:

const deletedSnasphotIds = await deleteSnapshotsMatching({ group: 'example:snapshot' })

Full Example

The below lists snapshotable accounts, saves and restores a snapshot and finally deletes it.

Full Example

See the Pen Luzid: Snapshots by Thorsten Lorenz (@thlorenz) on CodePen.