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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//                              Næ§@@@ÑÉ©
//                        æ@@@@@@@@@@@@@@@@@@
//                    Ñ@@@@?.?@@@@@@@@@@@@@@@@@@@N
//                 ¶@@@@@?^%@@.=@@@@@@@@@@@@@@@@@@@@
//               N@@@@@@@?^@@@»^@@@@@@@@@@@@@@@@@@@@@@
//               @@@@@@@@?^@@@».............?@@@@@@@@@É
//              Ñ@@@@@@@@?^@@@@@@@@@@@@@@@@@@'?@@@@@@@@Ñ
//              @@@@@@@@@?^@@@»..............»@@@@@@@@@@
//              @@@@@@@@@?^@@@»^@@@@@@@@@@@@@@@@@@@@@@@@
//              @@@@@@@@@?^ë@@&.@@@@@@@@@@@@@@@@@@@@@@@@
//               @@@@@@@@?^´@@@o.%@@@@@@@@@@@@@@@@@@@@©
//                @@@@@@@?.´@@@@@ë.........*.±@@@@@@@æ
//                 @@@@@@@@?´.I@@@@@@@@@@@@@@.&@@@@@N
//                  N@@@@@@@@@@ë.*=????????=?@@@@@Ñ
//                    @@@@@@@@@@@@@@@@@@@@@@@@@@@¶
//                        É@@@@@@@@@@@@@@@@Ѷ
//                             Næ§@@@ÑÉ©

// Copyright 2020 Chris D'Costa
// This file is part of Totem Live Accounting.
// Authors:
// - Félix Daudré-Vignier   email: felix@totemaccounting.com
// - Chris D'Costa          email: chris.dcosta@totemaccounting.com

// Totem is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Totem is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Totem.  If not, see <http://www.gnu.org/licenses/>.

//! Allows to fund/crowdsale Totem.

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

pub use pallet::*;

#[frame_support::pallet]
mod pallet {

    use frame_support::{fail, pallet_prelude::*};
    use frame_system::pallet_prelude::*;
    use totem_primitives::Set;

    #[pallet::pallet]
    #[pallet::generate_store(trait Store)]
    pub struct Pallet<T>(_);

    /// Defines if the transfer mechanism is open yet.
    #[pallet::storage]
    #[pallet::getter(fn transfer_status)]
    pub type TransferStatus<T: Config> = StorageValue<_, bool, ValueQuery>;

    /// The Maximum Quantity of Coins that can be minted.
    #[pallet::storage]
    #[pallet::getter(fn max_issuance)]
    pub type MaxlIssuance<T: Config> = StorageValue<_, u128, ValueQuery>;

    /// Initially 45% of Supply (Reserved Funds).
    #[pallet::storage]
    #[pallet::getter(fn unissued)]
    pub type UnIssued<T: Config> = StorageValue<_, u128, ValueQuery>;

    /// Initially 55% of Supply Reduces as funds distributed.
    #[pallet::storage]
    #[pallet::getter(fn issued)]
    pub type Issued<T: Config> = StorageValue<_, u128, ValueQuery>;

    /// Controller of funds (Live Accounting Association Account).
    #[pallet::storage]
    #[pallet::getter(fn controller)]
    pub type Controller<T: Config> = StorageValue<_, T::AccountId>;

    /// The number of coins distributed. It should equal the sum in AccountIdBalances.
    #[pallet::storage]
    #[pallet::getter(fn total_distributed)]
    pub type TotalDistributed<T: Config> = StorageValue<_, u128, ValueQuery>;

    /// Place to store investors accountids with balances.
    #[pallet::storage]
    #[pallet::getter(fn account_id_balances)]
    pub type AccountIdBalances<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, u128>;

    /// List of account Ids who have tokens (updated when token value is 0).
    #[pallet::storage]
    #[pallet::getter(fn holders_account_ids)]
    pub type HoldersAccountIds<T: Config> = StorageValue<_, Set<T::AccountId>, ValueQuery>;

    #[pallet::config]
    pub trait Config: frame_system::Config {
        type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
    }

    #[pallet::genesis_config]
    pub struct GenesisConfig<T: Config> {
        transfer_status: bool,
        max_issuance: u128,
        unissued: u128,
        issued: u128,
        _marker: PhantomData<T>,
    }

    #[cfg(feature = "std")]
    impl<T: Config> Default for GenesisConfig<T> {
        fn default() -> Self {
            GenesisConfig {
                transfer_status: false,
                max_issuance: 161_803_398_875_u128,
                unissued: 72_811_529_493_u128,
                issued: 88_991_869_382_u128,
                _marker: PhantomData,
            }
        }
    }

    #[pallet::genesis_build]
    impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
        fn build(&self) {
            TransferStatus::<T>::put(self.transfer_status);
            MaxlIssuance::<T>::put(self.max_issuance);
            UnIssued::<T>::put(self.unissued);
            Issued::<T>::put(self.issued);
        }
    }

    #[pallet::hooks]
    impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        /// Super User sets the controller account.
        #[pallet::weight(0/*TODO*/)]
        pub fn set_controller_account(
            origin: OriginFor<T>,
            controller: T::AccountId,
        ) -> DispatchResultWithPostInfo {
            // Only Sudo
            let _who = ensure_root(origin)?;

            // abandon if this is the same controller
            if Self::is_controller(&controller) {
                fail!(Error::<T>::SameController);
            }

            // insert new controller
            Controller::<T>::put(controller);

            Ok(().into())
        }

        /// Super User sets the transfers to open or closed.
        #[pallet::weight(0/*TODO*/)]
        pub fn set_transfer_status(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
            let _who = ensure_root(origin)?;

            if Self::transfer_status() {
                TransferStatus::<T>::put(false)
            } else if Self::check_setup() {
                TransferStatus::<T>::put(true)
            } else {
                fail!(Error::<T>::ControllerNotSet);
            }

            Ok(().into())
        }

        /// Super User can only mint coins if transfers are disabled.
        #[pallet::weight(0/*TODO*/)]
        pub fn mint_coins(origin: OriginFor<T>, quantity: u128) -> DispatchResultWithPostInfo {
            let _who = ensure_root(origin)?;

            if Self::transfer_status() {
                fail!(Error::<T>::CannotMintCoins);
            }

            let supply = Self::max_issuance()
                .checked_add(quantity)
                .ok_or(Error::<T>::Overflow)?;
            let unissued = Self::unissued()
                .checked_add(quantity)
                .ok_or(Error::<T>::Overflow)?;

            // Update unissued account with new balance
            UnIssued::<T>::put(unissued);
            // Update Max Supply
            MaxlIssuance::<T>::put(supply);

            Ok(().into())
        }

        /// Super User can move from unissued to issued coins if transfers are disabled.
        #[pallet::weight(0/*TODO*/)]
        pub fn rebalance_issued_coins(
            origin: OriginFor<T>,
            amount: u128,
        ) -> DispatchResultWithPostInfo {
            let _who = ensure_root(origin)?;
            let unissued = Self::unissued();

            // check that the amount is not greater than the available funds
            if amount > unissued {
                fail!(Error::<T>::InsufficientFunds);
            }

            // Those should never error.
            let unissued = unissued.checked_sub(amount).ok_or(Error::<T>::Overflow)?;
            let issued = Self::issued()
                .checked_add(amount)
                .ok_or(Error::<T>::Overflow)?;

            UnIssued::<T>::put(unissued);
            Issued::<T>::put(issued);

            Ok(().into())
        }

        /// Only the controller can do the initial distribution
        #[pallet::weight(0/*TODO*/)]
        pub fn distribute(
            origin: OriginFor<T>,
            to: T::AccountId,
            amount: u128,
        ) -> DispatchResultWithPostInfo {
            let who = ensure_signed(origin)?;
            let issued = Self::issued();

            // ensure that this is the controller account
            if Self::is_controller(&who) == false {
                fail!(Error::<T>::NotController);
            }

            if amount > issued {
                fail!(Error::<T>::InsufficientFunds);
            }

            let issued = issued.checked_sub(amount).ok_or(Error::<T>::Overflow)?;
            let new_balance = match Self::account_id_balances(&to) {
                Some(b) => {
                    let new_balance = b.checked_add(amount).ok_or(Error::<T>::Overflow)?;
                    AccountIdBalances::<T>::take(&to);
                    new_balance
                }
                None => 0,
            };
            // Ensure that the amount to send is less the available funds.
            let total_distributed = Self::total_distributed()
                .checked_add(amount)
                .ok_or(Error::<T>::Overflow)?;

            Issued::<T>::put(issued);
            AccountIdBalances::<T>::insert(&to, new_balance);
            TotalDistributed::<T>::put(total_distributed);
            HoldersAccountIds::<T>::mutate(|holders_account_ids| holders_account_ids.insert(to));

            Ok(().into())
        }

        /// This function transfers funds between accounts (only when opened)
        #[pallet::weight(0/*TODO*/)]
        pub fn transfer(
            origin: OriginFor<T>,
            to: T::AccountId,
            amount: u128,
        ) -> DispatchResultWithPostInfo {
            let from = ensure_signed(origin)?;

            // are transfers open?
            if Self::transfer_status() == false {
                fail!(Error::<T>::TransfersNotOpen);
            }

            // Get the balance of sender
            let new_sender_balance =
                Self::account_id_balances(&from).ok_or(Error::<T>::InsufficientFunds)?;
            let new_receiver_balance = Self::account_id_balances(&to).unwrap_or(0);

            match new_sender_balance.cmp(&amount) {
                core::cmp::Ordering::Less => fail!(Error::<T>::InsufficientFunds),
                core::cmp::Ordering::Greater => {
                    // reduce balance on sender
                    let new_sender_balance = new_sender_balance
                        .checked_sub(amount)
                        .ok_or(Error::<T>::Overflow)?;
                    // increase balance on receiver
                    let new_receiver_balance = new_receiver_balance
                        .checked_add(amount)
                        .ok_or(Error::<T>::Overflow)?;

                    AccountIdBalances::<T>::insert(&from, new_sender_balance);
                    AccountIdBalances::<T>::insert(&to, new_receiver_balance);
                    HoldersAccountIds::<T>::mutate(|holders_account_ids| {
                        holders_account_ids.insert(to)
                    });
                }
                core::cmp::Ordering::Equal => {
                    let new_receiver_balance = Self::account_id_balances(&to)
                        .unwrap_or(0)
                        .checked_add(amount)
                        .ok_or(Error::<T>::Overflow)?;
                    // balance of sender will be 0 remove from table
                    AccountIdBalances::<T>::remove(&from);
                    HoldersAccountIds::<T>::mutate(|holders_account_ids| {
                        holders_account_ids.remove(&from)
                    });
                    // increase balance on receiver
                    AccountIdBalances::<T>::insert(&to, new_receiver_balance);
                    HoldersAccountIds::<T>::mutate(|holders_account_ids| {
                        holders_account_ids.insert(to)
                    });
                }
            }

            Ok(().into())
        }
    }

    impl<T: Config> Pallet<T> {
        /// Checks if the account parameter is the controller.
        fn is_controller(account: &T::AccountId) -> bool {
            Self::controller()
                .map(|controller| account == &controller)
                .unwrap_or(false)
        }
    }

    #[pallet::error]
    pub enum Error<T> {
        /// You cannot change a controller to the same controller.
        SameController,
        /// You are not the controller.
        NotController,
        /// Cannot open transfers when controller not set.
        ControllerNotSet,
        /// Cannot mint whilst transfers open.
        CannotMintCoins,
        /// Minting Overflowed.
        Overflow,
        /// Insufficient funds to rebalance.
        InsufficientFunds,
        /// Transfers not open.
        TransfersNotOpen,
    }

    #[pallet::event]
    pub enum Event<T: Config> {
        SuccessMessage(T::AccountId),
    }

    impl<T: Config> Pallet<T> {
        /// Checks if all the setup actions have been done.
        fn check_setup() -> bool {
            Controller::<T>::exists()
        }
    }
}