Skip to content

Commit

Permalink
✅ Comprehensive funding end tests (#303)
Browse files Browse the repository at this point in the history
## What?

## Why?

## How?

## Testing?

## Screenshots (optional)

## Anything Else?
  • Loading branch information
JuaniRios authored Jun 3, 2024
1 parent 31bbba5 commit 079940c
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 86 deletions.
24 changes: 24 additions & 0 deletions pallets/funding/src/functions/5_funding_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,28 @@ impl<T: Config> Pallet<T> {
pays_fee: Pays::Yes,
})
}

pub fn finalize_funding(
project_id: ProjectId,
mut project_details: ProjectDetailsOf<T>,
outcome: ProjectOutcome,
settlement_delta: BlockNumberFor<T>,
) -> Result<u32, DispatchError> {
let now = <frame_system::Pallet<T>>::block_number();

project_details.status = match outcome {
ProjectOutcome::FundingSuccessful | ProjectOutcome::FundingAccepted => ProjectStatus::FundingSuccessful,
_ => ProjectStatus::FundingFailed,
};
ProjectsDetails::<T>::insert(project_id, project_details);

let insertion_iterations =
Self::add_to_update_store(now + settlement_delta, (&project_id, UpdateType::StartSettlement))
.map_err(|_| Error::<T>::TooManyInsertionAttempts)?;
Self::deposit_event(Event::ProjectPhaseTransition {
project_id,
phase: ProjectPhases::FundingFinalization(outcome),
});
Ok(insertion_iterations)
}
}
30 changes: 2 additions & 28 deletions pallets/funding/src/functions/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ impl<T: Config> Pallet<T> {
}

/// Computes the total fee from all defined fee brackets.
fn compute_total_fee_from_brackets(funding_reached: BalanceOf<T>) -> BalanceOf<T> {
pub fn compute_total_fee_from_brackets(funding_reached: BalanceOf<T>) -> BalanceOf<T> {
let mut remaining_for_fee = funding_reached;

T::FeeBrackets::get()
Expand All @@ -413,7 +413,7 @@ impl<T: Config> Pallet<T> {
}

/// Calculate the fee for a particular bracket.
fn compute_fee_for_bracket(
pub fn compute_fee_for_bracket(
remaining_for_fee: &mut BalanceOf<T>,
fee: Percent,
limit: BalanceOf<T>,
Expand Down Expand Up @@ -533,32 +533,6 @@ impl<T: Config> Pallet<T> {
Ok((liquidity_pools_reward_pot, long_term_holder_reward_pot))
}

pub fn finalize_funding(
project_id: ProjectId,
mut project_details: ProjectDetailsOf<T>,
outcome: ProjectOutcome,
settlement_delta: BlockNumberFor<T>,
) -> Result<u32, DispatchError> {
let now = <frame_system::Pallet<T>>::block_number();

project_details.status = match outcome {
ProjectOutcome::FundingSuccessful | ProjectOutcome::FundingAccepted => ProjectStatus::FundingSuccessful,
_ => ProjectStatus::FundingFailed,
};
ProjectsDetails::<T>::insert(project_id, project_details);

let insertion_iterations =
match Self::add_to_update_store(now + settlement_delta, (&project_id, UpdateType::StartSettlement)) {
Ok(iterations) => iterations,
Err(_iterations) => return Err(Error::<T>::TooManyInsertionAttempts.into()),
};
Self::deposit_event(Event::ProjectPhaseTransition {
project_id,
phase: ProjectPhases::FundingFinalization(outcome),
});
Ok(insertion_iterations)
}

pub fn migrations_per_xcm_message_allowed() -> u32 {
const MAX_WEIGHT: Weight = Weight::from_parts(20_000_000_000, 1_000_000);

Expand Down
2 changes: 1 addition & 1 deletion pallets/funding/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ parameter_types! {
pub const FundingPalletId: PalletId = PalletId(*b"py/cfund");
pub FeeBrackets: Vec<(Percent, Balance)> = vec![
(Percent::from_percent(10), 1_000_000 * USD_UNIT),
(Percent::from_percent(8), 5_000_000 * USD_UNIT),
(Percent::from_percent(8), 4_000_000 * USD_UNIT),
(Percent::from_percent(6), u128::MAX), // Making it max signifies the last bracket
];
pub EarlyEvaluationThreshold: Percent = Percent::from_percent(10);
Expand Down
2 changes: 1 addition & 1 deletion pallets/funding/src/tests/5_remainder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ mod remaining_contribute_extrinsic {
inst.bid_for_users(project_id, successful_bids).unwrap();
inst.advance_time(
<TestRuntime as Config>::AuctionOpeningDuration::get() +
<TestRuntime as Config>::AuctionClosingDuration::get()
<TestRuntime as Config>::AuctionClosingDuration::get(),
)
.unwrap();
inst.bid_for_users(project_id, failing_bids_after_random_end).unwrap();
Expand Down
Loading

0 comments on commit 079940c

Please sign in to comment.