Skip to content
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

Feature/plmc 240 implement automatic funding payout to issuer for bids #66

Merged
Show file tree
Hide file tree
Changes from all 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
47 changes: 42 additions & 5 deletions pallets/funding/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ impl<T: Config> Pallet<T> {
project_id,
project_details,
SuccessReason::ReachedTarget,
1u32.into(),
T::SuccessToSettlementTime::get(),
)
}
}
Expand Down Expand Up @@ -1517,11 +1517,48 @@ impl<T: Config> Pallet<T> {
}

pub fn do_payout_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::FundingSuccessful &&
matches!(bid.status, BidStatus::Accepted | BidStatus::PartiallyAccepted(..)),
Error::<T>::NotAllowed
);

// * Calculate variables *
let issuer = project_details.issuer;
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,
&issuer,
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>::BidFundingPaidOut {
project_id,
bidder: bidder.clone(),
id: bid_id,
amount: payout_amount,
caller,
});

Ok(())
}
}
Expand Down
11 changes: 2 additions & 9 deletions pallets/funding/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ fn issuer_funding_payout_one_bid<T: Config>(project_id: T::ProjectIdentifier) ->

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_payout_bid_funds_for(
T::PalletId::get().into_account_truncating(),
bid.project_id,
Expand All @@ -615,14 +615,7 @@ fn issuer_funding_payout_one_bid<T: Config>(project_id: T::ProjectIdentifier) ->
error: e,
}),
};

bid.funds_released = true;

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

// (Weight::zero(), remaining_bids.count() as u64)
// TODO: Remove this when function is implemented
(Weight::zero(), 0u64)
(Weight::zero(), remaining_bids.count() as u64)
} else {
(Weight::zero(), 0u64)
}
Expand Down
7 changes: 7 additions & 0 deletions pallets/funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,13 @@ pub mod pallet {
amount: BalanceOf<T>,
caller: AccountIdOf<T>,
},
BidFundingPaidOut {
project_id: ProjectIdOf<T>,
bidder: AccountIdOf<T>,
id: StorageItemIdOf<T>,
amount: BalanceOf<T>,
caller: AccountIdOf<T>,
},
}

#[pallet::error]
Expand Down
Loading