Skip to content

Commit

Permalink
Remove unnecessary bid fields
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Jul 30, 2024
1 parent 0f61954 commit b612f1d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 29 deletions.
2 changes: 0 additions & 2 deletions pallets/funding/src/functions/3_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,6 @@ impl<T: Config> Pallet<T> {
status: BidStatus::YetUnknown,
original_ct_amount: ct_amount,
original_ct_usd_price: ct_usd_price,
final_ct_amount: ct_amount,
final_ct_usd_price: ct_usd_price,
funding_asset,
funding_asset_amount_locked,
multiplier,
Expand Down
29 changes: 21 additions & 8 deletions pallets/funding/src/functions/6_settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,16 @@ impl<T: Config> Pallet<T> {
ProjectStatus::SettlementStarted(FundingOutcome::Success) => true,
_ => false,
};
let wap = project_details.weighted_average_price.ok_or(Error::<T>::ImpossibleState)?;

ensure!(
matches!(project_details.status, ProjectStatus::SettlementStarted(..)) || bid.status == BidStatus::Rejected,
Error::<T>::SettlementNotStarted
);

// Return either the full amount to refund if bid is rejected/project failed, or a partial amount when the wap > paid price
let (refunded_plmc, refunded_funding_asset_amount) = Self::calculate_refund(&bid, funding_success)?;
let (final_ct_price, final_ct_amount, refunded_plmc, refunded_funding_asset_amount) =
Self::calculate_refund(&bid, funding_success, wap)?;

Self::release_participation_bond(project_id, &bid.bidder, refunded_plmc)?;
Self::release_funding_asset(project_id, &bid.bidder, refunded_funding_asset_amount, bid.funding_asset)?;
Expand All @@ -172,14 +175,14 @@ impl<T: Config> Pallet<T> {
HoldReason::Participation(project_id).into(),
)?;

Self::mint_contribution_tokens(project_id, &bid.bidder, bid.final_ct_amount)?;
Self::mint_contribution_tokens(project_id, &bid.bidder, final_ct_amount)?;

Self::create_migration(
project_id,
&bid.bidder,
bid.id,
ParticipationType::Bid,
bid.final_ct_amount,
final_ct_amount,
plmc_vesting_info.duration,
)?;

Expand All @@ -197,7 +200,8 @@ impl<T: Config> Pallet<T> {
project_id,
account: bid.bidder,
id: bid.id,
ct_amount: bid.final_ct_amount,
final_ct_amount,
final_ct_price
});

Ok(())
Expand All @@ -208,18 +212,27 @@ impl<T: Config> Pallet<T> {
fn calculate_refund(
bid: &BidInfoOf<T>,
funding_success: bool,
) -> Result<(BalanceOf<T>, BalanceOf<T>), DispatchError> {
wap: PriceOf<T>,
) -> Result<(PriceOf<T>, BalanceOf<T>, BalanceOf<T>, BalanceOf<T>), DispatchError> {
let final_ct_usd_price = if bid.original_ct_usd_price > wap { wap } else { bid.original_ct_usd_price };

if bid.status == BidStatus::Rejected || !funding_success {
return Ok((bid.plmc_bond, bid.funding_asset_amount_locked));
return Ok((final_ct_usd_price, Zero::zero(), bid.plmc_bond, bid.funding_asset_amount_locked));
}

let new_ticket_size = bid.final_ct_usd_price.checked_mul_int(bid.final_ct_amount).ok_or(Error::<T>::BadMath)?;
let final_ct_amount = match bid.status {
BidStatus::Accepted => bid.original_ct_amount,
BidStatus::PartiallyAccepted(amount) => amount,
_ => Zero::zero(),
};

let new_ticket_size = final_ct_usd_price.checked_mul_int(final_ct_amount).ok_or(Error::<T>::BadMath)?;
let new_plmc_bond = Self::calculate_plmc_bond(new_ticket_size, bid.multiplier)?;
let new_funding_asset_amount = Self::calculate_funding_asset_amount(new_ticket_size, bid.funding_asset)?;
let mut refund_plmc = bid.plmc_bond.saturating_sub(new_plmc_bond);
let mut refund_funding_asset = bid.funding_asset_amount_locked.saturating_sub(new_funding_asset_amount);

Ok((refund_plmc, refund_funding_asset))
Ok((final_ct_usd_price, final_ct_amount, refund_plmc, refund_funding_asset))
}

pub fn do_settle_contribution(contribution: ContributionInfoOf<T>, project_id: ProjectId) -> DispatchResult {
Expand Down
15 changes: 8 additions & 7 deletions pallets/funding/src/functions/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,14 @@ impl<T: Config> Pallet<T> {
let buyable_amount = auction_allocation_size.saturating_sub(bid_token_amount_sum);
if buyable_amount.is_zero() {
bid.status = BidStatus::Rejected;
bid.final_ct_amount = Zero::zero();
} else if bid.original_ct_amount <= buyable_amount {
if bid.final_ct_usd_price > wap {
bid.final_ct_usd_price = wap;
}
bid_token_amount_sum.saturating_accrue(bid.original_ct_amount);
bid.final_ct_amount = bid.original_ct_amount;
bid.status = BidStatus::Accepted;
DidWithWinningBids::<T>::mutate(project_id, bid.did.clone(), |flag| {
*flag = true;
});
} else {
bid_token_amount_sum.saturating_accrue(buyable_amount);
bid.final_ct_amount = buyable_amount;
bid.status = BidStatus::PartiallyAccepted(buyable_amount);
DidWithWinningBids::<T>::mutate(project_id, bid.did.clone(), |flag| {
*flag = true;
Expand All @@ -118,7 +112,14 @@ impl<T: Config> Pallet<T> {
let total_auction_allocation_usd: BalanceOf<T> = accepted_bids
.into_iter()
.try_fold(Zero::zero(), |acc: BalanceOf<T>, bid: BidInfoOf<T>| {
bid.final_ct_usd_price.checked_mul_int(bid.final_ct_amount).and_then(|ticket| acc.checked_add(&ticket))
let final_ct_usd_price = if bid.original_ct_usd_price > wap { wap } else { bid.original_ct_usd_price };
let final_ct_amount = match bid.status {
BidStatus::Accepted => bid.original_ct_amount,
BidStatus::PartiallyAccepted(amount) => amount,
_ => Zero::zero(),
};

final_ct_usd_price.checked_mul_int(final_ct_amount).and_then(|usd_ticket| acc.checked_add(&usd_ticket))
})
.ok_or(Error::<T>::BadMath)?;

Expand Down
11 changes: 8 additions & 3 deletions pallets/funding/src/instantiator/chain_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ impl<

self.execute(|| crate::Pallet::<T>::do_start_auction(caller, project_id).unwrap());

assert_eq!(self.get_project_details(project_id).status, ProjectStatus::Auction);
assert_eq!(self.get_project_details(project_id).status, ProjectStatus::AuctionRound);

Ok(())
}
Expand Down Expand Up @@ -818,7 +818,12 @@ impl<
for bid in bids {
let account = bid.bidder.clone();
assert_eq!(self.execute(|| { Bids::<T>::iter_prefix_values((&project_id, &account)).count() }), 0);
let amount: BalanceOf<T> = if is_successful { bid.final_ct_amount } else { 0u64.into() };
let amount: BalanceOf<T> = match bid.status {
BidStatus::Accepted => bid.original_ct_amount,
BidStatus::PartiallyAccepted(amount) => amount,
BidStatus::Rejected => 0u64.into(),
BidStatus::YetUnknown => {panic!("Bid should have a different status than YetUnknown")}
};
self.assert_migration(project_id, account, amount, bid.id, ParticipationType::Bid, is_successful);
}
}
Expand Down Expand Up @@ -1116,7 +1121,7 @@ impl<
),
ProjectStatus::CommunityRound(..) =>
self.create_community_contributing_project(project_metadata, issuer, None, evaluations, bids),
ProjectStatus::Auction => self.create_auctioning_project(project_metadata, issuer, None, evaluations),
ProjectStatus::AuctionRound => self.create_auctioning_project(project_metadata, issuer, None, evaluations),
ProjectStatus::EvaluationRound => self.create_evaluating_project(project_metadata, issuer, None),
ProjectStatus::Application => self.create_new_project(project_metadata, issuer, None),
_ => panic!("unsupported project creation in that status"),
Expand Down
6 changes: 0 additions & 6 deletions pallets/funding/src/instantiator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,6 @@ impl<T: Config> BidInfoFilter<T> {
if self.original_ct_usd_price.is_some() && self.original_ct_usd_price.unwrap() != bid.original_ct_usd_price {
return false;
}
if self.final_ct_amount.is_some() && self.final_ct_amount.unwrap() != bid.final_ct_amount {
return false;
}
if self.final_ct_usd_price.is_some() && self.final_ct_usd_price.unwrap() != bid.final_ct_usd_price {
return false;
}
if self.funding_asset.is_some() && self.funding_asset.unwrap() != bid.funding_asset {
return false;
}
Expand Down
3 changes: 2 additions & 1 deletion pallets/funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,8 @@ pub mod pallet {
project_id: ProjectId,
account: AccountIdOf<T>,
id: u32,
ct_amount: BalanceOf<T>,
final_ct_amount: BalanceOf<T>,
final_ct_price: PriceOf<T>,
},
ContributionSettled {
project_id: ProjectId,
Expand Down
2 changes: 0 additions & 2 deletions pallets/funding/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,6 @@ pub mod storage_types {
#[codec(compact)]
pub original_ct_amount: Balance,
pub original_ct_usd_price: Price,
pub final_ct_amount: Balance,
pub final_ct_usd_price: Price,
pub funding_asset: AcceptedFundingAsset,
pub funding_asset_amount_locked: Balance,
pub multiplier: Multiplier,
Expand Down

0 comments on commit b612f1d

Please sign in to comment.