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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//! # Faucets Module
//!
//! The Faucets module allows a root key (sudo) to add accounts (faucets) that are eligible
//! to drip free tokens to other accounts (recipients).
//! 
//! Currently, only sudo account can add, update and remove faucets.
//! But this can be changed in the future to allow anyone else
//! to set up new faucets for their needs.
//!
//! This would allow each space to create its own faucet(s) and distribute its tokens to its 
//! members based on a set of conditions the space decides suits the needs of its community.

#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode};
use frame_support::{
    decl_error, decl_event, decl_module, decl_storage,
    dispatch::{DispatchError, DispatchResult},
    ensure,
    traits::{Currency, ExistenceRequirement, Get},
    weights::Pays,
};
use frame_system::{self as system, ensure_root, ensure_signed};
use sp_runtime::RuntimeDebug;
use sp_runtime::traits::{Saturating, Zero};
use sp_std::{
    collections::btree_set::BTreeSet,
    iter::FromIterator,
    prelude::*,
};

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
pub struct Faucet<T: Trait> {

    // Settings
    pub enabled: bool,
    pub period: T::BlockNumber,
    pub period_limit: BalanceOf<T>,
    pub drip_limit: BalanceOf<T>,

    // State
    pub next_period_at: T::BlockNumber,
    pub dripped_in_current_period: BalanceOf<T>,
}

#[derive(Encode, Decode, Clone, Eq, PartialEq, RuntimeDebug)]
pub struct FaucetUpdate<BlockNumber, Balance> {
    pub enabled: Option<bool>,
    pub period: Option<BlockNumber>,
    pub period_limit: Option<Balance>,
    pub drip_limit: Option<Balance>,
}

type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::Balance;

/// The pallet's configuration trait.
pub trait Trait: system::Trait {

    /// The overarching event type.
    type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;

    type Currency: Currency<Self::AccountId>;
}

decl_storage! {
    trait Store for Module<T: Trait> as FaucetsModule {

        /// Get a faucet data by its account id.
        pub FaucetByAccount get(fn faucet_by_account):
            map hasher(twox_64_concat) T::AccountId // Faucet account
            => Option<Faucet<T>>;
    }
}

decl_event!(
    pub enum Event<T> where
        AccountId = <T as system::Trait>::AccountId,
        Balance = BalanceOf<T>
    {
        FaucetAdded(AccountId),
        FaucetUpdated(AccountId),
        FaucetsRemoved(Vec<AccountId>),
        Dripped(
            AccountId, // Faucet account
            AccountId, // Recipient account
            Balance    // Amount dripped
        ),
    }
);

decl_error! {
    pub enum Error for Module<T: Trait> {
        FaucetNotFound,
        FaucetAlreadyAdded,
        NoFreeBalanceOnFaucet,
        NotEnoughFreeBalanceOnFaucet,
        NoFaucetsProvided,
        NoUpdatesProvided,
        NothingToUpdate,
        FaucetDisabled,
        NotFaucetOwner,
        RecipientEqualsFaucet,

        ZeroPeriodProvided,
        ZeroPeriodLimitProvided,
        ZeroDripLimitProvided,
        ZeroDripAmountProvided,
        
        PeriodLimitReached,
        DripLimitReached,
    }
}

decl_module! {
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        // Initializing errors
        type Error = Error<T>;

        // Initializing events
        fn deposit_event() = default;

        #[weight = 50_000 + T::DbWeight::get().reads_writes(2, 1)]
        pub fn add_faucet(
            origin,
            faucet: T::AccountId,
            period: T::BlockNumber,
            period_limit: BalanceOf<T>,
            drip_limit: BalanceOf<T>,
        ) -> DispatchResult {

            ensure_root(origin.clone())?;

            Self::ensure_period_not_zero(period)?;
            Self::ensure_period_limit_not_zero(period_limit)?;
            Self::ensure_drip_limit_not_zero(drip_limit)?;

            ensure!(
                Self::faucet_by_account(&faucet).is_none(),
                Error::<T>::FaucetAlreadyAdded
            );

            ensure!(
                T::Currency::free_balance(&faucet) >=
                T::Currency::minimum_balance(),
                Error::<T>::NoFreeBalanceOnFaucet
            );

            let new_faucet = Faucet::<T>::new(
                period,
                period_limit,
                drip_limit
            );

            FaucetByAccount::<T>::insert(faucet.clone(), new_faucet);
            Self::deposit_event(RawEvent::FaucetAdded(faucet));
            Ok(())
        }

        #[weight = 50_000 + T::DbWeight::get().reads_writes(1, 1)]
        pub fn update_faucet(
            origin,
            faucet: T::AccountId,
            update: FaucetUpdate<T::BlockNumber, BalanceOf<T>>
        ) -> DispatchResult {

            ensure_root(origin)?;

            let has_updates =
                update.enabled.is_some() ||
                update.period.is_some() ||
                update.period_limit.is_some() ||
                update.drip_limit.is_some();

            ensure!(has_updates, Error::<T>::NoUpdatesProvided);

            let mut settings = Self::require_faucet(&faucet)?;

            // `true` if there is at least one updated field.
            let mut should_update = false;

            if let Some(enabled) = update.enabled {
                if enabled != settings.enabled {
                    settings.enabled = enabled;
                    should_update = true;
                }
            }

            if let Some(period) = update.period {
                Self::ensure_period_not_zero(period)?;

                if period != settings.period {
                    settings.period = period;
                    should_update = true;
                }
            }

            if let Some(period_limit) = update.period_limit {
                Self::ensure_period_limit_not_zero(period_limit)?;

                if period_limit != settings.period_limit {
                    settings.period_limit = period_limit;
                    should_update = true;
                }
            }

            if let Some(drip_limit) = update.drip_limit {
                Self::ensure_drip_limit_not_zero(drip_limit)?;

                if drip_limit != settings.drip_limit {
                    settings.drip_limit = drip_limit;
                    should_update = true;
                }
            }

            ensure!(should_update, Error::<T>::NothingToUpdate);

            FaucetByAccount::<T>::insert(faucet.clone(), settings);
            Self::deposit_event(RawEvent::FaucetUpdated(faucet));
            Ok(())
        }

        #[weight = 20_000 + T::DbWeight::get().reads_writes(0, 0) + 20_000 * faucets.len() as u64]
        pub fn remove_faucets(
            origin,
            faucets: Vec<T::AccountId>
        ) -> DispatchResult {

            ensure_root(origin)?;

            ensure!(!faucets.len().is_zero(), Error::<T>::NoFaucetsProvided);

            let unique_faucets = BTreeSet::from_iter(faucets.iter());
            for faucet in unique_faucets.iter() {
                FaucetByAccount::<T>::remove(faucet);
            }

            Self::deposit_event(RawEvent::FaucetsRemoved(faucets));
            Ok(())
        }

        #[weight = (
            50_000 + T::DbWeight::get().reads_writes(2, 2),
            
            // TODO Replace with Ok(Pays::No.into())
            // See https://github.com/substrate-developer-hub/substrate-node-template/commit/6546b15634bf088e8faee806b5cf266621412889#diff-657cb55f3d39058f730b46f7c84f90698ad43b3ab5c1aa8789a435a230c77f19R106
            Pays::No
        )]
        pub fn drip(
            origin, // Should be a faucet account
            recipient: T::AccountId,
            amount: BalanceOf<T>,
        ) -> DispatchResult {
            let faucet = ensure_signed(origin)?;

            // Validate input values
            ensure!(faucet != recipient, Error::<T>::RecipientEqualsFaucet);
            ensure!(amount > Zero::zero(), Error::<T>::ZeroDripAmountProvided);

            let mut settings = Self::require_faucet(&faucet)?;
            ensure!(settings.enabled, Error::<T>::FaucetDisabled);
            ensure!(amount <= settings.drip_limit, Error::<T>::DripLimitReached);

            let faucet_balance = T::Currency::free_balance(&faucet);
            ensure!(amount <= faucet_balance, Error::<T>::NotEnoughFreeBalanceOnFaucet);

            let current_block = <system::Module<T>>::block_number();

            if settings.next_period_at <= current_block {
                // Move to the next period and reset the period stats
                settings.next_period_at = current_block.saturating_add(settings.period);
                settings.dripped_in_current_period = Zero::zero();
            }

            // Calculate have many tokens still can be dripped in the current period
            let tokens_left_in_current_period = settings.period_limit
                .saturating_sub(settings.dripped_in_current_period);

            ensure!(amount <= tokens_left_in_current_period, Error::<T>::PeriodLimitReached);

            T::Currency::transfer(
                &faucet,
                &recipient,
                amount,
                ExistenceRequirement::KeepAlive
            )?;

            settings.dripped_in_current_period = amount
                .saturating_add(settings.dripped_in_current_period);

            FaucetByAccount::<T>::insert(&faucet, settings);

            Self::deposit_event(RawEvent::Dripped(faucet, recipient, amount));
            Ok(())
        }
    }
}

impl<T: Trait> Module<T> {

    pub fn require_faucet(faucet: &T::AccountId) -> Result<Faucet<T>, DispatchError> {
        Ok(Self::faucet_by_account(faucet).ok_or(Error::<T>::FaucetNotFound)?)
    }

    fn ensure_period_not_zero(period: T::BlockNumber) -> DispatchResult {
        ensure!(period > Zero::zero(), Error::<T>::ZeroPeriodProvided);
        Ok(())
    }

    fn ensure_period_limit_not_zero(period_limit: BalanceOf<T>) -> DispatchResult {
        ensure!(period_limit > Zero::zero(), Error::<T>::ZeroPeriodLimitProvided);
        Ok(())
    }

    fn ensure_drip_limit_not_zero(drip_limit: BalanceOf<T>) -> DispatchResult {
        ensure!(drip_limit > Zero::zero(), Error::<T>::ZeroDripLimitProvided);
        Ok(())
    }
}

impl<T: Trait> Faucet<T> {

    pub fn new(
        period: T::BlockNumber,
        period_limit: BalanceOf<T>,
        drip_limit: BalanceOf<T>,
    ) -> Self {
        Self {
            enabled: true,
            period,
            period_limit,
            drip_limit,

            next_period_at: Zero::zero(),
            dripped_in_current_period: Zero::zero(),
        }
    }
}