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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use alloc::vec::Vec;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarks;
#[cfg(test)]
mod tests;
mod weights;
use weights::*;
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
    use super::*;
    use frame_support::pallet_prelude::*;
    use frame_system::pallet_prelude::*;
    use sp_core::Hasher;
    #[pallet::config]
    pub trait Config: frame_system::Config {
        type Event: From<Event<Self>>
            + IsType<<Self as frame_system::Config>::Event>
            + Into<<Self as frame_system::Config>::Event>;
    }
    #[pallet::error]
    pub enum Error<T> {
        AnchorExists,
    }
    #[pallet::event]
    #[pallet::generate_deposit(pub(super) fn deposit_event)]
    pub enum Event<T: Config> {
        AnchorDeployed(T::Hash, T::AccountId, T::BlockNumber),
    }
    #[pallet::pallet]
    #[pallet::generate_store(pub(super) trait Store)]
    pub struct Pallet<T>(_);
    #[pallet::storage]
    pub type Anchors<T: Config> = StorageMap<_, Identity, T::Hash, T::BlockNumber>;
    #[pallet::call]
    impl<T: Config> Pallet<T> {
        #[pallet::weight(SubstrateWeight::<T>::deploy(data.len() as u32))]
        pub fn deploy(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
            let account = ensure_signed(origin)?;
            Pallet::<T>::deploy_(data, account)
        }
    }
    impl<T: Config> Pallet<T> {
        fn deploy_(data: Vec<u8>, account: T::AccountId) -> DispatchResult {
            let hash = <T as frame_system::Config>::Hashing::hash(&data);
            ensure!(Anchors::<T>::get(hash).is_none(), Error::<T>::AnchorExists);
            let last_block = <frame_system::Pallet<T>>::block_number();
            Anchors::<T>::insert(hash, last_block);
            Self::deposit_event(Event::<T>::AnchorDeployed(hash, account, last_block));
            Ok(())
        }
    }
}