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 242 implement correct duration of vesting #70

Merged
merged 7 commits into from
Aug 23, 2023
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
30 changes: 17 additions & 13 deletions pallets/funding/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use super::*;
use sp_std::marker::PhantomData;

use crate::traits::{BondingRequirementCalculation, ProvideStatemintPrice};
use crate::traits::{BondingRequirementCalculation, ProvideStatemintPrice, VestingDurationCalculation};
use frame_support::{
dispatch::DispatchResult,
ensure,
Expand All @@ -38,6 +38,7 @@ use sp_arithmetic::Perquintill;

use polimec_traits::ReleaseSchedule;
use sp_arithmetic::traits::{CheckedDiv, CheckedSub, Zero};
use sp_runtime::traits::Convert;
use sp_std::prelude::*;

// Round transition functions
Expand Down Expand Up @@ -1639,26 +1640,27 @@ impl<T: Config> Pallet<T> {
multiplier: MultiplierOf<T>,
plmc_price: PriceOf<T>,
) -> Result<BalanceOf<T>, DispatchError> {
let usd_bond = multiplier.calculate_bonding_requirement(ticket_size).map_err(|_| Error::<T>::BadMath)?;
let usd_bond = multiplier.calculate_bonding_requirement::<T>(ticket_size).map_err(|_| Error::<T>::BadMath)?;
plmc_price.reciprocal().ok_or(Error::<T>::BadMath)?.checked_mul_int(usd_bond).ok_or(Error::<T>::BadMath.into())
}

/// Based on the amount of tokens and price to buy, a desired multiplier, and the type of investor the caller is,
/// calculate the amount and vesting periods of bonded PLMC and reward CT tokens.
pub fn calculate_vesting_info(
_caller: AccountIdOf<T>,
_multiplier: MultiplierOf<T>,
multiplier: MultiplierOf<T>,
bonded_amount: BalanceOf<T>,
) -> Result<VestingInfo<T::BlockNumber, BalanceOf<T>>, DispatchError> {
// TODO: duration should depend on `_multiplier` and `_caller` credential
let duration: u32 = 1u32 * parachains_common::DAYS;
let amount_per_block = bonded_amount.checked_div(&duration.into()).ok_or(Error::<T>::BadMath)?;
let duration: T::BlockNumber = multiplier.calculate_vesting_duration::<T>();
let duration_as_balance = T::BlockNumberToBalance::convert(duration);
let amount_per_block = if duration_as_balance == Zero::zero() {
bonded_amount
} else {
bonded_amount.checked_div(&duration_as_balance).ok_or(Error::<T>::BadMath)?
};

Ok(VestingInfo {
total_amount: bonded_amount,
amount_per_block,
duration: <T::BlockNumber as From<u32>>::from(duration),
})
Ok(VestingInfo { total_amount: bonded_amount, amount_per_block, duration })
}

/// Calculates the price (in USD) of contribution tokens for the Community and Remainder Rounds
Expand Down Expand Up @@ -1780,7 +1782,7 @@ impl<T: Config> Pallet<T> {

let usd_bond_needed = bid
.multiplier
.calculate_bonding_requirement(ticket_size)
.calculate_bonding_requirement::<T>(ticket_size)
.map_err(|_| Error::<T>::BadMath)?;
let plmc_bond_needed = plmc_price
.reciprocal()
Expand Down Expand Up @@ -1898,8 +1900,10 @@ impl<T: Config> Pallet<T> {

bid.funding_asset_amount_locked = funding_asset_amount_needed;

let usd_bond_needed =
bid.multiplier.calculate_bonding_requirement(new_ticket_size).map_err(|_| Error::<T>::BadMath)?;
let usd_bond_needed = bid
.multiplier
.calculate_bonding_requirement::<T>(new_ticket_size)
.map_err(|_| Error::<T>::BadMath)?;
let plmc_bond_needed = plmc_price
.reciprocal()
.ok_or(Error::<T>::BadMath)?
Expand Down
10 changes: 8 additions & 2 deletions pallets/funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,12 @@ const PLMC_STATEMINT_ID: u32 = 2069;
#[frame_support::pallet(dev_mode)]
pub mod pallet {
use super::*;
use crate::traits::{BondingRequirementCalculation, ProvideStatemintPrice};
use crate::traits::{BondingRequirementCalculation, ProvideStatemintPrice, VestingDurationCalculation};
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use local_macros::*;
use sp_arithmetic::Percent;
use sp_runtime::traits::Convert;

#[pallet::pallet]
pub struct Pallet<T>(_);
Expand All @@ -277,7 +278,7 @@ 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<Self> + Default + From<u32> + Copy;
type Multiplier: Parameter + BondingRequirementCalculation + VestingDurationCalculation + Default + Copy;

/// 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 @@ -395,6 +396,11 @@ pub mod pallet {
type EvaluatorSlash: Get<Percent>;

type TreasuryAccount: Get<AccountIdOf<Self>>;

/// Convert 24 hours as FixedU128, to the corresponding amount of blocks in the same type as frame_system
type DaysToBlocks: Convert<FixedU128, BlockNumberOf<Self>>;

type BlockNumberToBalance: Convert<BlockNumberOf<Self>, BalanceOf<Self>>;
}

#[pallet::storage]
Expand Down
4 changes: 3 additions & 1 deletion pallets/funding/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,12 @@ impl pallet_funding::Config for TestRuntime {
type Balance = Balance;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
type BlockNumberToBalance = ConvertInto;
type CandleAuctionDuration = CandleAuctionDuration;
type CommunityFundingDuration = CommunityRoundDuration;
type ContributionTokenCurrency = LocalAssets;
type ContributionVesting = ConstU32<4>;
type DaysToBlocks = DaysToBlocks;
type EnglishAuctionDuration = EnglishAuctionDuration;
type EvaluationDuration = EvaluationDuration;
type EvaluationSuccessThreshold = EarlyEvaluationThreshold;
Expand All @@ -260,7 +262,7 @@ impl pallet_funding::Config for TestRuntime {
type MaxContributionsPerUser = ConstU32<4>;
type MaxEvaluationsPerUser = ConstU32<4>;
type MaxProjectsToUpdatePerBlock = ConstU32<100>;
type Multiplier = Multiplier<TestRuntime>;
type Multiplier = Multiplier;
type NativeCurrency = Balances;
type PalletId = FundingPalletId;
type PreImageLimit = ConstU32<1024>;
Expand Down
Loading