Attribute Macro stellar_axelar_std_derive::contractstorage
source · #[contractstorage]
Expand description
Implements a storage interface for a Stellar contract storage enum.
The enum variants define contract data keys, with optional named fields as contract data map keys.
Each variant requires a #[value(Type)]
xor #[status]
attribute to specify the stored value type.
Storage type can be specified with #[instance]
, #[persistent]
, or #[temporary]
attributes (defaults to instance).
Certain types have default behaviors for TTL extensions:
#[persistent]
: This is extended by default every time a data key is accessed, for that data key. The persistent data type does not share the same TTL as the contract instance.#[instance]
: This is extended by default for all contract endpoints, so it does not need to be included in generated data key access functions. This also serves to extend the lifetime of the contract’s bytecode, since the instance data type does share the same TTL as the contract instance.#[temporary]
: This is not extended by default, since this data type can be easily recreated or only valid for a certain period of time. In the special case that temporary data needs to be extended, a user may call the generated #ttl_extender function for that temporary data key.
More on Stellar data types: https://developers.stellar.org/docs/learn/encyclopedia/storage/state-archival#contract-data-type-descriptions
§Example
ⓘ
use stellar_axelar_std::{contract, contractimpl, contractype, Address, Env, String};
use stellar_axelar_std::contractstorage;
#[contractstorage]
#[derive(Clone, Debug)]
enum DataKey {
#[instance]
#[value(Address)]
Owner,
#[persistent]
#[value(String)]
TokenName { token_id: u32 },
#[temporary]
#[value(u64)]
LastUpdate { account: Address },
#[instance]
#[status]
Paused,
}
#[contract]
pub struct Contract;
#[contractimpl]
impl Contract {
pub fn __constructor(
env: &Env,
token_id: u32,
name: String,
) {
storage::set_token_name(env, token_id, &name);
}
pub fn foo(env: &Env, token_id: u32) -> Option<String> {
storage::token_name(env, token_id);
}
pub fn bar(env: &Env, token_id: u32) -> Option<String> {
storage::remove_token_name(env, token_id)
}
}