Persistent Storage
#![no_std]
use soroban_sdk::{contract, contractimpl, contracttype, Env};
#[contracttype]
pub enum DataKey {
MyKey
}
#[contract]
pub struct PersistentStorage;
#[contractimpl]
impl PersistentStorage {
/// Create contract entry
pub fn setup(env: Env) {
env.storage().persistent().set(&DataKey::MyKey, &0);
}
/// Extend the TTL (Time-To-Live) to 5000 ledgers
/// in the case that TTL is less than 1000 ledgers
pub fn extend_persistent(env: Env) {
env.storage()
.persistent()
.extend_ttl(&DataKey::MyKey, 1000, 5000);
}
}
Guide
#![no_std]
This attribute prevents linking to the standard library, making the code lighter and more efficient for Soroban contracts. It's big so we save on size.
use soroban_sdk::{contract, contractimpl, Env, log}
Imports stuffs from the Soroban SDK. Env
is basic Soroban type, we need it because we can't use the Rust standard library.
#[contracttype]
Used to define the type of a contract. It's essential for specifying the contract's capabilities, permissions, and the kind of interactions it can have with other contracts or with the blockchain itself.
#[contract]
Marks the struct as a Soroban smart contract. Soroban smart contracts are defined as Rust structs.
#[contractimpl]
Marks the implementation block as containing contract methods and transforms it to code that Soroban can evaluate directly
pub
Marks a function as external, meaning it can be invoked outside of the context of the contract code it
self.
Explanation
The setup
function intializes the persistent storage method using the DataKey enum
created under contracttype
keyword.
Enums (or enumerations) in Rust are a data type that allows you to define a set of named constants. They're useful for representing values that can only be one of a fixed set of possibilities.
The extend_persistent
function extends the set TTL (Time-To-Live) for the persistent storage to 5000 ledgers if less than 1000 ledgers. It uses the value derived from DataKey
established above.
Run in Playground
Loading playground...