2021-10-19 15:51:13 -07:00
|
|
|
use crate::error::*;
|
|
|
|
use anchor_lang::prelude::*;
|
|
|
|
use anchor_spl::vote_weight_record;
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2021-10-20 13:07:10 -07:00
|
|
|
// Generate a VoteWeightRecord Anchor wrapper, owned by the current program.
|
|
|
|
// VoteWeightRecords are unique in that they are defined by the SPL governance
|
2021-11-28 21:51:06 -08:00
|
|
|
// program, but they are actually owned by this program.
|
2021-10-19 15:51:13 -07:00
|
|
|
vote_weight_record!(crate::ID);
|
|
|
|
|
|
|
|
/// Seconds in one day.
|
2021-11-25 22:49:22 -08:00
|
|
|
#[cfg(feature = "localnet")]
|
|
|
|
pub const SECS_PER_DAY: i64 = 10;
|
|
|
|
#[cfg(not(feature = "localnet"))]
|
2021-10-19 15:51:13 -07:00
|
|
|
pub const SECS_PER_DAY: i64 = 86_400;
|
|
|
|
|
2021-11-26 12:04:05 -08:00
|
|
|
/// Seconds in one month.
|
|
|
|
#[cfg(feature = "localnet")]
|
|
|
|
pub const SECS_PER_MONTH: i64 = 10;
|
|
|
|
#[cfg(not(feature = "localnet"))]
|
2021-11-30 00:23:07 -08:00
|
|
|
pub const SECS_PER_MONTH: i64 = 365 * SECS_PER_DAY / 12;
|
2021-11-26 12:04:05 -08:00
|
|
|
|
2021-10-19 15:51:13 -07:00
|
|
|
/// Maximum number of days one can lock for.
|
2021-11-30 00:23:07 -08:00
|
|
|
pub const MAX_DAYS_LOCKED: u64 = 7 * 365;
|
2021-10-19 15:51:13 -07:00
|
|
|
|
2021-11-26 12:04:05 -08:00
|
|
|
/// Maximum number of months one can lock for.
|
2021-11-30 00:23:07 -08:00
|
|
|
pub const MAX_MONTHS_LOCKED: u64 = 7 * 12;
|
2021-11-26 12:04:05 -08:00
|
|
|
|
2021-10-19 15:51:13 -07:00
|
|
|
#[zero_copy]
|
|
|
|
#[derive(AnchorSerialize, AnchorDeserialize)]
|
|
|
|
pub struct Lockup {
|
|
|
|
pub kind: LockupKind,
|
2021-10-24 18:14:06 -07:00
|
|
|
// Start of the lockup.
|
2021-10-19 15:51:13 -07:00
|
|
|
pub start_ts: i64,
|
2021-10-24 18:14:06 -07:00
|
|
|
// End of the lockup.
|
2021-10-19 15:51:13 -07:00
|
|
|
pub end_ts: i64,
|
2021-10-20 13:07:10 -07:00
|
|
|
// Empty bytes for future upgrades.
|
2021-11-28 21:51:06 -08:00
|
|
|
// TODO: what kinds of upgrades do we foresee?
|
2021-10-19 15:51:13 -07:00
|
|
|
pub padding: [u8; 16],
|
|
|
|
}
|
|
|
|
|
2021-12-03 00:48:10 -08:00
|
|
|
impl Default for Lockup {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
kind: LockupKind::None,
|
|
|
|
start_ts: 0,
|
|
|
|
end_ts: 0,
|
|
|
|
padding: [0; 16],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 15:51:13 -07:00
|
|
|
impl Lockup {
|
2021-12-03 05:51:10 -08:00
|
|
|
/// Create lockup for a given period
|
|
|
|
pub fn new_from_periods(kind: LockupKind, start_ts: i64, periods: u32) -> Result<Self> {
|
|
|
|
require!(periods as u64 <= kind.max_periods(), InvalidDays);
|
|
|
|
Ok(Self {
|
|
|
|
kind,
|
|
|
|
start_ts,
|
|
|
|
end_ts: start_ts
|
|
|
|
.checked_add(i64::from(periods).checked_mul(kind.period_secs()).unwrap())
|
|
|
|
.unwrap(),
|
|
|
|
padding: [0; 16],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-30 00:23:07 -08:00
|
|
|
/// Returns the number of periods left on the lockup.
|
|
|
|
pub fn periods_left(&self, curr_ts: i64) -> Result<u64> {
|
2021-10-22 16:57:20 -07:00
|
|
|
Ok(self
|
2021-11-30 00:23:07 -08:00
|
|
|
.periods_total()?
|
|
|
|
.saturating_sub(self.period_current(curr_ts)?))
|
2021-10-19 15:51:13 -07:00
|
|
|
}
|
|
|
|
|
2021-11-30 00:23:07 -08:00
|
|
|
/// Returns the current period in the vesting schedule.
|
|
|
|
pub fn period_current(&self, curr_ts: i64) -> Result<u64> {
|
|
|
|
let period_secs = self.kind.period_secs();
|
|
|
|
if period_secs == 0 {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
2021-10-22 16:57:20 -07:00
|
|
|
let d = u64::try_from({
|
|
|
|
let secs_elapsed = curr_ts.saturating_sub(self.start_ts);
|
2021-11-30 00:23:07 -08:00
|
|
|
secs_elapsed.checked_div(period_secs).unwrap()
|
2021-10-19 15:51:13 -07:00
|
|
|
})
|
2021-10-22 16:57:20 -07:00
|
|
|
.map_err(|_| ErrorCode::UnableToConvert)?;
|
|
|
|
Ok(d)
|
2021-10-19 15:51:13 -07:00
|
|
|
}
|
|
|
|
|
2021-11-30 00:23:07 -08:00
|
|
|
/// Returns the total amount of periods in the lockup period.
|
|
|
|
pub fn periods_total(&self) -> Result<u64> {
|
|
|
|
let period_secs = self.kind.period_secs();
|
|
|
|
if period_secs == 0 {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
2021-11-26 12:04:05 -08:00
|
|
|
|
|
|
|
// Number of seconds in the entire lockup.
|
|
|
|
let lockup_secs = self.end_ts.checked_sub(self.start_ts).unwrap();
|
2021-11-30 00:23:07 -08:00
|
|
|
require!(lockup_secs % period_secs == 0, InvalidLockupPeriod);
|
2021-11-26 12:04:05 -08:00
|
|
|
|
2021-11-30 00:23:07 -08:00
|
|
|
// Total periods in the entire lockup.
|
|
|
|
Ok(u64::try_from(lockup_secs.checked_div(period_secs).unwrap()).unwrap())
|
2021-11-26 12:04:05 -08:00
|
|
|
}
|
2021-10-19 15:51:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(u8)]
|
|
|
|
#[derive(AnchorSerialize, AnchorDeserialize, Debug, Clone, Copy)]
|
|
|
|
pub enum LockupKind {
|
2021-11-28 23:10:32 -08:00
|
|
|
None,
|
2021-10-19 15:51:13 -07:00
|
|
|
Daily,
|
2021-11-26 12:04:05 -08:00
|
|
|
Monthly,
|
2021-10-19 15:51:13 -07:00
|
|
|
Cliff,
|
|
|
|
}
|
2021-10-22 16:57:20 -07:00
|
|
|
|
2021-11-30 00:23:07 -08:00
|
|
|
impl LockupKind {
|
|
|
|
pub fn period_secs(&self) -> i64 {
|
|
|
|
match self {
|
|
|
|
LockupKind::None => 0,
|
|
|
|
LockupKind::Daily => SECS_PER_DAY,
|
|
|
|
LockupKind::Monthly => SECS_PER_MONTH,
|
|
|
|
LockupKind::Cliff => SECS_PER_DAY, // arbitrary choice
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn max_periods(&self) -> u64 {
|
|
|
|
match self {
|
|
|
|
LockupKind::None => 0,
|
|
|
|
LockupKind::Daily => MAX_DAYS_LOCKED,
|
|
|
|
LockupKind::Monthly => MAX_MONTHS_LOCKED,
|
|
|
|
LockupKind::Cliff => MAX_DAYS_LOCKED,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:57:20 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-12-02 11:05:36 -08:00
|
|
|
use crate::state::deposit_entry::DepositEntry;
|
2021-10-22 16:57:20 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn days_left_start() -> Result<()> {
|
|
|
|
run_test_days_left(TestDaysLeft {
|
|
|
|
expected_days_left: 10,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 0.0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn days_left_one_half() -> Result<()> {
|
|
|
|
run_test_days_left(TestDaysLeft {
|
|
|
|
expected_days_left: 10,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 0.5,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn days_left_one() -> Result<()> {
|
|
|
|
run_test_days_left(TestDaysLeft {
|
|
|
|
expected_days_left: 9,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 1.0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn days_left_one_and_one_half() -> Result<()> {
|
|
|
|
run_test_days_left(TestDaysLeft {
|
|
|
|
expected_days_left: 9,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 1.5,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn days_left_9() -> Result<()> {
|
|
|
|
run_test_days_left(TestDaysLeft {
|
|
|
|
expected_days_left: 1,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 9.0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn days_left_9_dot_one() -> Result<()> {
|
|
|
|
run_test_days_left(TestDaysLeft {
|
|
|
|
expected_days_left: 1,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 9.1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn days_left_9_dot_nine() -> Result<()> {
|
|
|
|
run_test_days_left(TestDaysLeft {
|
|
|
|
expected_days_left: 1,
|
|
|
|
days_total: 10.0,
|
2021-11-26 12:04:05 -08:00
|
|
|
curr_day: 9.9,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn days_left_ten() -> Result<()> {
|
|
|
|
run_test_days_left(TestDaysLeft {
|
|
|
|
expected_days_left: 0,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 10.0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn days_left_eleven() -> Result<()> {
|
|
|
|
run_test_days_left(TestDaysLeft {
|
|
|
|
expected_days_left: 0,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 11.0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-26 12:04:05 -08:00
|
|
|
#[test]
|
|
|
|
pub fn months_left_start() -> Result<()> {
|
|
|
|
run_test_months_left(TestMonthsLeft {
|
|
|
|
expected_months_left: 10,
|
|
|
|
months_total: 10.0,
|
|
|
|
curr_month: 0.,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn months_left_one_half() -> Result<()> {
|
|
|
|
run_test_months_left(TestMonthsLeft {
|
|
|
|
expected_months_left: 10,
|
|
|
|
months_total: 10.0,
|
|
|
|
curr_month: 0.5,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn months_left_one_and_a_half() -> Result<()> {
|
|
|
|
run_test_months_left(TestMonthsLeft {
|
|
|
|
expected_months_left: 9,
|
|
|
|
months_total: 10.0,
|
|
|
|
curr_month: 1.5,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn months_left_ten() -> Result<()> {
|
|
|
|
run_test_months_left(TestMonthsLeft {
|
|
|
|
expected_months_left: 9,
|
|
|
|
months_total: 10.0,
|
|
|
|
curr_month: 1.5,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn months_left_eleven() -> Result<()> {
|
|
|
|
run_test_months_left(TestMonthsLeft {
|
|
|
|
expected_months_left: 0,
|
|
|
|
months_total: 10.0,
|
|
|
|
curr_month: 11.,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:57:20 -07:00
|
|
|
#[test]
|
|
|
|
pub fn voting_power_cliff_warmup() -> Result<()> {
|
2021-10-22 22:43:13 -07:00
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
2021-10-22 16:57:20 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-22 22:43:13 -07:00
|
|
|
expected_voting_power: 0, // 0 warmup.
|
|
|
|
amount_deposited,
|
2021-10-22 16:57:20 -07:00
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: -0.5,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind::Cliff,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_cliff_start() -> Result<()> {
|
2021-10-22 22:43:13 -07:00
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
|
|
|
let expected_voting_power = (10 * amount_deposited) / MAX_DAYS_LOCKED;
|
2021-10-22 16:57:20 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-22 22:43:13 -07:00
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
2021-10-22 16:57:20 -07:00
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 0.5,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind::Cliff,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_cliff_one_third_day() -> Result<()> {
|
2021-10-22 22:43:13 -07:00
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
|
|
|
let expected_voting_power = (10 * amount_deposited) / MAX_DAYS_LOCKED;
|
2021-10-22 16:57:20 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-22 22:43:13 -07:00
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
2021-10-22 16:57:20 -07:00
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 0.33,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind::Cliff,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_cliff_half_day() -> Result<()> {
|
2021-10-22 22:43:13 -07:00
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
|
|
|
let expected_voting_power = (10 * amount_deposited) / MAX_DAYS_LOCKED;
|
2021-10-22 16:57:20 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-22 22:43:13 -07:00
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
2021-10-22 16:57:20 -07:00
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 0.5,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind::Cliff,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_cliff_two_thirds_day() -> Result<()> {
|
2021-10-22 22:43:13 -07:00
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
|
|
|
let expected_voting_power = (10 * amount_deposited) / MAX_DAYS_LOCKED;
|
2021-10-22 16:57:20 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-22 22:43:13 -07:00
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
2021-10-22 16:57:20 -07:00
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 0.66,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind::Cliff,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_cliff_one_day() -> Result<()> {
|
2021-10-22 22:43:13 -07:00
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
|
|
|
let expected_voting_power = (9 * amount_deposited) / MAX_DAYS_LOCKED;
|
2021-10-22 16:57:20 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-22 22:43:13 -07:00
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
2021-10-22 16:57:20 -07:00
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 1.0,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind::Cliff,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_cliff_one_day_one_third() -> Result<()> {
|
2021-10-22 22:43:13 -07:00
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
|
|
|
let expected_voting_power = (9 * amount_deposited) / MAX_DAYS_LOCKED;
|
2021-10-22 16:57:20 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-22 22:43:13 -07:00
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
2021-10-22 16:57:20 -07:00
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 1.33,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind::Cliff,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_cliff_two_days() -> Result<()> {
|
2021-10-22 22:43:13 -07:00
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
|
|
|
// (8/2555) * deposit w/ 6 decimals.
|
|
|
|
let expected_voting_power = (8 * amount_deposited) / MAX_DAYS_LOCKED;
|
2021-10-22 16:57:20 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-22 22:43:13 -07:00
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
2021-10-22 16:57:20 -07:00
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 2.0,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind::Cliff,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_cliff_nine_dot_nine_days() -> Result<()> {
|
2021-10-22 22:43:13 -07:00
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
|
|
|
let expected_voting_power = amount_deposited / MAX_DAYS_LOCKED;
|
2021-10-22 16:57:20 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-22 22:43:13 -07:00
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
2021-10-22 16:57:20 -07:00
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 9.9,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind::Cliff,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_cliff_ten_days() -> Result<()> {
|
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-22 22:43:13 -07:00
|
|
|
expected_voting_power: 0, // (0/MAX_DAYS_LOCKED) * deposit w/ 6 decimals.
|
|
|
|
amount_deposited: 10 * 1_000_000,
|
2021-10-22 16:57:20 -07:00
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 10.0,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind::Cliff,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_cliff_ten_dot_one_days() -> Result<()> {
|
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-22 22:43:13 -07:00
|
|
|
expected_voting_power: 0, // (0/MAX_DAYS_LOCKED) * deposit w/ 6 decimals.
|
2021-10-22 16:57:20 -07:00
|
|
|
amount_deposited: 10 * 1_000_000, // 10 tokens with 6 decimals.
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 10.1,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind::Cliff,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_cliff_eleven_days() -> Result<()> {
|
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-22 22:43:13 -07:00
|
|
|
expected_voting_power: 0, // (0/MAX_DAYS_LOCKED) * deposit w/ 6 decimals.
|
2021-10-22 16:57:20 -07:00
|
|
|
amount_deposited: 10 * 1_000_000, // 10 tokens with 6 decimals.
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 10.1,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind::Cliff,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_daily_warmup() -> Result<()> {
|
|
|
|
run_test_voting_power(TestVotingPower {
|
|
|
|
expected_voting_power: 0, // (0/MAX_DAYS_LOCKED) * deposit w/ 6 decimals.
|
|
|
|
amount_deposited: 10 * 1_000_000, // 10 tokens with 6 decimals.
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: -1.0,
|
|
|
|
kind: LockupKind::Daily,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_daily_start() -> Result<()> {
|
2021-10-23 13:48:19 -07:00
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
2021-11-28 23:04:33 -08:00
|
|
|
let expected_voting_power = locked_daily_power(amount_deposited, 0, 10);
|
2021-10-22 22:43:13 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
2021-10-23 13:48:19 -07:00
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
2021-10-22 22:43:13 -07:00
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 0.0,
|
|
|
|
kind: LockupKind::Daily,
|
2021-10-22 16:57:20 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-23 13:48:19 -07:00
|
|
|
#[test]
|
|
|
|
pub fn voting_power_daily_one_half() -> Result<()> {
|
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
2021-11-28 23:04:33 -08:00
|
|
|
let expected_voting_power = locked_daily_power(amount_deposited, 0, 10);
|
2021-10-23 13:48:19 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 0.5,
|
|
|
|
kind: LockupKind::Daily,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_daily_one() -> Result<()> {
|
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
2021-11-28 23:04:33 -08:00
|
|
|
let expected_voting_power = locked_daily_power(amount_deposited, 1, 10);
|
2021-10-23 13:48:19 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 1.0,
|
|
|
|
kind: LockupKind::Daily,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_daily_one_and_one_third() -> Result<()> {
|
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
2021-11-28 23:04:33 -08:00
|
|
|
let expected_voting_power = locked_daily_power(amount_deposited, 1, 10);
|
2021-10-23 13:48:19 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 1.3,
|
|
|
|
kind: LockupKind::Daily,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_daily_two() -> Result<()> {
|
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
2021-11-28 23:04:33 -08:00
|
|
|
let expected_voting_power = locked_daily_power(amount_deposited, 2, 10);
|
2021-10-23 13:48:19 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 2.0,
|
|
|
|
kind: LockupKind::Daily,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_daily_nine() -> Result<()> {
|
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
2021-11-28 23:04:33 -08:00
|
|
|
let expected_voting_power = locked_daily_power(amount_deposited, 9, 10);
|
2021-10-23 13:48:19 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 9.0,
|
|
|
|
kind: LockupKind::Daily,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_daily_nine_dot_nine() -> Result<()> {
|
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
2021-11-28 23:04:33 -08:00
|
|
|
let expected_voting_power = locked_daily_power(amount_deposited, 9, 10);
|
2021-10-23 13:48:19 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 9.9,
|
|
|
|
kind: LockupKind::Daily,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_daily_ten() -> Result<()> {
|
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
2021-11-28 23:04:33 -08:00
|
|
|
let expected_voting_power = locked_daily_power(amount_deposited, 10, 10);
|
2021-10-23 13:48:19 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 10.0,
|
|
|
|
kind: LockupKind::Daily,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_daily_ten_dot_one() -> Result<()> {
|
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
2021-11-28 23:04:33 -08:00
|
|
|
let expected_voting_power = locked_daily_power(amount_deposited, 10, 10);
|
2021-10-23 13:48:19 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 10.1,
|
|
|
|
kind: LockupKind::Daily,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn voting_power_daily_eleven() -> Result<()> {
|
|
|
|
// 10 tokens with 6 decimals.
|
|
|
|
let amount_deposited = 10 * 1_000_000;
|
2021-11-28 23:04:33 -08:00
|
|
|
let expected_voting_power = locked_daily_power(amount_deposited, 11, 10);
|
2021-10-23 13:48:19 -07:00
|
|
|
run_test_voting_power(TestVotingPower {
|
|
|
|
expected_voting_power,
|
|
|
|
amount_deposited,
|
|
|
|
days_total: 10.0,
|
|
|
|
curr_day: 11.0,
|
|
|
|
kind: LockupKind::Daily,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:57:20 -07:00
|
|
|
struct TestDaysLeft {
|
|
|
|
expected_days_left: u64,
|
|
|
|
days_total: f64,
|
|
|
|
curr_day: f64,
|
|
|
|
}
|
|
|
|
|
2021-11-26 12:04:05 -08:00
|
|
|
struct TestMonthsLeft {
|
|
|
|
expected_months_left: u64,
|
|
|
|
months_total: f64,
|
|
|
|
curr_month: f64,
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:57:20 -07:00
|
|
|
struct TestVotingPower {
|
|
|
|
amount_deposited: u64,
|
|
|
|
days_total: f64,
|
|
|
|
curr_day: f64,
|
|
|
|
expected_voting_power: u64,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: LockupKind,
|
2021-10-22 16:57:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run_test_days_left(t: TestDaysLeft) -> Result<()> {
|
|
|
|
let start_ts = 1634929833;
|
|
|
|
let end_ts = start_ts + days_to_secs(t.days_total);
|
|
|
|
let curr_ts = start_ts + days_to_secs(t.curr_day);
|
|
|
|
let l = Lockup {
|
|
|
|
kind: LockupKind::Cliff,
|
|
|
|
start_ts,
|
|
|
|
end_ts,
|
|
|
|
padding: [0u8; 16],
|
|
|
|
};
|
2021-11-30 00:23:07 -08:00
|
|
|
let days_left = l.periods_left(curr_ts)?;
|
2021-10-22 16:57:20 -07:00
|
|
|
assert_eq!(days_left, t.expected_days_left);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-11-26 12:04:05 -08:00
|
|
|
fn run_test_months_left(t: TestMonthsLeft) -> Result<()> {
|
|
|
|
let start_ts = 1634929833;
|
|
|
|
let end_ts = start_ts + months_to_secs(t.months_total);
|
|
|
|
let curr_ts = start_ts + months_to_secs(t.curr_month);
|
|
|
|
let l = Lockup {
|
|
|
|
kind: LockupKind::Monthly,
|
|
|
|
start_ts,
|
|
|
|
end_ts,
|
|
|
|
padding: [0u8; 16],
|
|
|
|
};
|
2021-11-30 00:23:07 -08:00
|
|
|
let months_left = l.periods_left(curr_ts)?;
|
2021-11-26 12:04:05 -08:00
|
|
|
assert_eq!(months_left, t.expected_months_left);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-22 16:57:20 -07:00
|
|
|
fn run_test_voting_power(t: TestVotingPower) -> Result<()> {
|
|
|
|
let start_ts = 1634929833;
|
|
|
|
let end_ts = start_ts + days_to_secs(t.days_total);
|
|
|
|
let d = DepositEntry {
|
|
|
|
is_used: true,
|
|
|
|
rate_idx: 0,
|
2021-11-28 23:04:33 -08:00
|
|
|
amount_deposited_native: t.amount_deposited,
|
|
|
|
amount_initially_locked_native: t.amount_deposited,
|
2021-12-01 01:20:39 -08:00
|
|
|
allow_clawback: false,
|
2021-10-22 16:57:20 -07:00
|
|
|
lockup: Lockup {
|
|
|
|
start_ts,
|
|
|
|
end_ts,
|
2021-10-22 22:43:13 -07:00
|
|
|
kind: t.kind,
|
2021-10-22 16:57:20 -07:00
|
|
|
padding: [0u8; 16],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let curr_ts = start_ts + days_to_secs(t.curr_day);
|
2021-11-28 23:04:33 -08:00
|
|
|
let power = d.voting_power_locked(curr_ts, t.amount_deposited)?;
|
2021-10-22 16:57:20 -07:00
|
|
|
assert_eq!(power, t.expected_voting_power);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn days_to_secs(days: f64) -> i64 {
|
2021-11-30 00:23:07 -08:00
|
|
|
let d = (SECS_PER_DAY as f64) * days;
|
2021-11-26 12:04:05 -08:00
|
|
|
d.round() as i64
|
|
|
|
}
|
|
|
|
|
|
|
|
fn months_to_secs(months: f64) -> i64 {
|
2021-11-30 00:23:07 -08:00
|
|
|
let d = (SECS_PER_MONTH as f64) * months;
|
2021-10-22 16:57:20 -07:00
|
|
|
d.round() as i64
|
|
|
|
}
|
2021-10-23 13:48:19 -07:00
|
|
|
|
|
|
|
// Calculates locked voting power. Done iteratively as a sanity check on
|
|
|
|
// the closed form calcuation.
|
|
|
|
//
|
|
|
|
// deposit - the amount locked up
|
2021-11-28 23:04:33 -08:00
|
|
|
// day - the current day in the lockup period
|
|
|
|
// total_days - the number of days locked up
|
|
|
|
fn locked_daily_power(amount: u64, day: u64, total_days: u64) -> u64 {
|
|
|
|
if day >= total_days {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
let days_remaining = total_days - day;
|
2021-10-23 13:48:19 -07:00
|
|
|
let mut total = 0f64;
|
2021-11-28 23:04:33 -08:00
|
|
|
for k in 1..=days_remaining {
|
|
|
|
// We have 'days_remaining' remaining cliff-locked deposits of
|
|
|
|
// amount / total_days each. Each of these deposits gets a scaling
|
|
|
|
// of k / MAX_DAYS_LOCKED.
|
|
|
|
total += (k as f64 * amount as f64) / (MAX_DAYS_LOCKED as f64 * total_days as f64)
|
2021-10-23 13:48:19 -07:00
|
|
|
}
|
|
|
|
total.floor() as u64
|
|
|
|
}
|
2021-10-22 16:57:20 -07:00
|
|
|
}
|