Skip to main content

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
}
}