zink/storage/
value.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Key-Value storage
use crate::{
    ffi,
    storage::{StorageValue, TransientStorageValue},
    Asm,
};

/// Storage trait. Currently not for public use
pub trait Storage {
    #[cfg(not(target_family = "wasm"))]
    const STORAGE_KEY: [u8; 32];
    const STORAGE_SLOT: i32;

    type Value: StorageValue + Asm;

    /// Get value from storage.
    fn get() -> Self::Value {
        Asm::push(Self::STORAGE_SLOT);
        Self::Value::sload()
    }

    /// Set value to storage.
    fn set(value: Self::Value) {
        value.push();
        Asm::push(Self::STORAGE_SLOT);
        unsafe {
            ffi::evm::sstore();
        }
    }
}

/// Transient storage trait. Currently not for public use
pub trait TransientStorage {
    #[cfg(not(target_family = "wasm"))]
    const STORAGE_KEY: [u8; 32];
    const STORAGE_SLOT: i32;

    type Value: TransientStorageValue + Asm;

    /// Get value from transient storage.
    fn get() -> Self::Value {
        Asm::push(Self::STORAGE_SLOT);
        Self::Value::tload()
    }

    /// Set value to transient storage.
    fn set(value: Self::Value) {
        value.push();
        Asm::push(Self::STORAGE_SLOT);
        unsafe {
            ffi::evm::tstore();
        }
    }
}