This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from logion-network/feature/safe_multisig
feat: implement vault.
- Loading branch information
Showing
11 changed files
with
475 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
[package] | ||
authors = ['Logion Team <https://github.com/logion-network>'] | ||
description = 'Pallet implements Logion Vault.' | ||
edition = '2018' | ||
homepage = 'https://logion.network' | ||
license = 'Apache 2.0' | ||
name = 'pallet-logion-vault' | ||
repository = 'https://github.com/logion-network/logion-node' | ||
version = '0.1.0' | ||
|
||
[package.metadata.docs.rs] | ||
targets = ['x86_64-unknown-linux-gnu'] | ||
|
||
[dependencies] | ||
codec = { default-features = false, features = ['derive'], package = 'parity-scale-codec', version = '2.0.0' } | ||
frame-system = { default-features = false, version = '3.0.0' } | ||
frame-support = { default-features = false, version = '3.0.0' } | ||
frame-benchmarking = { default-features = false, optional = true, version = '3.1.0' } | ||
logion-shared = { default-features = false, path = '../shared/', version = '0.1.0' } | ||
pallet-multisig = { default-features = false, version='3.0.0' } | ||
serde = { default-features = false, version = "1.0.119" } | ||
sp-std = { default-features = false, version = '3.0.0' } | ||
|
||
[dev-dependencies] | ||
sp-core = { default-features = false, version = '3.0.0' } | ||
sp-io = { default-features = false, version = '3.0.0' } | ||
sp-runtime = { default-features = false, version = '3.0.0' } | ||
|
||
[features] | ||
default = ['std'] | ||
std = [ | ||
'codec/std', | ||
'frame-support/std', | ||
'frame-system/std', | ||
'frame-benchmarking/std', | ||
'pallet-multisig/std', | ||
'serde/std', | ||
'sp-std/std', | ||
] | ||
runtime-benchmarks = [ | ||
'frame-benchmarking', | ||
'frame-support/runtime-benchmarks', | ||
'frame-system/runtime-benchmarks', | ||
] | ||
# Note: frame-support `try-runtime` feature is released after v3. | ||
# Uncomment the following line when `frame-support` version > `3.0.0`. | ||
# try-runtime = ['frame-support/try-runtime'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) | ||
} | ||
} | ||
} |
Oops, something went wrong.