Skip to content

Commit

Permalink
otm_funding_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Sep 23, 2024
1 parent a04bf46 commit 16877ad
Show file tree
Hide file tree
Showing 14 changed files with 527 additions and 246 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions integration-tests/src/tests/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn ipfs_hash() -> BoundedVec<u8, ConstU32<96>> {
pub fn default_weights() -> Vec<u8> {
vec![20u8, 15u8, 10u8, 25u8, 30u8]
}
pub fn default_bidder_multipliers() -> Vec<u8> {
pub fn default_bidder_modes() -> Vec<u8> {
vec![1u8, 6u8, 10u8, 8u8, 3u8]
}
pub fn default_contributor_multipliers() -> Vec<u8> {
Expand Down Expand Up @@ -113,7 +113,7 @@ pub fn default_bids() -> Vec<BidParams<PolimecRuntime>> {
default_metadata.minimum_price,
default_weights(),
default_bidders(),
default_bidder_multipliers(),
default_bidder_modes(),
)
}

Expand Down
51 changes: 27 additions & 24 deletions pallets/funding/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ where
default_project_metadata.minimum_price,
default_weights(),
default_bidders::<T>(),
default_bidder_multipliers(),
default_bidder_modes(),
)
}

Expand All @@ -145,7 +145,7 @@ where
default_project.minimum_price,
default_weights(),
default_bidders::<T>(),
default_bidder_multipliers(),
default_bidder_modes(),
)
}

Expand All @@ -170,7 +170,7 @@ where
default_project_metadata.minimum_price,
default_weights(),
default_community_contributors::<T>(),
default_community_contributor_multipliers(),
default_community_contributor_modes(),
)
}

Expand All @@ -195,7 +195,7 @@ where
10u128.into(),
default_weights(),
default_remainder_contributors::<T>(),
default_remainder_contributor_multipliers(),
default_remainder_contributor_modes(),
)
}

Expand Down Expand Up @@ -241,14 +241,17 @@ pub fn default_remainder_contributors<T: Config>() -> Vec<AccountIdOf<T>> {
]
}

pub fn default_bidder_multipliers() -> Vec<u8> {
vec![10u8, 3u8, 1u8, 7u8, 4u8]
pub fn default_bidder_modes() -> Vec<ParticipationMode> {
use ParticipationMode::{Classic, OTM};
vec![Classic(10u8), Classic(3u8), OTM, OTM, Classic(4u8)]
}
pub fn default_community_contributor_multipliers() -> Vec<u8> {
vec![2u8, 1u8, 3u8, 1u8, 1u8]
pub fn default_community_contributor_modes() -> Vec<ParticipationMode> {
use ParticipationMode::{Classic, OTM};
vec![Classic(2u8), Classic(1u8), Classic(3u8), OTM, OTM]
}
pub fn default_remainder_contributor_multipliers() -> Vec<u8> {
vec![1u8, 11u8, 1u8, 1u8, 1u8]
pub fn default_remainder_contributor_modes() -> Vec<ParticipationMode> {
use ParticipationMode::{Classic, OTM};
vec![Classic(1u8), OTM, Classic(1u8), OTM, Classic(1u8)]
}

/// Grab an account, seeded by a name and index.
Expand Down Expand Up @@ -665,7 +668,7 @@ mod benchmarks {

let project_id = inst.create_auctioning_project(project_metadata.clone(), issuer, None, evaluations);

let existing_bid = BidParams::new(bidder.clone(), (50 * CT_UNIT).into(), 5u8, AcceptedFundingAsset::USDT);
let existing_bid = BidParams::new(bidder.clone(), (50 * CT_UNIT).into(), ParticipationMode::Classic(5u8), AcceptedFundingAsset::USDT);

let existing_bids = vec![existing_bid; x as usize];
let existing_bids_post_bucketing =
Expand Down Expand Up @@ -708,7 +711,7 @@ mod benchmarks {
let current_bucket = Buckets::<T>::get(project_id).unwrap();
// first lets bring the bucket to almost its limit with another bidder:
assert!(new_bidder.clone() != bidder.clone());
let bid_params = BidParams::new(new_bidder, current_bucket.amount_left, 1u8, AcceptedFundingAsset::USDT);
let bid_params = BidParams::new(new_bidder, current_bucket.amount_left, ParticipationMode::Classic(1u8), AcceptedFundingAsset::USDT);
maybe_filler_bid = Some(bid_params.clone());
let plmc_for_new_bidder = inst.calculate_auction_plmc_charged_with_given_price(
&vec![bid_params.clone()],
Expand All @@ -733,7 +736,7 @@ mod benchmarks {
ct_amount = bucket_size * (y as u128);
usdt_for_filler_bidder = usdt_for_new_bidder;
}
let extrinsic_bid = BidParams::new(bidder.clone(), ct_amount, 1u8, AcceptedFundingAsset::USDT);
let extrinsic_bid = BidParams::new(bidder.clone(), ct_amount, ParticipationMode::Classic(1u8), AcceptedFundingAsset::USDT);
let original_extrinsic_bid = extrinsic_bid.clone();
let current_bucket = Buckets::<T>::get(project_id).unwrap();
// we need to call this after bidding `x` amount of times, to get the latest bucket from storage
Expand Down Expand Up @@ -786,7 +789,7 @@ mod benchmarks {
jwt,
project_id,
original_extrinsic_bid.amount,
original_extrinsic_bid.multiplier,
original_extrinsic_bid.mode,
original_extrinsic_bid.asset,
);

Expand All @@ -803,7 +806,7 @@ mod benchmarks {
original_ct_usd_price: Some(price),
funding_asset: Some(AcceptedFundingAsset::USDT),
funding_asset_amount_locked: None,
multiplier: Some(bid_params.multiplier),
mode: Some(bid_params.mode),
plmc_bond: None,
when: None,
};
Expand Down Expand Up @@ -862,11 +865,11 @@ mod benchmarks {
Event::<T>::Bid {
project_id,
ct_amount,
multiplier, ..
mode, ..
},
project_id == project_id,
ct_amount == bid_params.amount,
multiplier == bid_params.multiplier
mode == bid_params.mode
};
assert!(maybe_event.is_some(), "Event not found");
}
Expand Down Expand Up @@ -917,7 +920,7 @@ mod benchmarks {
BidParams::<T>::new(
account::<AccountIdOf<T>>("bidder", 0, i),
(min_bid_amount * CT_UNIT).into(),
1u8,
ParticipationMode::Classic(1u8),
AcceptedFundingAsset::USDT,
)
})
Expand All @@ -929,7 +932,7 @@ mod benchmarks {
let last_rejected_bid = BidParams::<T>::new(
account::<AccountIdOf<T>>("bidder", 0, 420),
auction_allocation - (min_bid_amount * CT_UNIT * (y as u128 - 1u128)),
1u8,
ParticipationMode::Classic(1u8),
AcceptedFundingAsset::USDT,
);
all_bids.push(last_rejected_bid.clone());
Expand All @@ -939,7 +942,7 @@ mod benchmarks {
let allocation_bid = BidParams::<T>::new(
account::<AccountIdOf<T>>("bidder", 0, y),
auction_allocation,
1u8,
ParticipationMode::Classic(1u8),
AcceptedFundingAsset::USDT,
);
all_bids.push(allocation_bid);
Expand All @@ -954,7 +957,7 @@ mod benchmarks {
BidParams::<T>::new(
account::<AccountIdOf<T>>("bidder", 0, i),
(min_bid_amount * CT_UNIT).into(),
1u8,
ParticipationMode::Classic(1u8),
AcceptedFundingAsset::USDT,
)
})
Expand Down Expand Up @@ -1501,7 +1504,7 @@ mod benchmarks {
.map(|_| UserToUSDBalance::new(participant.clone(), (100 * USD_UNIT).into()))
.collect_vec();
let participant_bids = (0..max_bids)
.map(|_| BidParams::new(participant.clone(), (500 * CT_UNIT).into(), 1u8, AcceptedFundingAsset::USDT))
.map(|_| BidParams::new(participant.clone(), (500 * CT_UNIT).into(), ParticipationMode::Classic(1u8), AcceptedFundingAsset::USDT))
.collect_vec();
let participant_contributions = (0..max_contributions)
.map(|_| {
Expand Down Expand Up @@ -1843,7 +1846,7 @@ mod benchmarks {
.map(|_| UserToUSDBalance::new(participant.clone(), (100 * USD_UNIT).into()))
.collect_vec();
let participant_bids = (0..max_bids)
.map(|_| BidParams::new(participant.clone(), (500 * CT_UNIT).into(), 1u8, AcceptedFundingAsset::USDT))
.map(|_| BidParams::new(participant.clone(), (500 * CT_UNIT).into(), ParticipationMode::Classic(1u8), AcceptedFundingAsset::USDT))
.collect_vec();
let participant_contributions = (0..max_contributions)
.map(|_| {
Expand Down Expand Up @@ -1934,7 +1937,7 @@ mod benchmarks {
.map(|_| UserToUSDBalance::new(participant.clone(), (100 * USD_UNIT).into()))
.collect_vec();
let participant_bids = (0..max_bids)
.map(|_| BidParams::new(participant.clone(), (500 * CT_UNIT).into(), 1u8, AcceptedFundingAsset::USDT))
.map(|_| BidParams::new(participant.clone(), (500 * CT_UNIT).into(), ParticipationMode::Classic(1u8), AcceptedFundingAsset::USDT))
.collect_vec();
let participant_contributions = (0..max_contributions)
.map(|_| {
Expand Down
64 changes: 39 additions & 25 deletions pallets/funding/src/instantiator/calculations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ impl<
T::ExistentialDeposit::get()
}

pub fn get_funding_asset_ed(&mut self, asset_id: AssetIdOf<T>) -> Balance {
self.execute(|| T::FundingCurrency::minimum_balance(asset_id))
}

pub fn get_ct_account_deposit(&self) -> Balance {
<T as crate::Config>::ContributionTokenCurrency::deposit_required(One::one())
}
Expand Down Expand Up @@ -61,7 +65,7 @@ impl<
BidParams {
bidder: bid.bidder.clone(),
amount: bid_amount,
multiplier: bid.multiplier,
mode: bid.mode,
asset: bid.asset,
},
bucket.current_price,
Expand All @@ -86,7 +90,8 @@ impl<
let mut output = Vec::new();
for bid in bids {
let usd_ticket_size = ct_price.saturating_mul_int(bid.amount);
let usd_bond = bid.multiplier.calculate_bonding_requirement::<T>(usd_ticket_size).unwrap();
let multiplier: MultiplierOf<T> = bid.mode.multiplier().try_into().ok().unwrap();
let usd_bond = multiplier.calculate_bonding_requirement::<T>(usd_ticket_size).unwrap();
let mut plmc_bond = plmc_usd_price.reciprocal().unwrap().saturating_mul_int(usd_bond);
if with_ed {
plmc_bond = plmc_bond.saturating_add(self.get_ed());
Expand All @@ -111,7 +116,8 @@ impl<

for (bid, price) in self.get_actual_price_charged_for_bucketed_bids(bids, project_metadata, maybe_bucket) {
let usd_ticket_size = price.saturating_mul_int(bid.amount);
let usd_bond = bid.multiplier.calculate_bonding_requirement::<T>(usd_ticket_size).unwrap();
let multiplier: MultiplierOf<T> = bid.mode.multiplier().try_into().ok().unwrap();
let usd_bond = multiplier.calculate_bonding_requirement::<T>(usd_ticket_size).unwrap();
let mut plmc_bond = plmc_usd_price.reciprocal().unwrap().saturating_mul_int(usd_bond);
if with_ed {
plmc_bond = plmc_bond.saturating_add(self.get_ed());
Expand Down Expand Up @@ -147,8 +153,8 @@ impl<
for (price_charged, bids) in grouped_by_price_bids {
for bid in bids {
let charged_usd_ticket_size = price_charged.saturating_mul_int(bid.amount);
let charged_usd_bond =
bid.multiplier.calculate_bonding_requirement::<T>(charged_usd_ticket_size).unwrap();
let multiplier: MultiplierOf<T> = bid.mode.multiplier().try_into().ok().unwrap();
let charged_usd_bond = multiplier.calculate_bonding_requirement::<T>(charged_usd_ticket_size).unwrap();
let charged_plmc_bond = plmc_usd_price.reciprocal().unwrap().saturating_mul_int(charged_usd_bond);

if remaining_cts <= Zero::zero() {
Expand All @@ -163,8 +169,9 @@ impl<
if weighted_average_price > price_charged { price_charged } else { weighted_average_price };

let actual_usd_ticket_size = final_price.saturating_mul_int(bought_cts);
let actual_usd_bond =
bid.multiplier.calculate_bonding_requirement::<T>(actual_usd_ticket_size).unwrap();

let multiplier: MultiplierOf<T> = bid.mode.multiplier().try_into().ok().unwrap();
let actual_usd_bond = multiplier.calculate_bonding_requirement::<T>(actual_usd_ticket_size).unwrap();
let actual_plmc_bond = plmc_usd_price.reciprocal().unwrap().saturating_mul_int(actual_usd_bond);

let returned_plmc_bond = charged_plmc_bond - actual_plmc_bond;
Expand Down Expand Up @@ -336,7 +343,7 @@ impl<
filtered_bids.push(BidParams {
bidder: bid.bidder.clone(),
amount: total_cts_left,
multiplier: bid.multiplier,
mode: bid.mode,
asset: bid.asset,
});
total_cts_left = Zero::zero();
Expand Down Expand Up @@ -433,7 +440,14 @@ impl<
.unwrap()
});
let usd_ticket_size = token_usd_price.saturating_mul_int(cont.amount);
let funding_asset_spent = funding_asset_usd_price.reciprocal().unwrap().saturating_mul_int(usd_ticket_size);
let mut funding_asset_spent =
funding_asset_usd_price.reciprocal().unwrap().saturating_mul_int(usd_ticket_size);
if cont.mode == ParticipationMode::OTM {
let multiplier: MultiplierOf<T> = cont.mode.multiplier().try_into().ok().unwrap();
let plmc_bond = multiplier.calculate_bonding_requirement::<T>(usd_ticket_size).unwrap();
let otm_fee = <pallet_proxy_bonding::Pallet<T>>::calculate_fee(plmc_bond, funding_asset_id).unwrap();
funding_asset_spent += otm_fee;
}
output.push(UserToFundingAsset::new(cont.contributor, funding_asset_spent, cont.asset.id()));
}
output
Expand Down Expand Up @@ -547,16 +561,16 @@ impl<
min_price: PriceOf<T>,
weights: Vec<u8>,
bidders: Vec<AccountIdOf<T>>,
multipliers: Vec<u8>,
modes: Vec<ParticipationMode>,
) -> Vec<BidParams<T>> {
assert_eq!(weights.len(), bidders.len(), "Should have enough weights for all the bidders");

zip(zip(weights, bidders), multipliers)
.map(|((weight, bidder), multiplier)| {
zip(zip(weights, bidders), modes)
.map(|((weight, bidder), mode)| {
let ticket_size = Percent::from_percent(weight) * usd_amount;
let token_amount = min_price.reciprocal().unwrap().saturating_mul_int(ticket_size);

BidParams::new(bidder, token_amount, multiplier, AcceptedFundingAsset::USDT)
BidParams::new(bidder, token_amount, mode, AcceptedFundingAsset::USDT)
})
.collect()
}
Expand All @@ -567,17 +581,17 @@ impl<
percent_funding: u8,
weights: Vec<u8>,
bidders: Vec<AccountIdOf<T>>,
multipliers: Vec<u8>,
modes: Vec<ParticipationMode>,
) -> Vec<BidParams<T>> {
let total_allocation_size = project_metadata.total_allocation_size;
let total_ct_bid = Percent::from_percent(percent_funding) * total_allocation_size;

assert_eq!(weights.len(), bidders.len(), "Should have enough weights for all the bidders");

zip(zip(weights, bidders), multipliers)
.map(|((weight, bidder), multiplier)| {
zip(zip(weights, bidders), modes)
.map(|((weight, bidder), mode)| {
let token_amount = Percent::from_percent(weight) * total_ct_bid;
BidParams::new(bidder, token_amount, multiplier, AcceptedFundingAsset::USDT)
BidParams::new(bidder, token_amount, mode, AcceptedFundingAsset::USDT)
})
.collect()
}
Expand All @@ -588,17 +602,17 @@ impl<
final_price: PriceOf<T>,
weights: Vec<u8>,
contributors: Vec<AccountIdOf<T>>,
multipliers: Vec<u8>,
modes: Vec<ParticipationMode>,
) -> Vec<ContributionParams<T>> {
zip(zip(weights, contributors), multipliers)
.map(|((weight, bidder), multiplier)| {
zip(zip(weights, contributors), modes)
.map(|((weight, bidder), mode)| {
let ticket_size = Percent::from_percent(weight) * usd_amount;
let token_amount = final_price.reciprocal().unwrap().saturating_mul_int(ticket_size);

ContributionParams::new(
bidder,
token_amount,
ParticipationMode::Classic(multiplier),
mode,
AcceptedFundingAsset::USDT,
)
})
Expand All @@ -611,20 +625,20 @@ impl<
percent_funding: u8,
weights: Vec<u8>,
contributors: Vec<AccountIdOf<T>>,
multipliers: Vec<u8>,
modes: Vec<ParticipationMode>,
) -> Vec<ContributionParams<T>> {
let total_allocation_size = project_metadata.total_allocation_size;
let total_ct_bought = Percent::from_percent(percent_funding) * total_allocation_size;

assert_eq!(weights.len(), contributors.len(), "Should have enough weights for all the bidders");

zip(zip(weights, contributors), multipliers)
.map(|((weight, contributor), multiplier)| {
zip(zip(weights, contributors), modes)
.map(|((weight, contributor), mode)| {
let token_amount = Percent::from_percent(weight) * total_ct_bought;
ContributionParams::new(
contributor,
token_amount,
ParticipationMode::Classic(multiplier),
mode,
AcceptedFundingAsset::USDT,
)
})
Expand Down
Loading

0 comments on commit 16877ad

Please sign in to comment.