Skip to content

Commit

Permalink
fix(243): rebase error (#72)
Browse files Browse the repository at this point in the history
chore(243): add import

feat(245): manual and automatic release of PLMC and funding assets implemented and tested

feat(247): manual and automatic release of PLMC and funding assets implemented and tested

feat(247): manual release implemented and tested

feat(247): automatic release implemented and tested

feat(247): automatic release implemented and tested

feat(248): manual release implemented and tested

feat(248): automatic release implemented and tested
  • Loading branch information
JuaniRios authored Aug 25, 2023
1 parent bdc9075 commit e576cdc
Show file tree
Hide file tree
Showing 6 changed files with 1,954 additions and 321 deletions.
150 changes: 134 additions & 16 deletions pallets/funding/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1473,38 +1473,156 @@ impl<T: Config> Pallet<T> {
}

pub fn do_release_bid_funds_for(
_caller: AccountIdOf<T>,
_project_id: T::ProjectIdentifier,
_bidder: AccountIdOf<T>,
_bid_id: StorageItemIdOf<T>,
caller: AccountIdOf<T>,
project_id: T::ProjectIdentifier,
bidder: AccountIdOf<T>,
bid_id: StorageItemIdOf<T>,
) -> Result<(), DispatchError> {
// * Get variables *
let project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectInfoNotFound)?;
let mut bid = Bids::<T>::get((project_id, bidder.clone(), bid_id)).ok_or(Error::<T>::BidNotFound)?;

// * Validity checks *
ensure!(
project_details.status == ProjectStatus::FundingFailed &&
matches!(bid.status, BidStatus::Accepted | BidStatus::PartiallyAccepted(..)),
Error::<T>::NotAllowed
);

// * Calculate variables *
let project_pot = Self::fund_account_id(project_id);
let payout_amount = bid.funding_asset_amount_locked;
let payout_asset = bid.funding_asset;

// * Update storage *
T::FundingCurrency::transfer(
payout_asset.to_statemint_id(),
&project_pot,
&bidder,
payout_amount,
Preservation::Expendable,
)?;
bid.funds_released = true;
Bids::<T>::insert((project_id, bidder.clone(), bid_id), bid);

// * Emit events *
Self::deposit_event(Event::<T>::BidFundingReleased {
project_id,
bidder: bidder.clone(),
id: bid_id,
amount: payout_amount,
caller,
});

Ok(())
}

pub fn do_bid_unbond_for(
_caller: AccountIdOf<T>,
_project_id: T::ProjectIdentifier,
_bidder: AccountIdOf<T>,
_bid_id: StorageItemIdOf<T>,
caller: AccountIdOf<T>,
project_id: T::ProjectIdentifier,
bidder: AccountIdOf<T>,
bid_id: StorageItemIdOf<T>,
) -> Result<(), DispatchError> {
// * Get variables *
let project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectInfoNotFound)?;
let bid = Bids::<T>::get((project_id, bidder.clone(), bid_id)).ok_or(Error::<T>::EvaluationNotFound)?;

// * Validity checks *
ensure!(
project_details.status == ProjectStatus::FundingFailed &&
matches!(bid.status, BidStatus::Accepted | BidStatus::PartiallyAccepted(..)) &&
bid.funds_released,
Error::<T>::NotAllowed
);

// * Update Storage *
T::NativeCurrency::release(&LockType::Participation(project_id), &bidder, bid.plmc_bond, Precision::Exact)?;
Bids::<T>::remove((project_id, bidder.clone(), bid_id));

// * Emit events *
Self::deposit_event(Event::<T>::BondReleased {
project_id,
amount: bid.plmc_bond,
bonder: bidder,
releaser: caller,
});

Ok(())
}

pub fn do_release_contribution_funds_for(
_caller: AccountIdOf<T>,
_project_id: T::ProjectIdentifier,
_contributor: AccountIdOf<T>,
_contribution_id: StorageItemIdOf<T>,
caller: AccountIdOf<T>,
project_id: T::ProjectIdentifier,
contributor: AccountIdOf<T>,
contribution_id: StorageItemIdOf<T>,
) -> Result<(), DispatchError> {
// * Get variables *
let project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectInfoNotFound)?;
let mut contribution = Contributions::<T>::get((project_id, contributor.clone(), contribution_id))
.ok_or(Error::<T>::ContributionNotFound)?;

// * Validity checks *
ensure!(project_details.status == ProjectStatus::FundingFailed, Error::<T>::NotAllowed);

// * Calculate variables *
let project_pot = Self::fund_account_id(project_id);
let payout_amount = contribution.funding_asset_amount;
let payout_asset = contribution.funding_asset;

// * Update storage *
T::FundingCurrency::transfer(
payout_asset.to_statemint_id(),
&project_pot,
&contributor,
payout_amount,
Preservation::Expendable,
)?;
contribution.funds_released = true;
Contributions::<T>::insert((project_id, contributor.clone(), contribution_id), contribution);

// * Emit events *
Self::deposit_event(Event::<T>::ContributionFundingReleased {
project_id,
contributor: contributor.clone(),
id: contribution_id,
amount: payout_amount,
caller,
});

Ok(())
}

pub fn do_contribution_unbond_for(
_caller: AccountIdOf<T>,
_project_id: T::ProjectIdentifier,
_contributor: AccountIdOf<T>,
_contribution_id: StorageItemIdOf<T>,
caller: AccountIdOf<T>,
project_id: T::ProjectIdentifier,
contributor: AccountIdOf<T>,
contribution_id: StorageItemIdOf<T>,
) -> Result<(), DispatchError> {
// * Get variables *
let project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectInfoNotFound)?;
let bid = Contributions::<T>::get((project_id, contributor.clone(), contribution_id))
.ok_or(Error::<T>::EvaluationNotFound)?;

// * Validity checks *
ensure!(project_details.status == ProjectStatus::FundingFailed, Error::<T>::NotAllowed);

// * Update Storage *
T::NativeCurrency::release(
&LockType::Participation(project_id),
&contributor,
bid.plmc_bond,
Precision::Exact,
)?;
Contributions::<T>::remove((project_id, contributor.clone(), contribution_id));

// * Emit events *
Self::deposit_event(Event::<T>::BondReleased {
project_id,
amount: bid.plmc_bond,
bonder: contributor,
releaser: caller,
});

Ok(())
}

Expand Down
26 changes: 6 additions & 20 deletions pallets/funding/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ fn release_funds_one_bid<T: Config>(project_id: T::ProjectIdentifier) -> (Weight
let project_bids = Bids::<T>::iter_prefix_values((project_id,));
let mut remaining_bids = project_bids.filter(|bid| !bid.funds_released);

if let Some(mut bid) = remaining_bids.next() {
if let Some(bid) = remaining_bids.next() {
match Pallet::<T>::do_release_bid_funds_for(
T::PalletId::get().into_account_truncating(),
bid.project_id,
Expand All @@ -387,12 +387,7 @@ fn release_funds_one_bid<T: Config>(project_id: T::ProjectIdentifier) -> (Weight
}),
};

bid.funds_released = true;
Bids::<T>::insert((project_id, bid.bidder.clone(), bid.id), bid);

// (Weight::zero(), remaining_bids.count() as u64)
// TODO: delete this when function is implemented
(Weight::zero(), 0u64)
(Weight::zero(), remaining_bids.count() as u64)
} else {
(Weight::zero(), 0u64)
}
Expand All @@ -417,9 +412,7 @@ fn unbond_one_bid<T: Config>(project_id: T::ProjectIdentifier) -> (Weight, u64)
error: e,
}),
};
// (Weight::zero(), remaining_bids.count() as u64)
// TODO: Remove this below when function is implemented
(Weight::zero(), 0u64)
(Weight::zero(), remaining_bids.count() as u64)
} else {
(Weight::zero(), 0u64)
}
Expand All @@ -429,7 +422,7 @@ fn release_funds_one_contribution<T: Config>(project_id: T::ProjectIdentifier) -
let project_contributions = Contributions::<T>::iter_prefix_values((project_id,));
let mut remaining_contributions = project_contributions.filter(|contribution| !contribution.funds_released);

if let Some(mut contribution) = remaining_contributions.next() {
if let Some(contribution) = remaining_contributions.next() {
match Pallet::<T>::do_release_contribution_funds_for(
T::PalletId::get().into_account_truncating(),
contribution.project_id,
Expand All @@ -445,12 +438,7 @@ fn release_funds_one_contribution<T: Config>(project_id: T::ProjectIdentifier) -
}),
};

contribution.funds_released = true;

Contributions::<T>::insert((project_id, contribution.contributor.clone(), contribution.id), contribution);
// (Weight::zero(), remaining_contributions.count() as u64)
// TODO: Remove this when function is implemented
(Weight::zero(), 0u64)
(Weight::zero(), remaining_contributions.count() as u64)
} else {
(Weight::zero(), 0u64)
}
Expand All @@ -477,9 +465,7 @@ fn unbond_one_contribution<T: Config>(project_id: T::ProjectIdentifier) -> (Weig
error: e,
}),
};
// (Weight::zero(), (project_contributions.len() as u64).saturating_sub(One::one()))
// TODO: Remove this when function is implemented
(Weight::zero(), 0u64)
(Weight::zero(), remaining_contributions.count() as u64)
} else {
(Weight::zero(), 0u64)
}
Expand Down
87 changes: 86 additions & 1 deletion pallets/funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,12 @@ pub mod pallet {
// TODO: PLMC-153 + MaybeSerializeDeserialize: Maybe needed for JSON serialization @ Genesis: https://github.com/paritytech/substrate/issues/12738#issuecomment-1320921201

/// Multiplier that decides how much PLMC needs to be bonded for a token buy/bid
type Multiplier: Parameter + BondingRequirementCalculation + VestingDurationCalculation + Default + Copy;
type Multiplier: Parameter
+ BondingRequirementCalculation
+ VestingDurationCalculation
+ Default
+ Copy
+ TryFrom<u8>;

/// The inner balance type we will use for all of our outer currency types. (e.g native, funding, CTs)
type Balance: Balance + From<u64> + FixedPointOperand;
Expand Down Expand Up @@ -677,6 +682,20 @@ pub mod pallet {
amount: BalanceOf<T>,
caller: AccountIdOf<T>,
},
BidFundingReleased {
project_id: ProjectIdOf<T>,
bidder: AccountIdOf<T>,
id: StorageItemIdOf<T>,
amount: BalanceOf<T>,
caller: AccountIdOf<T>,
},
ContributionFundingReleased {
project_id: ProjectIdOf<T>,
contributor: AccountIdOf<T>,
id: StorageItemIdOf<T>,
amount: BalanceOf<T>,
caller: AccountIdOf<T>,
},
}

#[pallet::error]
Expand Down Expand Up @@ -798,6 +817,7 @@ pub mod pallet {
FinalizerFinished,
/// Tried to do an operation on a cleaner that is not ready
CleanerNotReady,
ContributionNotFound,
}

#[pallet::call]
Expand Down Expand Up @@ -886,6 +906,17 @@ pub mod pallet {
Self::do_evaluation_unbond_for(releaser, project_id, evaluator, bond_id)
}

#[pallet::weight(Weight::from_parts(0, 0))]
pub fn evaluation_slash_for(
origin: OriginFor<T>,
project_id: T::ProjectIdentifier,
evaluator: AccountIdOf<T>,
bond_id: T::StorageItemId,
) -> DispatchResult {
let caller = ensure_signed(origin)?;
Self::do_evaluation_slash_for(caller, project_id, evaluator, bond_id)
}

#[pallet::weight(Weight::from_parts(0, 0))]
pub fn evaluation_reward_payout_for(
origin: OriginFor<T>,
Expand Down Expand Up @@ -962,6 +993,60 @@ pub mod pallet {
let caller = ensure_signed(origin)?;
Self::do_payout_contribution_funds_for(caller, project_id, contributor, contribution_id)
}

#[pallet::weight(Weight::from_parts(0, 0))]
pub fn decide_project_outcome(
origin: OriginFor<T>,
project_id: T::ProjectIdentifier,
outcome: FundingOutcomeDecision,
) -> DispatchResult {
let caller = ensure_signed(origin)?;
Self::do_decide_project_outcome(caller, project_id, outcome)
}

#[pallet::weight(Weight::from_parts(0, 0))]
pub fn release_bid_funds_for(
origin: OriginFor<T>,
project_id: T::ProjectIdentifier,
bidder: AccountIdOf<T>,
bid_id: T::StorageItemId,
) -> DispatchResult {
let caller = ensure_signed(origin)?;
Self::do_release_bid_funds_for(caller, project_id, bidder, bid_id)
}

#[pallet::weight(Weight::from_parts(0, 0))]
pub fn bid_unbond_for(
origin: OriginFor<T>,
project_id: T::ProjectIdentifier,
bidder: AccountIdOf<T>,
bid_id: T::StorageItemId,
) -> DispatchResult {
let caller = ensure_signed(origin)?;
Self::do_bid_unbond_for(caller, project_id, bidder, bid_id)
}

#[pallet::weight(Weight::from_parts(0, 0))]
pub fn release_contribution_funds_for(
origin: OriginFor<T>,
project_id: T::ProjectIdentifier,
contributor: AccountIdOf<T>,
contribution_id: T::StorageItemId,
) -> DispatchResult {
let caller = ensure_signed(origin)?;
Self::do_release_contribution_funds_for(caller, project_id, contributor, contribution_id)
}

#[pallet::weight(Weight::from_parts(0, 0))]
pub fn contribution_unbond_for(
origin: OriginFor<T>,
project_id: T::ProjectIdentifier,
contributor: AccountIdOf<T>,
contribution_id: T::StorageItemId,
) -> DispatchResult {
let caller = ensure_signed(origin)?;
Self::do_contribution_unbond_for(caller, project_id, contributor, contribution_id)
}
}

#[pallet::hooks]
Expand Down
Loading

0 comments on commit e576cdc

Please sign in to comment.