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 24, 2024
1 parent a04bf46 commit 87b6edd
Show file tree
Hide file tree
Showing 26 changed files with 911 additions and 386 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.

6 changes: 4 additions & 2 deletions integration-tests/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ pub mod polimec {
let dot = (AcceptedFundingAsset::DOT.id(), prices.dot);
let usdc = (AcceptedFundingAsset::USDC.id(), prices.usdc);
let usdt = (AcceptedFundingAsset::USDT.id(), prices.usdt);
let plmc = (pallet_funding::PLMC_FOREIGN_ID, prices.plmc);
let plmc = (polimec_common::PLMC_FOREIGN_ID, prices.plmc);

let values: BoundedVec<(u32, FixedU128), <PolimecRuntime as orml_oracle::Config>::MaxFeedValues> =
vec![dot, usdc, usdt, plmc].try_into().expect("benchmarks can panic");
Expand Down Expand Up @@ -501,8 +501,10 @@ pub mod polimec {

funded_accounts.extend(accounts::init_balances().iter().cloned().map(|k| (k, INITIAL_DEPOSIT)));
funded_accounts.extend(collators::initial_authorities().iter().cloned().map(|(acc, _)| (acc, 20_005 * PLMC)));
funded_accounts.push((TreasuryAccount::get(), 20_005 * PLMC));
funded_accounts.push((TreasuryAccount::get(), 20_000_000 * PLMC));
funded_accounts.push((BlockchainOperationTreasury::get(), 20_005 * PLMC));
/// Treasury account needs PLMC for the One Token Model participations
funded_accounts.push((polimec_runtime::FeeRecipient::get(), INITIAL_DEPOSIT));

let genesis_config = polimec_runtime::RuntimeGenesisConfig {
system: Default::default(),
Expand Down
21 changes: 10 additions & 11 deletions integration-tests/src/tests/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
use crate::PolimecRuntime;
use frame_support::BoundedVec;
pub use pallet_funding::instantiator::{BidParams, ContributionParams, UserToUSDBalance};
use pallet_funding::{
AcceptedFundingAsset, BiddingTicketSizes, ContributingTicketSizes, CurrencyMetadata, PriceProviderOf,
ProjectMetadata, ProjectMetadataOf, TicketSize,
};
use pallet_funding::{AcceptedFundingAsset, BiddingTicketSizes, ContributingTicketSizes, CurrencyMetadata, ParticipationMode, PriceProviderOf, ProjectMetadata, ProjectMetadataOf, TicketSize};
use sp_arithmetic::{FixedPointNumber, Percent};

use macros::generate_accounts;
Expand Down Expand Up @@ -54,11 +51,13 @@ 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> {
vec![1u8, 6u8, 10u8, 8u8, 3u8]
pub fn default_bidder_modes() -> Vec<ParticipationMode> {
use ParticipationMode::{OTM, Classic};
vec![Classic(1u8), Classic(6u8), OTM, OTM, Classic(3u8)]
}
pub fn default_contributor_multipliers() -> Vec<u8> {
vec![1u8, 1u8, 1u8, 1u8, 1u8]
pub fn default_contributor_modes() -> Vec<ParticipationMode> {
use ParticipationMode::{OTM, Classic};
vec![Classic(1u8), Classic(1u8), OTM, OTM, Classic(3u8)]
}

pub fn default_project_metadata(issuer: AccountId) -> ProjectMetadataOf<polimec_runtime::Runtime> {
Expand Down Expand Up @@ -113,7 +112,7 @@ pub fn default_bids() -> Vec<BidParams<PolimecRuntime>> {
default_metadata.minimum_price,
default_weights(),
default_bidders(),
default_bidder_multipliers(),
default_bidder_modes(),
)
}

Expand All @@ -134,7 +133,7 @@ pub fn default_community_contributions() -> Vec<ContributionParams<PolimecRuntim
default_metadata.minimum_price,
default_weights(),
default_community_contributors(),
default_contributor_multipliers(),
default_contributor_modes(),
)
}

Expand All @@ -157,7 +156,7 @@ pub fn default_remainder_contributions() -> Vec<ContributionParams<PolimecRuntim
default_metadata.minimum_price,
vec![20u8, 15u8, 10u8, 25u8, 23u8, 7u8],
default_remainder_contributors(),
vec![1u8, 1u8, 1u8, 1u8, 1u8, 1u8],
default_contributor_modes(),
)
}
pub fn default_community_contributors() -> Vec<AccountId> {
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
2 changes: 1 addition & 1 deletion pallets/funding/src/functions/4_contribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<T: Config> Pallet<T> {
contributor: contributor.clone(),
ct_amount: buyable_tokens,
usd_contribution_amount: ticket_size,
mode: mode.clone(),
mode: mode,
funding_asset,
funding_asset_amount,
plmc_bond,
Expand Down
19 changes: 10 additions & 9 deletions pallets/funding/src/functions/6_settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use sp_runtime::{
traits::{Convert, Zero},
Perquintill,
};
use frame_support::traits::fungible::Inspect;

impl<T: Config> Pallet<T> {
#[transactional]
Expand Down Expand Up @@ -173,13 +174,14 @@ impl<T: Config> Pallet<T> {
Self::calculate_refund(&bid, funding_success, wap)?;

if bid.mode == ParticipationMode::OTM {
<pallet_proxy_bonding::Pallet<T>>::refund_fee(
project_id,
HoldReason::Participation.into(),
&bid.bidder,
refunded_plmc,
bid.funding_asset.id(),
)?;
if refunded_plmc > T::NativeCurrency::minimum_balance(){
<pallet_proxy_bonding::Pallet<T>>::refund_fee(
project_id,
&bid.bidder,
refunded_plmc,
bid.funding_asset.id(),
)?;
}
} else {
Self::release_participation_bond_for(&bid.bidder, refunded_plmc)?;
}
Expand All @@ -188,7 +190,7 @@ impl<T: Config> Pallet<T> {
if funding_success && bid.status != BidStatus::Rejected {
let ct_vesting_duration = Self::set_plmc_bond_release_with_mode(
bid.bidder.clone(),
bid.plmc_bond,
bid.plmc_bond.saturating_sub(refunded_plmc),
bid.mode,
project_details.funding_end_block.ok_or(Error::<T>::ImpossibleState)?,
)?;
Expand Down Expand Up @@ -267,7 +269,6 @@ impl<T: Config> Pallet<T> {
if contribution.mode == ParticipationMode::OTM {
<pallet_proxy_bonding::Pallet<T>>::refund_fee(
project_id,
HoldReason::Participation.into(),
&contribution.contributor,
contribution.plmc_bond,
contribution.funding_asset.id(),
Expand Down
2 changes: 1 addition & 1 deletion pallets/funding/src/functions/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<T: Config> Pallet<T> {
let plmc_usd_price =
<PriceProviderOf<T>>::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS)
.ok_or(Error::<T>::PriceNotFound)?;
let usd_bond = multiplier.calculate_bonding_requirement::<T>(ticket_size).ok_or(Error::<T>::BadMath)?;
let usd_bond = multiplier.calculate_usd_bonding_requirement::<T>(ticket_size).ok_or(Error::<T>::BadMath)?;
plmc_usd_price
.reciprocal()
.ok_or(Error::<T>::BadMath)?
Expand Down
Loading

0 comments on commit 87b6edd

Please sign in to comment.