Skip to content

Commit

Permalink
Feature/plmc 242 implement correct duration of vesting (#70)
Browse files Browse the repository at this point in the history
* wip

* feat(244): linear increase of duration based on vesting

* Update pallets/funding/src/types.rs

Co-authored-by: Leonardo Razovic <4128940+lrazovic@users.noreply.github.com>

* feat(242): small changes from Leonardo's feedback

* feat(242): change week to day conversion on config type

* chore(242): fmt

* feat(242): small optimization

---------

Co-authored-by: Leonardo Razovic <4128940+lrazovic@users.noreply.github.com>
  • Loading branch information
JuaniRios and lrazovic committed Aug 23, 2023
1 parent 8057e2e commit 6894904
Show file tree
Hide file tree
Showing 9 changed files with 377 additions and 359 deletions.
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

0 comments on commit 6894904

Please sign in to comment.