-
Notifications
You must be signed in to change notification settings - Fork 95
Salp improve #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Salp improve #305
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b579963
Remove can_redeem check&Fix contribution storage
yrong fcdd9ba
Support batch refund
yrong b3c8740
Fix xcm test
yrong 3c5ca2f
Merge branch 'develop' of https://github.com/bifrost-finance/bifrost …
yrong 0a7489c
Fix per review comments
yrong be69da7
Merge branch 'develop' into salp-improve
yrong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -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] | ||
|
|
@@ -770,7 +772,7 @@ pub mod pallet { | |
| Self::put_contribution( | ||
| fund.trie_index, | ||
| &who, | ||
| contributed, | ||
| Zero::zero(), | ||
| ContributionStatus::Refunded, | ||
| ); | ||
|
|
||
|
|
@@ -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); | ||
|
|
||
| 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
|
@@ -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); | ||
|
|
@@ -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, | ||
|
|
@@ -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()); | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?