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
#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet::*;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[frame_support::pallet]
pub mod pallet {
	use frame_system::pallet_prelude::*;
	use frame_support::{
		dispatch::DispatchResultWithPostInfo,
		pallet_prelude::*
	};
	use logion_shared::{MultisigApproveAsMultiCallFactory, MultisigAsMultiCallFactory, IsLegalOfficer};
	use frame_support::traits::{Vec, UnfilteredDispatchable};
	use pallet_multisig::{WeightInfo, Timepoint, OpaqueCall};

	#[pallet::config]
	pub trait Config: frame_system::Config {

		/// Implementation of multisig "approve as multi"
		type MultisigApproveAsMultiCallFactory: MultisigApproveAsMultiCallFactory<Self::Origin, Self::AccountId, Timepoint<Self::BlockNumber>>;

		/// Implementation of multisig "as multi"
		type MultisigAsMultiCallFactory: MultisigAsMultiCallFactory<Self::Origin, Self::AccountId, Timepoint<Self::BlockNumber>>;

		/// Query for checking that a signer is a legal officer
		type IsLegalOfficer: IsLegalOfficer<Self::AccountId>;

		/// Weight information for extrinsics in this pallet.
		type WeightInfo: WeightInfo;

		/// The overarching event type.
		type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
	}

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

    #[pallet::event]
	pub enum Event<T: Config> {
		
	}

	#[pallet::error]
	pub enum Error<T> {
		/// The set of signatories is invalid (size <> from 2 or does not contain only legal officers on transfer creation).
		InvalidSignatories,
		/// The transfer initiator is a legal officer.
		WrongInitiator,
	}

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

	#[pallet::call]
	impl<T:Config> Pallet<T> {

		/// Create a vault transfer. The creator must not be a legal officer.
		#[pallet::weight({
			// Weight computation comes from multisig pallet
			let s = legal_officers.len() as u32;

			T::WeightInfo::approve_as_multi_create(s)
			.max(T::WeightInfo::approve_as_multi_approve(s))
			.max(T::WeightInfo::approve_as_multi_complete(s))
			.saturating_add(*max_weight)
		})]
		pub fn request_call(
			origin: OriginFor<T>,
			legal_officers: Vec<T::AccountId>,
			call_hash: [u8; 32],
			max_weight: Weight,
		) -> DispatchResultWithPostInfo {
			if legal_officers.len() != 2
					|| !T::IsLegalOfficer::is_legal_officer(&legal_officers[0])
					|| !T::IsLegalOfficer::is_legal_officer(&legal_officers[1]) {
				Err(Error::<T>::InvalidSignatories)?
			} else {
				let who = ensure_signed(origin.clone())?;
				if T::IsLegalOfficer::is_legal_officer(&who) {
					Err(Error::<T>::WrongInitiator)?
				} else {
					Self::dispatch_create_multi(origin, legal_officers, call_hash, max_weight)
				}
			}
		}

		/// Approves a vault transfer.
		#[pallet::weight({
			// Weight computation comes from multisig pallet
			let s = other_signatories.len() as u32;
			let z = call.len() as u32;

			T::WeightInfo::as_multi_create(s, z)
			.max(T::WeightInfo::as_multi_create_store(s, z))
			.max(T::WeightInfo::as_multi_approve(s, z))
			.max(T::WeightInfo::as_multi_complete(s, z))
			.saturating_add(*max_weight)
		})]
		pub fn approve_call(
			origin: OriginFor<T>,
			other_signatories: Vec<T::AccountId>,
			call: OpaqueCall,
			timepoint: Timepoint<T::BlockNumber>,
			max_weight: Weight,
		) -> DispatchResultWithPostInfo {
			if other_signatories.len() != 2 {
				Err(Error::<T>::InvalidSignatories)?
			} else {
				Self::dispatch_as_multi(origin, other_signatories, call, timepoint, max_weight)
			}
		}
	}

	impl<T: Config> Pallet<T> {
		fn dispatch_create_multi(
			origin: OriginFor<T>,
			legal_officers: Vec<T::AccountId>,
			call_hash: [u8; 32],
			max_weight: Weight,
		) -> DispatchResultWithPostInfo {
			let call = <T as Config>::MultisigApproveAsMultiCallFactory::build_approve_as_multi_call(
					2,
					legal_officers,
					None,
					call_hash,
					max_weight
			);
			call.dispatch_bypass_filter(origin)
		}

		fn dispatch_as_multi(
			origin: OriginFor<T>,
			other_signatories: Vec<T::AccountId>,
			call: OpaqueCall,
			timepoint: Timepoint<T::BlockNumber>,
			max_weight: Weight,
		) -> DispatchResultWithPostInfo {
			let call = <T as Config>::MultisigAsMultiCallFactory::build_as_multi_call(
					2,
					other_signatories,
					Some(timepoint),
					call,
					false,
					max_weight
			);
			call.dispatch_bypass_filter(origin)
		}
	}
}