zink/storage/
value.rs

1//! Key-Value storage
2use crate::{asm, storage::Value};
3
4/// Storage trait. Currently not for public use
5pub trait Storage {
6    #[cfg(not(target_family = "wasm"))]
7    const STORAGE_KEY: [u8; 32];
8    const STORAGE_SLOT: i32;
9
10    type Value: Value;
11
12    /// Get value from storage.
13    fn get() -> Self::Value {
14        Value::push(Self::STORAGE_SLOT);
15        Self::Value::sload()
16    }
17
18    /// Set value to storage.
19    fn set(value: Self::Value) {
20        value.push();
21        Value::push(Self::STORAGE_SLOT);
22        unsafe {
23            asm::evm::sstore();
24        }
25    }
26}
27
28/// Transient storage trait. Currently not for public use
29pub trait TransientStorage {
30    #[cfg(not(target_family = "wasm"))]
31    const STORAGE_KEY: [u8; 32];
32    const STORAGE_SLOT: i32;
33
34    type Value: Value;
35
36    /// Get value from transient storage.
37    fn get() -> Self::Value {
38        Value::push(Self::STORAGE_SLOT);
39        Self::Value::tload()
40    }
41
42    /// Set value to transient storage.
43    fn set(value: Self::Value) {
44        value.push();
45        Value::push(Self::STORAGE_SLOT);
46        unsafe {
47            asm::evm::tstore();
48        }
49    }
50}