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
//! Multisig Operations.
//!
//! ## Overview
//!
//! Handles the core actions within an already established multisig.
//!
//! ### Core functionalities:
//! - Minting/Burning voting tokens to existing and new members.
//! - Handling proposal votes.
//! - Dispatching approved proposals when both support and approval meet/exceed their minimum required thresholds.
//! - Canceling proposals.

use super::pallet::{self, *};
use crate::{
    account_derivation::CoreAccountDerivation,
    fee_handling::{FeeAsset, FeeAssetNegativeImbalance, MultisigFeeHandler},
    origin::{ensure_multisig, INV4Origin},
    voting::{Tally, Vote},
};
use codec::DecodeLimit;
use core::{
    convert::{TryFrom, TryInto},
    iter::Sum,
};
use frame_support::{
    pallet_prelude::*,
    traits::{
        fungibles::{Inspect, Mutate},
        tokens::{Fortitude, Precision},
        Currency, ExistenceRequirement, VoteTally, WithdrawReasons,
    },
    weights::WeightToFee,
    BoundedBTreeMap,
};
use frame_system::{ensure_signed, pallet_prelude::*};
use sp_runtime::{
    traits::{Hash, Zero},
    Perbill,
};
use sp_std::{boxed::Box, collections::btree_map::BTreeMap};

/// Maximum size of call we store is 50kb.
pub const MAX_SIZE: u32 = 50 * 1024;

pub type BoundedCallBytes<T> = BoundedVec<u8, <T as Config>::MaxCallSize>;

/// Details of a multisig operation.
#[derive(Clone, Encode, Decode, RuntimeDebug, MaxEncodedLen, TypeInfo, PartialEq, Eq)]
pub struct MultisigOperation<AccountId, TallyOf, Call, Metadata> {
    pub tally: TallyOf,
    pub original_caller: AccountId,
    pub actual_call: Call,
    pub metadata: Option<Metadata>,
    pub fee_asset: FeeAsset,
}

pub type MultisigOperationOf<T> = MultisigOperation<
    <T as frame_system::Config>::AccountId,
    Tally<T>,
    BoundedCallBytes<T>,
    BoundedVec<u8, <T as pallet::Config>::MaxMetadata>,
>;

impl<T: Config> Pallet<T>
where
    Result<INV4Origin<T>, <T as frame_system::Config>::RuntimeOrigin>:
        From<<T as frame_system::Config>::RuntimeOrigin>,
    <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance: Sum,
    <T as frame_system::Config>::AccountId: From<[u8; 32]>,
{
    /// Inner function for the token_mint call.
    pub(crate) fn inner_token_mint(
        origin: OriginFor<T>,
        amount: BalanceOf<T>,
        target: T::AccountId,
    ) -> DispatchResult {
        // Grab the core id from the origin
        let core_origin = ensure_multisig::<T, OriginFor<T>>(origin)?;
        let core_id = core_origin.id;

        // Mint the core's voting token to the target.
        T::AssetsProvider::mint_into(core_id, &target, amount)?;

        Self::deposit_event(Event::Minted {
            core_id,
            target,
            amount,
        });

        Ok(())
    }

    /// Inner function for the token_burn call.
    pub(crate) fn inner_token_burn(
        origin: OriginFor<T>,
        amount: BalanceOf<T>,
        target: T::AccountId,
    ) -> DispatchResult {
        // Grab the core id from the origin
        let core_origin = ensure_multisig::<T, OriginFor<T>>(origin)?;
        let core_id = core_origin.id;

        // Burn the core's voting token from the target.
        T::AssetsProvider::burn_from(
            core_id,
            &target,
            amount,
            Precision::Exact,
            Fortitude::Polite,
        )?;

        Self::deposit_event(Event::Burned {
            core_id,
            target,
            amount,
        });

        Ok(())
    }

    /// Inner function for the operate_multisig call.
    pub(crate) fn inner_operate_multisig(
        caller: OriginFor<T>,
        core_id: T::CoreId,
        metadata: Option<BoundedVec<u8, T::MaxMetadata>>,
        fee_asset: FeeAsset,
        call: Box<<T as Config>::RuntimeCall>,
    ) -> DispatchResultWithPostInfo {
        let owner = ensure_signed(caller)?;

        // Get the voting token balance of the caller
        let owner_balance: BalanceOf<T> = T::AssetsProvider::balance(core_id, &owner);

        ensure!(!owner_balance.is_zero(), Error::<T>::NoPermission);

        // Get the minimum support value of the target core
        let (minimum_support, _) = Pallet::<T>::minimum_support_and_required_approval(core_id)
            .ok_or(Error::<T>::CoreNotFound)?;

        // Get the total issuance of the core's voting token
        let total_issuance: BalanceOf<T> = T::AssetsProvider::total_issuance(core_id);

        // Compute the call hash
        let call_hash = <<T as frame_system::Config>::Hashing as Hash>::hash_of(&call);

        // Make sure this exact multisig call doesn't already exist
        ensure!(
            Multisig::<T>::get(core_id, call_hash).is_none(),
            Error::<T>::MultisigCallAlreadyExists
        );

        // If caller has enough balance to meet/exeed the threshold, then go ahead and execute the call now
        // There is no need to check against required_approval as it's assumed the caller is voting aye
        if Perbill::from_rational(owner_balance, total_issuance) >= minimum_support {
            let dispatch_result =
                crate::dispatch::dispatch_call::<T>(core_id, &fee_asset, *call.clone());

            Self::deposit_event(Event::MultisigExecuted {
                core_id,
                executor_account: Self::derive_core_account(core_id),
                voter: owner,
                call_hash,
                call: *call,
                result: dispatch_result.map(|_| ()).map_err(|e| e.error),
            });
        } else {
            // Wrap the call making sure it fits the size boundary
            let bounded_call: BoundedCallBytes<T> = (*call)
                .encode()
                .try_into()
                .map_err(|_| Error::<T>::MaxCallLengthExceeded)?;

            let total_lenght = (bounded_call.len() as u64)
                .saturating_add(metadata.clone().unwrap_or_default().len() as u64);

            let storage_cost: BalanceOf<T> =
                T::LengthToFee::weight_to_fee(&Weight::from_parts(total_lenght as u64, 0));

            T::FeeCharger::handle_creation_fee(FeeAssetNegativeImbalance::Native(
                <T as Config>::Currency::withdraw(
                    &owner,
                    storage_cost,
                    WithdrawReasons::TRANSACTION_PAYMENT,
                    ExistenceRequirement::KeepAlive,
                )?,
            ));

            // Insert proposal in storage, it's now in the voting stage
            Multisig::<T>::insert(
                core_id,
                call_hash,
                MultisigOperation {
                    tally: Tally::from_parts(
                        owner_balance,
                        Zero::zero(),
                        BoundedBTreeMap::try_from(BTreeMap::from([(
                            owner.clone(),
                            Vote::Aye(owner_balance),
                        )]))
                        .map_err(|_| Error::<T>::MaxCallersExceeded)?,
                    ),
                    original_caller: owner.clone(),
                    actual_call: bounded_call,
                    metadata,
                    fee_asset,
                },
            );

            Self::deposit_event(Event::MultisigVoteStarted {
                core_id,
                executor_account: Self::derive_core_account(core_id),
                voter: owner,
                votes_added: Vote::Aye(owner_balance),
                call_hash,
            });
        }

        Ok(().into())
    }

    /// Inner function for the vote_multisig call.
    pub(crate) fn inner_vote_multisig(
        caller: OriginFor<T>,
        core_id: T::CoreId,
        call_hash: T::Hash,
        aye: bool,
    ) -> DispatchResultWithPostInfo {
        Multisig::<T>::try_mutate_exists(core_id, call_hash, |data| {
            let owner = ensure_signed(caller.clone())?;

            // Get the voting token balance of the caller
            let voter_balance: BalanceOf<T> = T::AssetsProvider::balance(core_id, &owner);

            // If caller doesn't own the token, they have no voting power.
            ensure!(!voter_balance.is_zero(), Error::<T>::NoPermission);

            // Get the multisig call data from the storage
            let mut old_data = data.take().ok_or(Error::<T>::MultisigCallNotFound)?;

            // Get the minimum support and required approval values of the target core
            let (minimum_support, required_approval) =
                Pallet::<T>::minimum_support_and_required_approval(core_id)
                    .ok_or(Error::<T>::CoreNotFound)?;

            let new_vote_record = if aye {
                Vote::Aye(voter_balance)
            } else {
                Vote::Nay(voter_balance)
            };

            // Mutate tally with the new vote
            old_data
                .tally
                .process_vote(owner.clone(), Some(new_vote_record))?;

            let support = old_data.tally.support(core_id);
            let approval = old_data.tally.approval(core_id);

            // Check if the multisig proposal passes the thresholds with the added vote
            if (support >= minimum_support) && (approval >= required_approval) {
                // Decode the call
                let decoded_call = <T as Config>::RuntimeCall::decode_all_with_depth_limit(
                    sp_api::MAX_EXTRINSIC_DEPTH / 4,
                    &mut &old_data.actual_call[..],
                )
                .map_err(|_| Error::<T>::FailedDecodingCall)?;

                // If the proposal thresholds are met, remove proposal from storage
                *data = None;

                // Dispatch the call and get the result
                let dispatch_result = crate::dispatch::dispatch_call::<T>(
                    core_id,
                    &old_data.fee_asset,
                    decoded_call.clone(),
                );

                Self::deposit_event(Event::MultisigExecuted {
                    core_id,
                    executor_account: Self::derive_core_account(core_id),
                    voter: owner,
                    call_hash,
                    call: decoded_call,
                    result: dispatch_result.map(|_| ()).map_err(|e| e.error),
                });
            } else {
                // If the thresholds aren't met, update storage with the new tally
                *data = Some(old_data.clone());

                Self::deposit_event(Event::MultisigVoteAdded {
                    core_id,
                    executor_account: Self::derive_core_account(core_id),
                    voter: owner,
                    votes_added: new_vote_record,
                    current_votes: old_data.tally,
                    call_hash,
                });
            }

            Ok(().into())
        })
    }

    /// Inner function for the withdraw_token_multisig call.
    pub(crate) fn inner_withdraw_vote_multisig(
        caller: OriginFor<T>,
        core_id: T::CoreId,
        call_hash: T::Hash,
    ) -> DispatchResultWithPostInfo {
        Multisig::<T>::try_mutate_exists(core_id, call_hash, |data| {
            let owner = ensure_signed(caller.clone())?;

            // Get the voting token balance of the caller
            let mut old_data = data.take().ok_or(Error::<T>::MultisigCallNotFound)?;

            // Try to mutate tally to remove the vote
            let old_vote = old_data.tally.process_vote(owner.clone(), None)?;

            // Update storage with the new tally
            *data = Some(old_data.clone());

            Self::deposit_event(Event::MultisigVoteWithdrawn {
                core_id,
                executor_account: Self::derive_core_account(core_id),
                voter: owner,
                votes_removed: old_vote,
                call_hash,
            });

            Ok(().into())
        })
    }

    /// Inner function for the cancel_multisig_proposal call.
    pub(crate) fn inner_cancel_multisig_proposal(
        origin: OriginFor<T>,
        call_hash: T::Hash,
    ) -> DispatchResultWithPostInfo {
        // Ensure that this is being called by the multisig origin rather than by a normal caller
        let core_origin = ensure_multisig::<T, OriginFor<T>>(origin)?;
        let core_id = core_origin.id;

        // Remove the proposal from storage
        Multisig::<T>::remove(core_id, call_hash);

        Self::deposit_event(Event::<T>::MultisigCanceled { core_id, call_hash });

        Ok(().into())
    }

    pub fn add_member(core_id: &T::CoreId, member: &T::AccountId) {
        CoreMembers::<T>::insert(core_id, member, ())
    }

    pub fn remove_member(core_id: &T::CoreId, member: &T::AccountId) {
        CoreMembers::<T>::remove(core_id, member)
    }
}