Use temporary storage in a contract
Temporary storage is useful for a contract to store data that can quickly become irrelevant or out-dated. For example, here's how a contract might be used to store a recent price of BTC against the US Dollar.
// This function updates the BTC price
pub fn update_btc_price(env: Env, price: i128) {
env.storage().temporary().set(&!symbol_short("BTC"), &price);
}
// This function reads and returns the current BTC price (zero if the storage
// entry is archived)
pub fn get_btc_price(env: Env) -> i128 {
if let Some(price) = env.storage().temporary().get(&!symbol_short("BTC")) {
price
} else {
0
}
}
Guides in this category:
📄️ Use instance storage in a contract
Under the hood, instance storage is exactly like persistent storage. The only difference is that anything stored in instance storage has an archival TTL that is tied to the contract instance itself. So, if a contract is live and available, the instance storage is guaranteed to be so, too.
📄️ Use persistent storage in a contract
Persistent storage can be very useful for ledger entrys that are not common across every user of the contract instance, but that are not suitable to be temporary (user balances, for example). In this guide, we'll assume we want to store a random number for a user, and store it in the contract's persistent storage as though it were their favorite number.
📄️ Use temporary storage in a contract
Temporary storage is useful for a contract to store data that can quickly become irrelevant or out-dated. For example, here's how a contract might be used to store a recent price of BTC against the US Dollar.