-
Notifications
You must be signed in to change notification settings - Fork 19
Define and implement VestingSchedule logic #422
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c686721
Define VestingSchedule trait
dmitrylavrenov 7857a38
Implement locked_at
dmitrylavrenov 5d14f88
Add docs to LinearWithCliff
dmitrylavrenov b95b1df
Implement end
dmitrylavrenov ccfeca8
Add little note
dmitrylavrenov c98e14d
Add validate logic
dmitrylavrenov 4b5566c
Enable no_std mod
dmitrylavrenov 8f3076f
Merge branch 'master' into vesting-schedule
dmitrylavrenov 14ab8fa
Improve validate logic for LinearWithCliff
dmitrylavrenov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [package] | ||
| name = "vesting-schedule" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| frame-support = { default-features = false, git = "https://github.com/humanode-network/substrate", branch = "master" } | ||
| sp-arithmetic = { default-features = false, git = "https://github.com/humanode-network/substrate", branch = "master" } | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "frame-support/std", | ||
| "sp-arithmetic/std", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| //! The vesting schedule. | ||
|
|
||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| use frame_support::traits::Currency as CurrencyT; | ||
| use sp_arithmetic::traits::{ | ||
| AtLeast32BitUnsigned, CheckedDiv, CheckedMul, Saturating, UniqueSaturatedFrom, | ||
| UniqueSaturatedInto, Zero, | ||
| }; | ||
|
|
||
| mod traits; | ||
| pub use traits::*; | ||
|
|
||
| /// Implements linear westing logic with cliff. | ||
| pub struct LinearWithCliff<AccountId, Moment, Currency: CurrencyT<AccountId>> { | ||
| /// Vesting cliff. | ||
| cliff: Moment, | ||
| /// Vesting period. | ||
| period: Moment, | ||
| /// Amount that should be unlocked per one vesting period. (!= 0) | ||
| per_period: Currency::Balance, | ||
| } | ||
|
|
||
| /// An error that can occur at linear with cliff vesting schedule logic. | ||
| pub enum LinearWithCliffError { | ||
| /// We don't let `per_period` be less than 1, or else the vesting will never end. | ||
| ZeroPerPeriod, | ||
| /// Genesis locked shouldn't be zero. | ||
| ZeroGenesisLocked, | ||
| } | ||
|
|
||
| impl<AccountId, Moment, Currency> VestingSchedule<AccountId> | ||
| for LinearWithCliff<AccountId, Moment, Currency> | ||
| where | ||
| Currency: CurrencyT<AccountId>, | ||
| Moment: AtLeast32BitUnsigned + Copy, | ||
| { | ||
| type Moment = Moment; | ||
|
|
||
| type Currency = Currency; | ||
|
|
||
| type Error = LinearWithCliffError; | ||
|
|
||
| fn validate( | ||
| &self, | ||
| genesis_locked: Currency::Balance, | ||
| _start: Self::Moment, | ||
| ) -> Result<(), Self::Error> { | ||
| if self.per_period == Zero::zero() { | ||
| return Err(LinearWithCliffError::ZeroPerPeriod); | ||
| } | ||
| if genesis_locked == Zero::zero() { | ||
| return Err(LinearWithCliffError::ZeroGenesisLocked); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn locked_at( | ||
| &self, | ||
| genesis_locked: Currency::Balance, | ||
| start: Self::Moment, | ||
| moment: Self::Moment, | ||
| ) -> Currency::Balance { | ||
| let actual_start = start.saturating_add(self.cliff); | ||
| if actual_start > moment { | ||
| return genesis_locked; | ||
| } | ||
|
|
||
| let actual_end = self.end(genesis_locked, start); | ||
| if actual_end < moment { | ||
| return Zero::zero(); | ||
| } | ||
|
|
||
| let actual_vesting_time = moment.saturating_sub(actual_start); | ||
| let periods_number = <Moment as UniqueSaturatedInto<u32>>::unique_saturated_into( | ||
| actual_vesting_time | ||
| .checked_div(&self.period) | ||
| .unwrap_or_else(Zero::zero), | ||
| ); | ||
| let vested_balance = self | ||
| .per_period | ||
| .checked_mul( | ||
| &<Currency::Balance as UniqueSaturatedFrom<u32>>::unique_saturated_from( | ||
| periods_number, | ||
| ), | ||
| ) | ||
| .unwrap_or_else(Zero::zero); | ||
| genesis_locked.saturating_sub(vested_balance) | ||
| } | ||
|
|
||
| fn end(&self, genesis_locked: Currency::Balance, start: Self::Moment) -> Self::Moment { | ||
| let periods_number = <Currency::Balance as UniqueSaturatedInto<u32>>::unique_saturated_into( | ||
| genesis_locked | ||
| .checked_div(&self.per_period) | ||
| .unwrap_or_else(Zero::zero), | ||
| ) + if (genesis_locked % self.per_period).is_zero() { | ||
| 0 | ||
| } else { | ||
| // `per_period` does not perfectly divide `locked`, so we need an extra period to | ||
| // unlock some amount less than `per_period`. | ||
| 1 | ||
| }; | ||
|
|
||
| let actual_start = start.saturating_add(self.cliff); | ||
| let actual_vesting_time = self | ||
| .period | ||
| .checked_mul( | ||
| &<Moment as UniqueSaturatedFrom<u32>>::unique_saturated_from(periods_number), | ||
| ) | ||
| .unwrap_or_else(Zero::zero); | ||
| actual_start.saturating_add(actual_vesting_time) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| //! Generic vesting schedule related traits to abstract away the implementations. | ||
|
|
||
| use frame_support::traits::Currency; | ||
|
|
||
| /// [`VestingSchedule`] defines logic for currency vesting(unlocking). | ||
| pub trait VestingSchedule<AccountId> { | ||
| /// The type used to denote time: Timestamp, BlockNumber, etc. | ||
| type Moment; | ||
| /// The currency that this schedule applies to. | ||
| type Currency: Currency<AccountId>; | ||
| /// An error that can occur at vesting schedule logic. | ||
| type Error; | ||
| /// Validate the schedule. | ||
| fn validate( | ||
| &self, | ||
| genesis_locked: <Self::Currency as Currency<AccountId>>::Balance, | ||
| start: Self::Moment, | ||
| ) -> Result<(), Self::Error>; | ||
| /// Locked amount at provided moment. | ||
| fn locked_at( | ||
| &self, | ||
| genesis_locked: <Self::Currency as Currency<AccountId>>::Balance, | ||
| start: Self::Moment, | ||
| moment: Self::Moment, | ||
| ) -> <Self::Currency as Currency<AccountId>>::Balance; | ||
| /// Moment at which the schedule ends. | ||
| fn end( | ||
| &self, | ||
| genesis_locked: <Self::Currency as Currency<AccountId>>::Balance, | ||
| start: Self::Moment, | ||
| ) -> Self::Moment; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.