Skip to content

Commit

Permalink
Payout funding assets to destination account
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed May 13, 2024
1 parent bbd8b31 commit 472f3fe
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
6 changes: 3 additions & 3 deletions nodes/parachain/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ fn start_consensus(
collator_service,
// Very limited proposal time, since we are not allowing async-backing, yet.
authoring_duration: Duration::from_millis(500), // 500ms as the not async-backing one at the moment
// Added in polkadot-sdk-v1.7.0
// #[cfg(feature = "async-backing")]
// reinitialize: false,
// Added in polkadot-sdk-v1.7.0
// #[cfg(feature = "async-backing")]
// reinitialize: false,
};

let fut = aura::run::<Block, sp_consensus_aura::sr25519::AuthorityPair, _, _, _, _, _, _, _, _, _>(params);
Expand Down
49 changes: 49 additions & 0 deletions pallets/funding/src/instantiator/chain_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,55 @@ impl<
self.execute(|| Contributions::<T>::iter_prefix_values((project_id,)).collect())
}

// Used to check all the USDT/USDC/DOT was paid to the issuer funding account
pub fn assert_total_funding_paid_out(
&mut self,
project_id: ProjectId,
bids: Vec<BidInfoOf<T>>,
contributions: Vec<ContributionInfoOf<T>>,
) {
let project_metadata = self.get_project_metadata(project_id);
let mut total_expected_dot: BalanceOf<T> = Zero::zero();
let mut total_expected_usdt: BalanceOf<T> = Zero::zero();
let mut total_expected_usdc: BalanceOf<T> = Zero::zero();

for bid in bids {
match bid.funding_asset {
AcceptedFundingAsset::DOT => total_expected_dot += bid.funding_asset_amount_locked,
AcceptedFundingAsset::USDT => total_expected_usdt += bid.funding_asset_amount_locked,
AcceptedFundingAsset::USDC => total_expected_usdc += bid.funding_asset_amount_locked,
}
}

for contribution in contributions {
match contribution.funding_asset {
AcceptedFundingAsset::DOT => total_expected_dot += contribution.funding_asset_amount,
AcceptedFundingAsset::USDT => total_expected_usdt += contribution.funding_asset_amount,
AcceptedFundingAsset::USDC => total_expected_usdc += contribution.funding_asset_amount,
}
}

let total_stored_dot = self.get_free_foreign_asset_balances_for(
AcceptedFundingAsset::DOT.to_assethub_id(),
vec![project_metadata.funding_destination_account.clone()],
)[0]
.asset_amount;
let total_stored_usdt = self.get_free_foreign_asset_balances_for(
AcceptedFundingAsset::USDT.to_assethub_id(),
vec![project_metadata.funding_destination_account.clone()],
)[0]
.asset_amount;
let total_stored_usdc = self.get_free_foreign_asset_balances_for(
AcceptedFundingAsset::USDC.to_assethub_id(),
vec![project_metadata.funding_destination_account.clone()],
)[0]
.asset_amount;

assert_eq!(total_expected_dot, total_stored_dot, "DOT amount is incorrect");
assert_eq!(total_expected_usdt, total_stored_usdt, "USDT amount is incorrect");
assert_eq!(total_expected_usdc, total_stored_usdc, "USDC amount is incorrect");
}

// Used to check if all evaluations are settled correctly. We cannot check amount of
// contributions minted for the user, as they could have received more tokens from other participations.
pub fn assert_evaluations_migrations_created(
Expand Down
6 changes: 4 additions & 2 deletions pallets/funding/src/settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl<T: Config> Pallet<T> {
}

pub fn do_settle_successful_bid(bid: BidInfoOf<T>, project_id: ProjectId) -> DispatchResult {
let project_metadata = ProjectsMetadata::<T>::get(project_id).ok_or(Error::<T>::ProjectMetadataNotFound)?;
let project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectDetailsNotFound)?;

ensure!(project_details.status == ProjectStatus::FundingSuccessful, Error::<T>::IncorrectRound);
Expand Down Expand Up @@ -138,7 +139,7 @@ impl<T: Config> Pallet<T> {
// Payout the bid funding asset amount to the project account
Self::release_funding_asset(
project_id,
&project_details.issuer_account,
&project_metadata.funding_destination_account,
bid.funding_asset_amount_locked,
bid.funding_asset,
)?;
Expand Down Expand Up @@ -188,6 +189,7 @@ impl<T: Config> Pallet<T> {
contribution: ContributionInfoOf<T>,
project_id: ProjectId,
) -> DispatchResult {
let project_metadata = ProjectsMetadata::<T>::get(project_id).ok_or(Error::<T>::ProjectMetadataNotFound)?;
let project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectDetailsNotFound)?;
// Ensure that:
// 1. The project is in the FundingSuccessful state
Expand Down Expand Up @@ -221,7 +223,7 @@ impl<T: Config> Pallet<T> {
// Payout the bid funding asset amount to the project account
Self::release_funding_asset(
project_id,
&project_details.issuer_account,
&project_metadata.funding_destination_account,
contribution.funding_asset_amount,
contribution.funding_asset,
)?;
Expand Down
1 change: 1 addition & 0 deletions pallets/funding/src/tests/7_settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fn can_settle_accepted_project() {

inst.settle_project(project_id).unwrap();

inst.assert_total_funding_paid_out(project_id, bids.clone(), contributions.clone());
inst.assert_evaluations_migrations_created(project_id, evaluations, percentage);
inst.assert_bids_migrations_created(project_id, bids, true);
inst.assert_contributions_migrations_created(project_id, contributions, true);
Expand Down

0 comments on commit 472f3fe

Please sign in to comment.