Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
82 changes: 78 additions & 4 deletions pallets/salp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,10 @@ pub mod pallet {
ContributeFailed(AccountIdOf<T>, ParaId, BalanceOf<T>, MessageId),
/// Withdrew full balance of a contributor. [who, fund_index, amount]
Withdrew(ParaId, BalanceOf<T>),
/// redeem to account. [who, fund_index,value]
/// refund to account. [who, fund_index,value]
Refunded(AccountIdOf<T>, ParaId, BalanceOf<T>),
/// all refund
AllRefunded(ParaId),
/// redeem to account. [who, fund_index, first_slot, last_slot, value]
Redeemed(AccountIdOf<T>, ParaId, LeasePeriod, LeasePeriod, BalanceOf<T>),
/// Fund is edited. [fund_index]
Expand Down Expand Up @@ -770,7 +772,7 @@ pub mod pallet {
Self::put_contribution(
fund.trie_index,
&who,
contributed,
Zero::zero(),
ContributionStatus::Refunded,
);

Expand All @@ -779,6 +781,63 @@ pub mod pallet {
Ok(())
}

#[pallet::weight(T::WeightInfo::refund())]
#[transactional]
pub fn batch_refund(
origin: OriginFor<T>,
#[pallet::compact] index: ParaId,
) -> DispatchResult {
ensure_signed(origin)?;

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

in batch_refund function, the fund.status is required to be FundStatus::Failed, while in the refund function, the fund status is required to be FundStatus::RefundWithdrew. Is there any difference between these two functions in regard to their execution contexts?


let mut refund_count = 0u32;
let contributions = Self::contribution_iterator(fund.trie_index);
// Assume everyone will be refunded.
let mut all_refunded = true;

for (who, (contributed, status)) in contributions {
if refund_count >= T::RemoveKeysLimit::get() {
// Not everyone was able to be refunded this time around.
all_refunded = false;
break;
}
if status != ContributionStatus::Refunded {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we need to make sure all the contributions which need to be refunded have a status of Idle, like what we do in the refund function? What if one of the contriubtions is in the contriubiting status?

#[allow(non_snake_case)]
let (vsToken, vsBond) = Self::vsAssets(index, fund.first_slot, fund.last_slot);
fund.raised = fund.raised.saturating_sub(contributed);

let balance = T::MultiCurrency::slash_reserved(vsToken, &who, contributed);
ensure!(balance == Zero::zero(), Error::<T>::NotEnoughReservedAssetsToRefund);
let balance = T::MultiCurrency::slash_reserved(vsBond, &who, contributed);
ensure!(balance == Zero::zero(), Error::<T>::NotEnoughReservedAssetsToRefund);

if T::TransactType::get() == ParachainTransactType::Xcm {
T::MultiCurrency::transfer(
T::RelayChainToken::get(),
&Self::fund_account_id(index),
&who,
contributed,
)?;
}
Self::put_contribution(
fund.trie_index,
&who,
Zero::zero(),
ContributionStatus::Refunded,
);
refund_count += 1;
}
}

if all_refunded {
Self::deposit_event(Event::<T>::AllRefunded(index));
}

Ok(())
}

#[pallet::weight(T::WeightInfo::redeem())]
#[transactional]
pub fn redeem(
Expand All @@ -791,10 +850,18 @@ pub mod pallet {
let mut fund = Self::funds(index).ok_or(Error::<T>::InvalidParaId)?;
ensure!(fund.status == FundStatus::RedeemWithdrew, Error::<T>::InvalidFundStatus);
ensure!(fund.raised >= value, Error::<T>::NotEnoughBalanceInRedeemPool);

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

ensure!(Self::redeem_pool() >= value, Error::<T>::NotEnoughBalanceInRedeemPool);
let cur_block = <frame_system::Pallet<T>>::block_number();
ensure!(!Self::is_expired(cur_block, fund.last_slot), Error::<T>::VSBondExpired);
ensure!(Self::can_redeem(cur_block, fund.last_slot), Error::<T>::UnRedeemableNow);

#[allow(non_snake_case)]
let (vsToken, vsBond) = Self::vsAssets(index, fund.first_slot, fund.last_slot);
Expand All @@ -817,7 +884,13 @@ pub mod pallet {
value,
)?;
}
Self::put_contribution(fund.trie_index, &who, value, ContributionStatus::Redeemed);
let contributed_new = contributed.saturating_sub(value);
Self::put_contribution(
fund.trie_index,
&who,
contributed_new,
ContributionStatus::Redeemed,
);
Self::deposit_event(Event::Redeemed(
who,
index,
Expand Down Expand Up @@ -957,6 +1030,7 @@ pub mod pallet {
}

/// Check if the vsBond is `in` the redeemable date
#[allow(dead_code)]
pub(crate) fn can_redeem(block: BlockNumberFor<T>, last_slot: LeasePeriod) -> bool {
let block_begin_redeem = Self::block_end_of_lease_period_index(last_slot);
let block_end_redeem = block_begin_redeem.saturating_add(T::VSBondValidPeriod::get());
Expand Down
9 changes: 3 additions & 6 deletions pallets/salp/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ fn refund_should_work() {

let fund = Salp::funds(3_000).unwrap();
let (contributed, status) = Salp::contribution(fund.trie_index, &BRUCE);
assert_eq!(contributed, 100);
assert_eq!(contributed, 0);
assert_eq!(status, ContributionStatus::Refunded);

#[allow(non_snake_case)]
Expand Down Expand Up @@ -786,7 +786,7 @@ fn refund_when_xcm_error_should_work() {

let fund = Salp::funds(3_000).unwrap();
let (contributed, status) = Salp::contribution(fund.trie_index, &BRUCE);
assert_eq!(contributed, 100);
assert_eq!(contributed, 0);
assert_eq!(status, ContributionStatus::Refunded);

#[allow(non_snake_case)]
Expand Down Expand Up @@ -815,10 +815,7 @@ fn double_refund_when_one_of_xcm_error_should_work() {
assert_ok!(Salp::fund_fail(Some(ALICE).into(), 3_000));
assert_ok!(Salp::withdraw(Some(ALICE).into(), 3_000));
assert_ok!(Salp::refund(Some(BRUCE).into(), 3_000));
assert_noop!(
Salp::refund(Some(BRUCE).into(), 3_000),
Error::<Test>::InvalidContributionStatus
);
assert_noop!(Salp::refund(Some(BRUCE).into(), 3_000), Error::<Test>::ZeroContribution);
});
}

Expand Down