Skip to content

Commit 4b5be6c

Browse files
authored
Merge pull request #2138 from opentensor/fix-crowdloan-refund
Only allow the crowdloan creator to refund
2 parents bf92ee4 + 919121b commit 4b5be6c

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ pub mod pallet {
12521252
/// The extrinsic will call the Subtensor pallet to set the minimum delegate take.
12531253
#[pallet::call_index(46)]
12541254
#[pallet::weight((
1255-
Weight::from_parts(5_000_000, 0).saturating_add(T::DbWeight::get().writes(1_u64)),
1255+
Weight::from_parts(7_885_000, 0).saturating_add(T::DbWeight::get().writes(1_u64)),
12561256
DispatchClass::Operational,
12571257
Pays::Yes
12581258
))]
@@ -1999,7 +1999,7 @@ pub mod pallet {
19991999
/// Only callable by root.
20002000
#[pallet::call_index(74)]
20012001
#[pallet::weight((
2002-
Weight::from_parts(5_771_000, 0)
2002+
Weight::from_parts(9_418_000, 0)
20032003
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(0_u64))
20042004
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1_u64)),
20052005
DispatchClass::Operational

pallets/crowdloan/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,16 @@ pub mod pallet {
638638
origin: OriginFor<T>,
639639
#[pallet::compact] crowdloan_id: CrowdloanId,
640640
) -> DispatchResultWithPostInfo {
641-
ensure_signed(origin)?;
641+
let who = ensure_signed(origin)?;
642642

643643
let mut crowdloan = Self::ensure_crowdloan_exists(crowdloan_id)?;
644644

645645
// Ensure the crowdloan is not finalized
646646
ensure!(!crowdloan.finalized, Error::<T>::AlreadyFinalized);
647647

648+
// Only the creator can refund the crowdloan
649+
ensure!(who == crowdloan.creator, Error::<T>::InvalidOrigin);
650+
648651
let mut refunded_contributors: Vec<T::AccountId> = vec![];
649652
let mut refund_count = 0;
650653

pallets/crowdloan/src/tests.rs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,20 +1618,47 @@ fn test_refund_succeeds() {
16181618
}
16191619

16201620
#[test]
1621-
fn test_refund_fails_if_bad_origin() {
1622-
TestState::default().build_and_execute(|| {
1623-
let crowdloan_id: CrowdloanId = 0;
1621+
fn test_refund_fails_if_bad_or_invalid_origin() {
1622+
TestState::default()
1623+
.with_balance(U256::from(1), 100)
1624+
.build_and_execute(|| {
1625+
// create a crowdloan
1626+
let crowdloan_id: CrowdloanId = 0;
1627+
let creator: AccountOf<Test> = U256::from(1);
1628+
let initial_deposit: BalanceOf<Test> = 50;
1629+
let min_contribution: BalanceOf<Test> = 10;
1630+
let cap: BalanceOf<Test> = 300;
1631+
let end: BlockNumberFor<Test> = 50;
1632+
assert_ok!(Crowdloan::create(
1633+
RuntimeOrigin::signed(creator),
1634+
initial_deposit,
1635+
min_contribution,
1636+
cap,
1637+
end,
1638+
Some(noop_call()),
1639+
None,
1640+
));
16241641

1625-
assert_err!(
1626-
Crowdloan::refund(RuntimeOrigin::none(), crowdloan_id),
1627-
DispatchError::BadOrigin
1628-
);
1642+
assert_err!(
1643+
Crowdloan::refund(RuntimeOrigin::none(), crowdloan_id),
1644+
DispatchError::BadOrigin
1645+
);
16291646

1630-
assert_err!(
1631-
Crowdloan::refund(RuntimeOrigin::root(), crowdloan_id),
1632-
DispatchError::BadOrigin
1633-
);
1634-
});
1647+
assert_err!(
1648+
Crowdloan::refund(RuntimeOrigin::root(), crowdloan_id),
1649+
DispatchError::BadOrigin
1650+
);
1651+
1652+
// run some blocks
1653+
run_to_block(60);
1654+
1655+
// try to refund
1656+
let unknown_contributor: AccountOf<Test> = U256::from(2);
1657+
assert_err!(
1658+
Crowdloan::refund(RuntimeOrigin::signed(unknown_contributor), crowdloan_id),
1659+
pallet_crowdloan::Error::<Test>::InvalidOrigin,
1660+
);
1661+
});
16351662
}
16361663

16371664
#[test]

0 commit comments

Comments
 (0)