Skip to content

Storage Syscalls

Back to Syscalls

Storage API

Storage syscalls provide persistent key-value state for contracts. Each contract has its own isolated storage context.

Syscall NameGas CostDescriptionDevpack Wrapper
System.Storage.GetContext1Get current storage contextSyscalls.getStorageContext()
System.Storage.GetReadOnlyContext1Get read-only contextSyscalls.getReadOnlyStorageContext()
System.Storage.AsReadOnly1Convert context to read-onlySyscalls.storageAsReadOnly(ctx)
System.Storage.Get100Read value by keySyscalls.storageGet(ctx, key)
System.Storage.Put1,000Write key-value pairSyscalls.storagePut(ctx, key, val)
System.Storage.Delete100Delete keySyscalls.storageDelete(ctx, key)
System.Storage.Find100Find by prefix (returns iterator)Syscalls.storageFind(ctx, prefix)
System.Storage.Local.Get100Read from local contextSyscalls.storageGetLocal(key)
System.Storage.Local.Put1,000Write to local contextSyscalls.storagePutLocal(key, val)
System.Storage.Local.Delete100Delete from local contextSyscalls.storageDeleteLocal(key)
System.Storage.Local.Find100Find in local contextSyscalls.storageFindLocal(prefix)

Local Storage

Local storage (System.Storage.Local.*) is contract-private and cannot be read by other contracts even through System.Storage.GetReadOnlyContext. Use it for internal bookkeeping that should never be externally visible.

Example Usage

solidity
import "devpack/contracts/Syscalls.sol";

contract TokenVault {
    // The compiler lowers state variables to Storage.Get/Put automatically.
    // For manual control, use the Syscalls library directly:

    function manualStorageExample(bytes memory key, bytes memory value) internal {
        // Get the current contract's storage context
        Syscalls.StorageContext memory ctx = Syscalls.getStorageContext();

        // Write a key-value pair (costs 1,000 GAS units)
        Syscalls.storagePut(ctx, key, value);

        // Read it back (costs 100 GAS units)
        bytes memory stored = Syscalls.storageGet(ctx, key);

        // Delete when no longer needed (costs 100 GAS units)
        Syscalls.storageDelete(ctx, key);
    }
}

Cost Guidance

Storage Cost Awareness

System.Storage.Put costs 1,000 GAS units — 10x more expensive than Get or Delete (100 each). Minimize writes by batching updates and avoiding redundant puts. The Storage.sol library provides batchPut() for multi-key writes, but each individual put still incurs the full syscall cost.

MIT Licensed