|
| 1 | +#![cfg_attr(not(feature = "std"), no_std)] |
| 2 | + |
| 3 | +use frame_support::{dispatch::DispatchResult, ensure}; |
| 4 | +use itp_binary_merkle_tree::{merkle_proof, merkle_root, MerkleProofWithCodec}; |
| 5 | +use parity_scale_codec::{Decode, Encode}; |
| 6 | +use scale_info::TypeInfo; |
| 7 | +use sp_core::{MaxEncodedLen, H256}; |
| 8 | +use sp_runtime::traits::Keccak256; |
| 9 | +use sp_std::{fmt::Debug, vec::Vec}; |
| 10 | + |
| 11 | +pub use pallet::*; |
| 12 | + |
| 13 | +pub mod merkle_tree { |
| 14 | + pub use itp_binary_merkle_tree::{ |
| 15 | + merkle_proof, merkle_root, verify_proof, MerkleProofWithCodec, |
| 16 | + }; |
| 17 | + pub use sp_runtime::traits::Keccak256; |
| 18 | +} |
| 19 | + |
| 20 | +#[cfg(test)] |
| 21 | +mod mock; |
| 22 | +#[cfg(test)] |
| 23 | +mod tests; |
| 24 | +pub mod weights; |
| 25 | + |
| 26 | +pub type RaffleIndex = u32; |
| 27 | +pub type WinnerCount = u32; |
| 28 | + |
| 29 | +#[derive(Debug, Clone, Encode, Decode, Eq, PartialEq, TypeInfo, MaxEncodedLen)] |
| 30 | +pub struct RaffleMetadata<AccountId: Debug> { |
| 31 | + index: RaffleIndex, |
| 32 | + raffle: Raffle<AccountId>, |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug, Clone, Encode, Decode, Eq, PartialEq, TypeInfo, MaxEncodedLen)] |
| 36 | +pub struct Raffle<AccountId: Debug> { |
| 37 | + owner: AccountId, |
| 38 | + winner_count: WinnerCount, |
| 39 | + registration_open: bool, |
| 40 | +} |
| 41 | + |
| 42 | +#[frame_support::pallet] |
| 43 | +pub mod pallet { |
| 44 | + use crate::{weights::WeightInfo, Raffle, RaffleIndex, Shuffle, WinnerCount}; |
| 45 | + use frame_support::pallet_prelude::*; |
| 46 | + use frame_system::pallet_prelude::*; |
| 47 | + use sp_core::H256; |
| 48 | + use sp_std::vec::Vec; |
| 49 | + |
| 50 | + const STORAGE_VERSION: StorageVersion = StorageVersion::new(0); |
| 51 | + #[pallet::pallet] |
| 52 | + #[pallet::storage_version(STORAGE_VERSION)] |
| 53 | + #[pallet::without_storage_info] |
| 54 | + pub struct Pallet<T>(PhantomData<T>); |
| 55 | + |
| 56 | + /// Configuration trait. |
| 57 | + #[pallet::config] |
| 58 | + pub trait Config: frame_system::Config { |
| 59 | + type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; |
| 60 | + type WeightInfo: WeightInfo; |
| 61 | + |
| 62 | + /// Implements random shuffling of values. |
| 63 | + /// |
| 64 | + /// If you use this on-chain you need to make sure to have a deterministic seed based |
| 65 | + /// on-chain values. If you use this in SGX, we want to make sure that the randomness |
| 66 | + /// is as secure as possible, hence we use the SGX's randomness, which uses a hardware |
| 67 | + /// secured randomness source: https://en.wikipedia.org/wiki/RDRAND. |
| 68 | + type Shuffle: Shuffle; |
| 69 | + } |
| 70 | + |
| 71 | + #[pallet::event] |
| 72 | + #[pallet::generate_deposit(pub(super) fn deposit_event)] |
| 73 | + pub enum Event<T: Config> { |
| 74 | + /// A new raffle has been registered |
| 75 | + RaffleAdded { index: RaffleIndex, raffle: Raffle<T::AccountId> }, |
| 76 | + |
| 77 | + /// Someone has registered for a raffle |
| 78 | + RaffleRegistration { who: T::AccountId, index: RaffleIndex }, |
| 79 | + |
| 80 | + /// Winners were drawn of a raffle |
| 81 | + WinnersDrawn { index: RaffleIndex, winners: Vec<T::AccountId>, registrations_root: H256 }, |
| 82 | + } |
| 83 | + |
| 84 | + #[pallet::error] |
| 85 | + pub enum Error<T> { |
| 86 | + /// The raffle does not exist |
| 87 | + NonexistentRaffle, |
| 88 | + /// The registrations for that raffles are closed |
| 89 | + RegistrationsClosed, |
| 90 | + /// Only the raffle owner can draw the winners |
| 91 | + OnlyRaffleOwnerCanDrawWinners, |
| 92 | + } |
| 93 | + |
| 94 | + /// Ongoing raffles. |
| 95 | + #[pallet::storage] |
| 96 | + #[pallet::getter(fn ongoing_raffles)] |
| 97 | + pub type OnGoingRaffles<T: Config> = |
| 98 | + StorageMap<_, Blake2_128Concat, RaffleIndex, Raffle<T::AccountId>, OptionQuery>; |
| 99 | + |
| 100 | + /// Ongoing raffles. |
| 101 | + #[pallet::storage] |
| 102 | + #[pallet::getter(fn winners)] |
| 103 | + pub type Winners<T: Config> = |
| 104 | + StorageMap<_, Blake2_128Concat, RaffleIndex, Vec<T::AccountId>, OptionQuery>; |
| 105 | + |
| 106 | + #[pallet::storage] |
| 107 | + #[pallet::getter(fn registrations)] |
| 108 | + pub type Registrations<T: Config> = StorageDoubleMap< |
| 109 | + _, |
| 110 | + Blake2_128Concat, |
| 111 | + RaffleIndex, |
| 112 | + Blake2_128Concat, |
| 113 | + T::AccountId, |
| 114 | + (), |
| 115 | + OptionQuery, |
| 116 | + >; |
| 117 | + |
| 118 | + #[pallet::storage] |
| 119 | + #[pallet::getter(fn raffle_count)] |
| 120 | + pub type RaffleCount<T> = StorageValue<_, RaffleIndex, ValueQuery>; |
| 121 | + |
| 122 | + #[pallet::hooks] |
| 123 | + impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {} |
| 124 | + |
| 125 | + #[pallet::call] |
| 126 | + impl<T: Config> Pallet<T> { |
| 127 | + #[pallet::call_index(0)] |
| 128 | + #[pallet::weight(T::WeightInfo::add_raffle())] |
| 129 | + pub fn add_raffle(origin: OriginFor<T>, winner_count: WinnerCount) -> DispatchResult { |
| 130 | + let sender = ensure_signed(origin)?; |
| 131 | + let index = Self::raffle_count(); |
| 132 | + |
| 133 | + let raffle = Raffle { owner: sender, winner_count, registration_open: true }; |
| 134 | + |
| 135 | + OnGoingRaffles::<T>::insert(index, &raffle); |
| 136 | + RaffleCount::<T>::put(index + 1); |
| 137 | + |
| 138 | + Self::deposit_event(Event::RaffleAdded { index, raffle }); |
| 139 | + Ok(()) |
| 140 | + } |
| 141 | + |
| 142 | + #[pallet::call_index(1)] |
| 143 | + #[pallet::weight(T::WeightInfo::register_for_raffle())] |
| 144 | + pub fn register_for_raffle(origin: OriginFor<T>, index: RaffleIndex) -> DispatchResult { |
| 145 | + let sender = ensure_signed(origin)?; |
| 146 | + |
| 147 | + ensure!(OnGoingRaffles::<T>::contains_key(index), Error::<T>::NonexistentRaffle); |
| 148 | + ensure!( |
| 149 | + OnGoingRaffles::<T>::get(index) |
| 150 | + .expect("Asserted above that the key exists; qed") |
| 151 | + .registration_open, |
| 152 | + Error::<T>::RegistrationsClosed |
| 153 | + ); |
| 154 | + |
| 155 | + Registrations::<T>::insert(index, &sender, ()); |
| 156 | + |
| 157 | + Self::deposit_event(Event::RaffleRegistration { who: sender, index }); |
| 158 | + Ok(()) |
| 159 | + } |
| 160 | + |
| 161 | + #[pallet::call_index(2)] |
| 162 | + #[pallet::weight(T::WeightInfo::draw_winners())] |
| 163 | + pub fn draw_winners(origin: OriginFor<T>, index: RaffleIndex) -> DispatchResult { |
| 164 | + let sender = ensure_signed(origin)?; |
| 165 | + Self::try_draw_winners(sender, index) |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +impl<T: Config> Pallet<T> { |
| 171 | + pub fn all_ongoing_raffles() -> Vec<RaffleMetadata<T::AccountId>> { |
| 172 | + OnGoingRaffles::<T>::iter() |
| 173 | + .map(|kv| RaffleMetadata { index: kv.0, raffle: kv.1 }) |
| 174 | + .collect() |
| 175 | + } |
| 176 | + |
| 177 | + pub fn raffle_registrations(index: RaffleIndex) -> Vec<T::AccountId> { |
| 178 | + Registrations::<T>::iter_prefix(index).map(|kv| kv.0).collect() |
| 179 | + } |
| 180 | + |
| 181 | + fn try_draw_winners(owner: T::AccountId, index: RaffleIndex) -> DispatchResult { |
| 182 | + let raffle = OnGoingRaffles::<T>::get(index).ok_or(Error::<T>::NonexistentRaffle)?; |
| 183 | + ensure!(raffle.registration_open, Error::<T>::RegistrationsClosed); |
| 184 | + ensure!(raffle.owner == owner, Error::<T>::OnlyRaffleOwnerCanDrawWinners); |
| 185 | + |
| 186 | + let mut registrations = Self::raffle_registrations(index); |
| 187 | + let registrations_root = Self::merkle_root(®istrations); |
| 188 | + <T as Config>::Shuffle::shuffle(&mut registrations); |
| 189 | + |
| 190 | + let count = core::cmp::min(registrations.len(), raffle.winner_count as usize); |
| 191 | + let winners = registrations[..count].to_vec(); |
| 192 | + |
| 193 | + OnGoingRaffles::<T>::mutate(index, |r| r.as_mut().map(|r| r.registration_open = false)); |
| 194 | + Winners::<T>::insert(index, &winners); |
| 195 | + |
| 196 | + Self::deposit_event(Event::WinnersDrawn { index, winners, registrations_root }); |
| 197 | + Ok(()) |
| 198 | + } |
| 199 | + |
| 200 | + pub fn merkle_proof_for_registration( |
| 201 | + index: RaffleIndex, |
| 202 | + account: &T::AccountId, |
| 203 | + ) -> Option<MerkleProofWithCodec<H256, Vec<u8>>> { |
| 204 | + let registrations = Self::raffle_registrations(index); |
| 205 | + Self::merkle_proof(account, ®istrations) |
| 206 | + } |
| 207 | + |
| 208 | + pub(crate) fn merkle_root(accounts: &[T::AccountId]) -> H256 { |
| 209 | + merkle_root::<Keccak256, _>(accounts.iter().map(Encode::encode)) |
| 210 | + } |
| 211 | + |
| 212 | + pub(crate) fn merkle_proof( |
| 213 | + account: &T::AccountId, |
| 214 | + registrations: &[T::AccountId], |
| 215 | + ) -> Option<MerkleProofWithCodec<H256, Vec<u8>>> { |
| 216 | + let leaf_index = Self::merkle_leaf_index_for_registration(account, registrations)?; |
| 217 | + let p = |
| 218 | + merkle_proof::<Keccak256, _, _>(registrations.iter().map(Encode::encode), leaf_index); |
| 219 | + Some(p.into()) |
| 220 | + } |
| 221 | + |
| 222 | + pub(crate) fn merkle_leaf_index_for_registration( |
| 223 | + account: &T::AccountId, |
| 224 | + registrations: &[T::AccountId], |
| 225 | + ) -> Option<usize> { |
| 226 | + registrations.iter().position(|a| a == account) |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +pub trait Shuffle { |
| 231 | + fn shuffle<T>(values: &mut [T]); |
| 232 | +} |
0 commit comments