Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Latest commit

 

History

History
23 lines (20 loc) · 726 Bytes

use-temporary.mdx

File metadata and controls

23 lines (20 loc) · 726 Bytes
title hide_table_of_contents
Use temporary storage in a contract
true

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