Skip to content

Commit

Permalink
Remove the Percent type
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Jan 9, 2024
1 parent 6d162e6 commit a3dace8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 94 deletions.
22 changes: 11 additions & 11 deletions blueprints/olympus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ mod olympus {
/// Note the following:
/// * The key is a [`LockupPeriod`] of the seconds of the lockup time.
/// A u32 value of 1 equals 1 second.
/// * The value is a [`Percent`] which is a decimal between 0 and 1.
reward_rates: KeyValueStore<LockupPeriod, Percent>,
/// * The value is a [`Decimal`] which is a decimal value where 0
/// indicated 0%, 0.5 indicates 50%, 1 indicates 100%, and so on.
reward_rates: KeyValueStore<LockupPeriod, Decimal>,

/// The resource address of the USDC, USDT, or any stablecoin. This
/// resource is needed when trying to find the value of the tokens
Expand All @@ -154,7 +155,7 @@ mod olympus {
/// price. If the price difference is above this protocol parameter,
/// then the opening and closing of liquidity positions is not allowed
/// and fails at runtime.
maximum_allowed_price_difference: Percent,
maximum_allowed_price_difference: Decimal,

/// Controls whether the opening of new liquidity positions is enabled.
///
Expand Down Expand Up @@ -247,8 +248,7 @@ mod olympus {
is_close_liquidity_position_enabled: false,
reward_rates: KeyValueStore::new(),
maximum_allowed_price_staleness: 60, /* 1 Minutes */
maximum_allowed_price_difference: Percent::new(dec!(0.05))
.unwrap(), /* 5% price difference allowed */
maximum_allowed_price_difference: dec!(0.05),
}
.instantiate()
.prepare_to_globalize(owner_role)
Expand Down Expand Up @@ -346,7 +346,7 @@ mod olympus {
let upfront_xrd_reward = self
.reward_rates
.get(&lockup_period)
.map(|percent| input_value_in_xrd * **percent)
.map(|percent| input_value_in_xrd * *percent)
.map(|reward_amount| self.withdraw(XRD, reward_amount))
.expect("No reward percentage associated with lockup period.");

Expand All @@ -368,7 +368,7 @@ mod olympus {
.relative_difference(&pool_price)
.expect("Oracle price and pool price are of different assets");
assert!(
relative_difference <= *self.maximum_allowed_price_difference,
relative_difference <= self.maximum_allowed_price_difference,
"Found a {}% difference in the price of {}/{} when the maximum allowed is: {}",
relative_difference * dec!(100),
Runtime::bech32_encode_address(input_resource_address),
Expand Down Expand Up @@ -793,11 +793,11 @@ mod olympus {
/// # Arguments
///
/// * `lockup_period`: [`LockupPeriod`] - The lockup period.
/// * `rate`: [`Percent`] - The rewards rate as a percent.
/// * `rate`: [`Decimal`] - The rewards rate as a percent.
pub fn add_rewards_rate(
&mut self,
lockup_period: LockupPeriod,
rate: Percent,
rate: Decimal,
) {
self.reward_rates.insert(lockup_period, rate);
}
Expand Down Expand Up @@ -857,10 +857,10 @@ mod olympus {
///
/// # Arguments
///
/// `value`: [`Percent`] - The maximum allowed percentage difference.
/// `value`: [`Decimal`] - The maximum allowed percentage difference.
pub fn update_maximum_allowed_price_difference(
&mut self,
value: Percent,
value: Decimal,
) {
self.maximum_allowed_price_difference = value;
}
Expand Down
65 changes: 0 additions & 65 deletions libraries/adapters-interface/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,71 +72,6 @@ impl Price {
}
}

/// Represents a percentage with the [`Decimal`] as the underlying type used to
/// represent the percentage. This is a value between `0` and `1` where
/// `dec!(0)` is 0% and `dec!(1)` is 100%. This type is checked upon SBOR
/// decoding and construction to ensure that it matches these conditions.
#[derive(
Clone,
Copy,
Debug,
ScryptoEncode,
ScryptoCategorize,
ScryptoDescribe,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
)]
#[sbor(transparent)]
pub struct Percent(Decimal);

impl Percent {
pub fn new(value: Decimal) -> Option<Self> {
if value >= Decimal::ZERO && value <= Decimal::ONE {
Some(Self(value))
} else {
None
}
}

pub fn value(&self) -> &Decimal {
&self.0
}
}

impl Display for Percent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}%", self.0 * 100)
}
}

impl std::ops::Deref for Percent {
type Target = Decimal;

fn deref(&self) -> &Self::Target {
self.value()
}
}

impl<D: Decoder<ScryptoCustomValueKind>> Decode<ScryptoCustomValueKind, D>
for Percent
{
#[inline]
fn decode_body_with_value_kind(
decoder: &mut D,
value_kind: ValueKind<ScryptoCustomValueKind>,
) -> Result<Self, DecodeError> {
let inner =
<Decimal as Decode<
ScryptoCustomValueKind,
D,
>>::decode_body_with_value_kind(decoder, value_kind)?;
Self::new(inner).ok_or(DecodeError::InvalidCustomValue)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
11 changes: 2 additions & 9 deletions unit-tests/tests/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
mod utils;
use utils::*;

use adapters_interface::common::*;
use olympus::types::*;
use scrypto_test::prelude::*;

Expand Down Expand Up @@ -163,16 +162,10 @@ test_access_rules!(withdraw_pool_units(NonFungibleGlobalId::new(
NonFungibleLocalId::integer(0)
)));
test_access_rules!(
add_rewards_rate(
LockupPeriod::from_seconds(10),
Percent::new(dec!(0.5)).unwrap()
),
add_rewards_rate(LockupPeriod::from_seconds(10), dec!(0.5)),
protocol_owner
);
test_access_rules!(add_rewards_rate(
LockupPeriod::from_seconds(10),
Percent::new(dec!(0.5)).unwrap()
));
test_access_rules!(add_rewards_rate(LockupPeriod::from_seconds(10), dec!(0.5)));
test_access_rules!(
remove_rewards_rate(LockupPeriod::from_seconds(10)),
protocol_owner
Expand Down
9 changes: 3 additions & 6 deletions unit-tests/tests/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
mod utils;
use utils::*;

use adapters_interface::common::*;
use ociswap_adapter::*;
use olympus::{LiquidityPosition, LockupPeriod};

Expand Down Expand Up @@ -191,8 +190,7 @@ fn can_open_position_on_an_ociswap_pool() -> Result<(), RuntimeError> {
let price_bitcoin_base_xrd_quote = bitcoin_price / xrd_price;

let lockup_period = LockupPeriod::from_months(6);
let upfront_reward_percentage =
Percent::new(dec!(0.1)).expect("Must succeed!");
let upfront_reward_percentage = dec!(0.1);

protocol.oracle.set_price(
resources.bitcoin.0,
Expand Down Expand Up @@ -230,7 +228,7 @@ fn can_open_position_on_an_ociswap_pool() -> Result<(), RuntimeError> {
change.0.amount(env),
Ok(price_bitcoin_base_xrd_quote
* bitcoin_contribution_amount
* *upfront_reward_percentage)
* upfront_reward_percentage)
);
assert!(additional_resources.is_empty());

Expand Down Expand Up @@ -280,8 +278,7 @@ fn can_open_position_on_an_ociswap_pool() -> Result<(), RuntimeError> {
adapter_specific_data
.share_in_pool_when_opening_position
.checked_round(6, RoundingMode::ToNearestMidpointTowardZero),
Percent::new(dec!(0.1) / dec!(1.1))
.unwrap()
(dec!(0.1) / dec!(1.1))
.checked_round(6, RoundingMode::ToNearestMidpointTowardZero),
);

Expand Down
5 changes: 2 additions & 3 deletions unit-tests/tests/utils/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use radix_engine_interface::prelude::*;
use scrypto::prelude::{RoleDefinition, ToRoleEntry};
use scrypto_test::prelude::*;

use adapters_interface::common::*;
use ociswap_adapter::test_bindings::*;
use ociswap_adapter::*;
use olympus::test_bindings::*;
Expand Down Expand Up @@ -179,12 +178,12 @@ impl Environment {
// 6 months at 10% and 9 months at 20%.
olympus.add_rewards_rate(
LockupPeriod::from_months(6),
Percent::new(dec!(0.1)).expect("Must succeed!"),
dec!(0.1),
&mut env,
)?;
olympus.add_rewards_rate(
LockupPeriod::from_months(9),
Percent::new(dec!(0.2)).expect("Must succeed!"),
dec!(0.2),
&mut env,
)?;

Expand Down

0 comments on commit a3dace8

Please sign in to comment.