Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 13 additions & 23 deletions pallets/salp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ pub mod pallet {
Success = MultiLocation,
>;

type EnsureConfirmAsMultiSig: EnsureOrigin<
<Self as frame_system::Config>::Origin,
Success = AccountIdOf<Self>,
>;

type BifrostXcmExecutor: BifrostXcmExecutor;

#[pallet::constant]
Expand Down Expand Up @@ -453,13 +458,11 @@ pub mod pallet {
origin: OriginFor<T>,
#[pallet::compact] index: ParaId,
) -> DispatchResult {
let owner = ensure_signed(origin)?;
let _owner = T::EnsureConfirmAsMultiSig::ensure_origin(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(fund.status == FundStatus::Ongoing, Error::<T>::InvalidFundStatus);

ensure!(owner == fund.depositor, Error::<T>::UnauthorizedAccount);

let fund_new = FundInfo { status: FundStatus::Success, ..fund };
Funds::<T>::insert(index, Some(fund_new));

Expand All @@ -472,14 +475,12 @@ pub mod pallet {
Pays::No
))]
pub fn fund_fail(origin: OriginFor<T>, #[pallet::compact] index: ParaId) -> DispatchResult {
let owner = ensure_signed(origin)?;
let _owner = T::EnsureConfirmAsMultiSig::ensure_origin(origin)?;

// crownload is failed, so enable the withdrawal function of vsToken/vsBond
let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(fund.status == FundStatus::Ongoing, Error::<T>::InvalidFundStatus);

ensure!(owner == fund.depositor, Error::<T>::UnauthorizedAccount);

let fund_new = FundInfo { status: FundStatus::Failed, ..fund };
Funds::<T>::insert(index, Some(fund_new));

Expand All @@ -495,13 +496,11 @@ pub mod pallet {
origin: OriginFor<T>,
#[pallet::compact] index: ParaId,
) -> DispatchResult {
let owner = ensure_signed(origin)?;
let _owner = T::EnsureConfirmAsMultiSig::ensure_origin(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(fund.status == FundStatus::Success, Error::<T>::InvalidFundStatus);

ensure!(owner == fund.depositor, Error::<T>::UnauthorizedAccount);

let fund_new = FundInfo { status: FundStatus::Retired, ..fund };
Funds::<T>::insert(index, Some(fund_new));

Expand All @@ -514,7 +513,7 @@ pub mod pallet {
Pays::No
))]
pub fn fund_end(origin: OriginFor<T>, #[pallet::compact] index: ParaId) -> DispatchResult {
let owner = ensure_signed(origin)?;
let _owner = T::EnsureConfirmAsMultiSig::ensure_origin(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(
Expand All @@ -523,8 +522,6 @@ pub mod pallet {
Error::<T>::InvalidFundStatus
);

ensure!(owner == fund.depositor, Error::<T>::UnauthorizedAccount);

let fund_new = FundInfo { status: FundStatus::End, ..fund };
Funds::<T>::insert(index, Some(fund_new));

Expand Down Expand Up @@ -574,7 +571,6 @@ pub mod pallet {
Ok(())
}

/// TODO: Refactor the docs.
/// Create a new crowdloaning campaign for a parachain slot deposit for the current auction.
#[pallet::weight((
0,
Expand Down Expand Up @@ -675,16 +671,14 @@ pub mod pallet {
#[pallet::compact] index: ParaId,
is_success: bool,
) -> DispatchResult {
let owner = ensure_signed(origin)?;
let _owner = T::EnsureConfirmAsMultiSig::ensure_origin(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let can_confirm = fund.status == FundStatus::Ongoing ||
fund.status == FundStatus::Failed ||
fund.status == FundStatus::Success;
ensure!(can_confirm, Error::<T>::InvalidFundStatus);

ensure!(owner == fund.depositor, Error::<T>::UnauthorizedAccount);

let (contributed, status) = Self::contribution(fund.trie_index, &who);
ensure!(status.is_contributing(), Error::<T>::InvalidContributionStatus);
let contributing = status.contributing();
Expand Down Expand Up @@ -768,14 +762,12 @@ pub mod pallet {
#[pallet::compact] index: ParaId,
is_success: bool,
) -> DispatchResult {
let owner = ensure_signed(origin)?;
let owner = T::EnsureConfirmAsMultiSig::ensure_origin(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
let can = fund.status == FundStatus::Failed || fund.status == FundStatus::Retired;
ensure!(can, Error::<T>::InvalidFundStatus);

ensure!(owner == fund.depositor, Error::<T>::UnauthorizedAccount);

let amount_withdrew = fund.raised;

if is_success {
Expand Down Expand Up @@ -854,13 +846,11 @@ pub mod pallet {
#[pallet::compact] index: ParaId,
is_success: bool,
) -> DispatchResult {
let owner = ensure_signed(origin)?;
let _owner = T::EnsureConfirmAsMultiSig::ensure_origin(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(fund.status == FundStatus::RefundWithdrew, Error::<T>::InvalidFundStatus);

ensure!(owner == fund.depositor, Error::<T>::UnauthorizedAccount);

let (contributed, status) = Self::contribution(fund.trie_index, &who);
ensure!(status == ContributionStatus::Refunding, Error::<T>::InvalidContributionStatus);

Expand Down Expand Up @@ -961,7 +951,7 @@ pub mod pallet {
) -> DispatchResult {
use RedeemStatus as RS;

ensure_root(origin).map_err(|_| Error::<T>::UnauthorizedAccount)?;
let _owner = T::EnsureConfirmAsMultiSig::ensure_origin(origin)?;

let status = Self::redeem_status(who.clone(), (index, first_slot, last_slot));
ensure!(status.is_redeeming(), Error::<T>::InvalidRedeemStatus);
Expand Down
32 changes: 30 additions & 2 deletions pallets/salp/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@

// Ensure we're `no_std` when compiling for Wasm.

use frame_support::{construct_runtime, parameter_types, traits::GenesisBuild, PalletId};
use frame_support::{
construct_runtime, parameter_types,
traits::{EnsureOrigin, GenesisBuild},
PalletId,
};
use frame_system::RawOrigin;
use node_primitives::{Amount, Balance, CurrencyId, TokenSymbol, TransferOriginType};
use sp_arithmetic::Percent;
use sp_core::H256;
Expand Down Expand Up @@ -164,7 +169,6 @@ parameter_types! {
pub const BifrostCrowdloanId: PalletId = PalletId(*b"bf/salp#");
pub const RemoveKeysLimit: u32 = 50;
pub const SlotLength: BlockNumber = 8u32 as BlockNumber;

pub const LeasePeriod: BlockNumber = 6 * WEEKS;
pub const VSBondValidPeriod: BlockNumber = 30 * DAYS;
pub const ReleaseCycle: BlockNumber = 1 * DAYS;
Expand All @@ -175,6 +179,7 @@ parameter_types! {
pub ContributionWeight:u64 = 1_000_000_000 as u64;
pub WithdrawWeight:u64 = 1_000_000_000 as u64;
pub const SelfParaId: u32 = 2001;
pub ConfirmMuitiSigAccount:AccountId = ALICE;//AccountId::new([0u8; 32]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Multisig::multi_account_id() in tests

}

parameter_types! {
Expand All @@ -183,6 +188,28 @@ parameter_types! {

type LocalOriginToLocation = (SignedToAccountId32<Origin, AccountId, AnyNetwork>,);

pub struct EnsureConfirmAsMultiSig;
impl EnsureOrigin<Origin> for EnsureConfirmAsMultiSig {
type Success = AccountId;

fn try_origin(o: Origin) -> Result<Self::Success, Origin> {
Into::<Result<RawOrigin<AccountId>, Origin>>::into(o).and_then(|o| match o {
RawOrigin::Signed(who) =>
if who == ConfirmMuitiSigAccount::get() {
Ok(who)
} else {
Err(Origin::from(Some(who)))
},
r => Err(Origin::from(r)),
})
}

#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> Origin {
Origin::from(RawOrigin::Signed(ConfirmMuitiSigAccount::get()))
}
}

impl salp::Config for Test {
type BancorPool = Bancor;
type BifrostXcmExecutor = MockXcmExecutor;
Expand All @@ -206,6 +233,7 @@ impl salp::Config for Test {
type BaseXcmWeight = BaseXcmWeight;
type ContributionWeight = ContributionWeight;
type WithdrawWeight = WithdrawWeight;
type EnsureConfirmAsMultiSig = EnsureConfirmAsMultiSig;
}

// To control the result returned by `MockXcmExecutor`
Expand Down
56 changes: 32 additions & 24 deletions pallets/salp/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ fn set_fund_success_with_wrong_origin_should_fail() {
assert_ok!(Salp::create(Some(ALICE).into(), 3_000, 1_000, 1, SlotLength::get()));
assert_noop!(Salp::fund_success(Origin::root(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_success(Origin::none(), 3_000), DispatchError::BadOrigin);
assert_noop!(
Salp::fund_success(Some(BRUCE).into(), 3_000),
Error::<Test>::UnauthorizedAccount
);
assert_noop!(Salp::fund_success(Some(BRUCE).into(), 3_000), DispatchError::BadOrigin);
})
}

Expand Down Expand Up @@ -142,10 +139,7 @@ fn set_fund_fail_with_wrong_origin_should_fail() {
assert_ok!(Salp::create(Some(ALICE).into(), 3_000, 1_000, 1, SlotLength::get()));
assert_noop!(Salp::fund_fail(Origin::root(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_fail(Origin::none(), 3_000), DispatchError::BadOrigin);
assert_noop!(
Salp::fund_fail(Some(BRUCE).into(), 3_000),
Error::<Test>::UnauthorizedAccount
);
assert_noop!(Salp::fund_fail(Some(BRUCE).into(), 3_000), DispatchError::BadOrigin);
});
}

Expand Down Expand Up @@ -186,10 +180,7 @@ fn set_fund_retire_with_wrong_origin_should_fail() {
assert_ok!(Salp::fund_success(Some(ALICE).into(), 3_000));
assert_noop!(Salp::fund_retire(Origin::root(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_retire(Origin::none(), 3_000), DispatchError::BadOrigin);
assert_noop!(
Salp::fund_retire(Some(BRUCE).into(), 3_000),
Error::<Test>::UnauthorizedAccount
);
assert_noop!(Salp::fund_retire(Some(BRUCE).into(), 3_000), DispatchError::BadOrigin);
});
}

Expand Down Expand Up @@ -240,7 +231,7 @@ fn set_fund_end_with_wrong_origin_should_fail() {

assert_noop!(Salp::fund_end(Origin::root(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_end(Origin::none(), 3_000), DispatchError::BadOrigin);
assert_noop!(Salp::fund_end(Some(BRUCE).into(), 3_000), Error::<Test>::UnauthorizedAccount);
assert_noop!(Salp::fund_end(Some(BRUCE).into(), 3_000), DispatchError::BadOrigin);
});
}

Expand Down Expand Up @@ -463,7 +454,7 @@ fn contribute_with_wrong_origin_should_fail() {
);
assert_noop!(
Salp::confirm_contribute(Some(BRUCE).into(), BRUCE, 3000, true),
Error::<Test>::UnauthorizedAccount,
DispatchError::BadOrigin,
);
});
}
Expand Down Expand Up @@ -942,7 +933,7 @@ fn refund_with_wrong_origin_should_fail() {
);
assert_noop!(
Salp::confirm_refund(Some(BRUCE).into(), BRUCE, 3_000, true),
Error::<Test>::UnauthorizedAccount
DispatchError::BadOrigin
);
});
}
Expand Down Expand Up @@ -1081,7 +1072,14 @@ fn redeem_should_work() {
assert_ok!(<Tokens as MultiCurrency<AccountId>>::transfer(vsBond, &BRUCE, &CATHI, 50));

assert_ok!(Salp::redeem(Some(BRUCE).into(), 3_000, 1, SlotLength::get(), 50));
assert_ok!(Salp::confirm_redeem(Origin::root(), BRUCE, 3_000, 1, SlotLength::get(), true));
assert_ok!(Salp::confirm_redeem(
Some(ALICE).into(),
BRUCE,
3_000,
1,
SlotLength::get(),
true
));

assert_eq!(Salp::redeem_pool(), 50);

Expand All @@ -1093,7 +1091,14 @@ fn redeem_should_work() {
assert_eq!(Tokens::accounts(BRUCE, vsBond).reserved, 0);

assert_ok!(Salp::redeem(Some(CATHI).into(), 3_000, 1, SlotLength::get(), 50));
assert_ok!(Salp::confirm_redeem(Origin::root(), CATHI, 3_000, 1, SlotLength::get(), true));
assert_ok!(Salp::confirm_redeem(
Some(ALICE).into(),
CATHI,
3_000,
1,
SlotLength::get(),
true
));

assert_eq!(Salp::redeem_pool(), 0);

Expand Down Expand Up @@ -1127,7 +1132,14 @@ fn redeem_when_xcm_error_should_work() {
let (vsToken, vsBond) = Salp::vsAssets(3_000, 1, SlotLength::get());

assert_ok!(Salp::redeem(Some(BRUCE).into(), 3_000, 1, SlotLength::get(), 50));
assert_ok!(Salp::confirm_redeem(Origin::root(), BRUCE, 3_000, 1, SlotLength::get(), false));
assert_ok!(Salp::confirm_redeem(
Some(ALICE).into(),
BRUCE,
3_000,
1,
SlotLength::get(),
false
));

assert_eq!(Salp::redeem_pool(), 100);

Expand Down Expand Up @@ -1181,7 +1193,7 @@ fn confirm_redeem_when_not_in_redeeming_should_fail() {
assert_ok!(Salp::confirm_withdraw(Some(ALICE).into(), 3_000, true));

assert_noop!(
Salp::confirm_redeem(Origin::root(), BRUCE, 3_000, 1, SlotLength::get(), true),
Salp::confirm_redeem(Some(ALICE).into(), BRUCE, 3_000, 1, SlotLength::get(), true),
Error::<Test>::InvalidRedeemStatus
);
});
Expand Down Expand Up @@ -1215,11 +1227,7 @@ fn redeem_with_wrong_origin_should_fail() {

assert_noop!(
Salp::confirm_redeem(Origin::none(), BRUCE, 3_000, 1, SlotLength::get(), true),
Error::<Test>::UnauthorizedAccount
);
assert_noop!(
Salp::confirm_redeem(Some(ALICE).into(), BRUCE, 3_000, 1, SlotLength::get(), true),
Error::<Test>::UnauthorizedAccount
DispatchError::BadOrigin
);
});
}
Expand Down
Loading