Skip to content

Commit

Permalink
Add edit fund interface & Some fix (#457)
Browse files Browse the repository at this point in the history
* Add edit fund interface & Some fix

* Remove unused codes
  • Loading branch information
yrong authored Dec 27, 2021
1 parent 1fe2659 commit 63ac67f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
40 changes: 40 additions & 0 deletions pallets/salp-lite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,46 @@ pub mod pallet {

Ok(())
}

/// Edit the configuration for an in-progress crowdloan.
///
/// Can only be called by Root origin.
#[pallet::weight((
0,
DispatchClass::Normal,
Pays::No
))]
pub fn edit(
origin: OriginFor<T>,
#[pallet::compact] index: ParaId,
#[pallet::compact] cap: BalanceOf<T>,
#[pallet::compact] raised: BalanceOf<T>,
#[pallet::compact] first_slot: LeasePeriod,
#[pallet::compact] last_slot: LeasePeriod,
fund_status: Option<FundStatus>,
) -> DispatchResult {
T::EnsureConfirmAsGovernance::ensure_origin(origin)?;

let fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;

let status = match fund_status {
None => fund.status,
Some(status) => status,
};
Funds::<T>::insert(
index,
Some(FundInfo {
cap,
first_slot,
last_slot,
status,
raised,
trie_index: fund.trie_index,
}),
);
Self::deposit_event(Event::<T>::Edited(index));
Ok(())
}
}

#[pallet::hooks]
Expand Down
22 changes: 22 additions & 0 deletions pallets/salp-lite/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,3 +866,25 @@ fn refund_meanwhile_issue_should_work() {
assert_noop!(Salp::redeem(Some(BRUCE).into(), 3_000, 50), Error::<Test>::InvalidParaId);
});
}

#[test]
fn edit_fund_should_work() {
new_test_ext().execute_with(|| {
assert_ok!(Salp::create(Some(ALICE).into(), 3_000, 1_000, 1, SlotLength::get()));
assert_ok!(Salp::issue(Some(ALICE).into(), BRUCE, 3_000, 100, CONTRIBUTON_INDEX));
assert_ok!(Salp::fund_fail(Some(ALICE).into(), 3_000));

assert_ok!(Salp::edit(
Some(ALICE).into(),
3_000,
1_000,
150,
2,
SlotLength::get() + 1,
Some(FundStatus::Ongoing)
));
let fund = Salp::funds(3_000).unwrap();
assert_eq!(fund.raised, 150);
assert_eq!(fund.status, FundStatus::Ongoing);
});
}
8 changes: 7 additions & 1 deletion pallets/salp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ pub mod pallet {
origin: OriginFor<T>,
#[pallet::compact] index: ParaId,
#[pallet::compact] cap: BalanceOf<T>,
#[pallet::compact] raised: BalanceOf<T>,
#[pallet::compact] first_slot: LeasePeriod,
#[pallet::compact] last_slot: LeasePeriod,
fund_status: Option<FundStatus>,
Expand All @@ -484,7 +485,7 @@ pub mod pallet {
first_slot,
last_slot,
status,
raised: fund.raised,
raised,
trie_index: fund.trie_index,
}),
);
Expand Down Expand Up @@ -792,6 +793,8 @@ pub mod pallet {

fund.raised = fund.raised.saturating_sub(contributed);

Funds::<T>::insert(index, Some(fund.clone()));

T::MultiCurrency::slash_reserved(vsToken, &who, contributed);
T::MultiCurrency::slash_reserved(vsBond, &who, contributed);

Expand Down Expand Up @@ -853,6 +856,8 @@ pub mod pallet {
}
}

Funds::<T>::insert(index, Some(fund));

if all_refunded {
Self::deposit_event(Event::<T>::AllRefunded(index));
}
Expand Down Expand Up @@ -901,6 +906,7 @@ pub mod pallet {
}

fund.raised = fund.raised.saturating_sub(value);
Funds::<T>::insert(index, Some(fund.clone()));

if T::TransactType::get() == ParachainTransactType::Xcm {
T::MultiCurrency::transfer(
Expand Down
12 changes: 11 additions & 1 deletion pallets/salp/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,20 +1195,30 @@ fn edit_fund_should_work() {
CONTRIBUTON_INDEX
));
assert_ok!(Salp::fund_fail(Some(ALICE).into(), 3_000));
assert_ok!(Salp::edit(Some(ALICE).into(), 3_000, 1_000, 2, SlotLength::get() + 1, None));
assert_ok!(Salp::edit(
Some(ALICE).into(),
3_000,
1_000,
200,
2,
SlotLength::get() + 1,
None
));
let mut fund = Salp::funds(3_000).unwrap();
assert_eq!(fund.first_slot, 2);
assert_eq!(fund.status, FundStatus::Failed);
assert_ok!(Salp::edit(
Some(ALICE).into(),
3_000,
1_000,
200,
2,
SlotLength::get() + 1,
Some(FundStatus::Ongoing),
));
fund = Salp::funds(3_000).unwrap();
assert_eq!(fund.status, FundStatus::Ongoing);
assert_eq!(fund.raised, 200);
})
}

Expand Down

0 comments on commit 63ac67f

Please sign in to comment.