Skip to content

Commit

Permalink
Merge pull request #445 from Setheum-Labs/serp-tes
Browse files Browse the repository at this point in the history
Serp tes
  • Loading branch information
balqaasem authored Aug 13, 2021
2 parents 2d604b0 + c92b170 commit 775af6f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 203 deletions.
123 changes: 40 additions & 83 deletions lib-serml/serp/serp-treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,18 @@ use frame_system::pallet_prelude::*;
use orml_traits::{GetByKey, MultiCurrency, MultiCurrencyExtended};
use primitives::{Balance, CurrencyId};
use sp_runtime::{
traits::{AccountIdConversion, Convert, Zero},
DispatchError, DispatchResult,
};
use sp_std::{
prelude::*,
traits::{AccountIdConversion, Zero},
DispatchError, DispatchResult
};
use sp_std::prelude::*;
use support::{
DEXManager, Price, PriceProvider, Ratio, SerpTreasury, SerpTreasuryExtended
};

mod price_to_balance_convertor;
mod market_price_to_balance_convertor;
mod peg_price_to_balance_convertor;
mod mock;
mod tests;
pub mod weights;

pub use price_to_balance_convertor::PriceToBalanceConvertor;
pub use market_price_to_balance_convertor::MarketPriceToBalanceConvertor;
pub use peg_price_to_balance_convertor::PegPriceToBalanceConvertor;
pub use module::*;
pub use weights::WeightInfo;

Expand Down Expand Up @@ -253,47 +245,24 @@ impl<T: Config> SerpTreasury<T::AccountId> for Pallet<T> {
Ok(())
}

fn get_peg_price_balance(currency_id: Self::CurrencyId) -> Price {
T::PriceSource::get_peg_price(currency_id).unwrap_or_else(T::DefaultPriceRate::get)
}

fn get_market_price_balance(currency_id: Self::CurrencyId) -> Price {
T::PriceSource::get_market_price(currency_id).unwrap_or_else(T::DefaultPriceRate::get)
}

fn get_price_balance(currency_id: Self::CurrencyId) -> Price {
T::PriceSource::get_price(currency_id).unwrap_or_else(T::DefaultPriceRate::get)
}

fn get_peg_price_balance_value(currency_id: Self::CurrencyId, balance: Self::Balance) -> Self::Balance {
crate::PegPriceToBalanceConvertor::<T>::convert((currency_id, balance))
}

fn get_market_price_balance_value(currency_id: Self::CurrencyId, balance: Self::Balance) -> Self::Balance {
crate::MarketPriceToBalanceConvertor::<T>::convert((currency_id, balance))
}

fn get_price_balance_value(currency_id: Self::CurrencyId, balance: Self::Balance) -> Self::Balance {
crate::PriceToBalanceConvertor::<T>::convert((currency_id, balance))
}

fn setter_on_tes() -> DispatchResult {
let currency_id = T::SetterCurrencyId::get();
let one: Balance = 1;
let market_price_into = Self::get_market_price_balance_value(currency_id, one);
let peg_price_into = Self::get_peg_price_balance_value(currency_id, one);
let market_price = <T as Config>::PriceSource::get_market_price(currency_id);
let peg_price = <T as Config>::PriceSource::get_peg_price(currency_id);
let total_supply = T::Currency::total_issuance(currency_id);

match market_price_into {
market_price_into if market_price_into > peg_price_into => {
match market_price {
market_price if market_price > peg_price => {

// safe from underflow because `peg_price_into` is checked to be less than `market_price_into`
let expand_by = get_supply_change(market_price_into, peg_price_into, total_supply);
// safe from underflow because `peg_price` is checked to be less than `market_price`
// expand_by = 0.2% of total_supply;
let expand_by = total_supply / 500;
Self::on_serpup(currency_id, expand_by)?;
}
market_price_into if market_price_into < peg_price_into => {
// safe from underflow because `peg_price_into` is checked to be greater than `market_price_into`
let contract_by = get_supply_change(peg_price_into, market_price_into, total_supply);
market_price if market_price < peg_price => {
// safe from underflow because `peg_price` is checked to be greater than `market_price`
// expand_by = 0.2% of total_supply;
let contract_by = total_supply / 500;
Self::on_serpdown(currency_id, contract_by)?;
}
_ => {}
Expand All @@ -304,21 +273,22 @@ impl<T: Config> SerpTreasury<T::AccountId> for Pallet<T> {

fn usdj_on_tes() -> DispatchResult {
let currency_id = T::GetSettUSDCurrencyId::get();
let one: Balance = 1;
let market_price_into = Self::get_market_price_balance_value(currency_id, one);
let peg_price_into = Self::get_peg_price_balance_value(currency_id, one);
let market_price = <T as Config>::PriceSource::get_market_price(currency_id);
let peg_price = <T as Config>::PriceSource::get_peg_price(currency_id);
let total_supply = T::Currency::total_issuance(currency_id);
match market_price_into {
market_price_into if market_price_into > peg_price_into => {

match market_price {
market_price if market_price > peg_price => {

// safe from underflow because `peg_price_into` is checked to be less than `market_price_into`
let expand_by = get_supply_change(market_price_into, peg_price_into, total_supply);
// safe from underflow because `peg_price` is checked to be less than `market_price`
// expand_by = 0.2% of total_supply;
let expand_by = total_supply / 500;
Self::on_serpup(currency_id, expand_by)?;
}
market_price_into if market_price_into < peg_price_into => {
// safe from underflow because `peg_price_into` is checked to be greater than `market_price_into`
let contract_by = get_supply_change(peg_price_into, market_price_into, total_supply);
market_price if market_price < peg_price => {
// safe from underflow because `peg_price` is checked to be greater than `market_price`
// expand_by = 0.2% of total_supply;
let contract_by = total_supply / 500;
Self::on_serpdown(currency_id, contract_by)?;
}
_ => {}
Expand Down Expand Up @@ -474,18 +444,16 @@ impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
path
}
};
let one: Balance = 1;
let price_impact_limit = Some(T::MaxSlippageSwapWithDEX::get());
let dinar_price_into = Self::get_price_balance_value(dinar_currency_id, one);
let setter_price_into = Self::get_price_balance_value(setter_currency_id, one);

// get a max_supply_amount of 105% of market value, marking the 5% slippage of `price_impact_limit`.
let relative_price = dinar_price_into / setter_price_into;
// get a min_target_amount of 105% of market value,
// marking the 5% slippage of `price_impact_limit`.
let (pool_0, pool_1) = T::Dex::get_liquidity_pool(setter_currency_id, dinar_currency_id);
let relative_price = pool_1 / pool_0;
let max_supply_amount_full = target_amount / relative_price;
let max_supply_amount_fives = max_supply_amount_full / 20;
let max_supply_amount = max_supply_amount_fives * 21;

// let Some(max_supply_amount) = T::Dex::get_swap_supply_amount(&swap_path, target_amount, price_impact_limit);

T::Currency::deposit(dinar_currency_id, &Self::account_id(), max_supply_amount)?;
T::Dex::swap_with_exact_target(
&Self::account_id(),
Expand Down Expand Up @@ -520,19 +488,16 @@ impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
path
}
};
let one: Balance = 1;
let price_impact_limit = Some(T::MaxSlippageSwapWithDEX::get());
let setter_price_into = Self::get_price_balance_value(setter_currency_id, one);
let settcurrency_price_into = Self::get_price_balance_value(currency_id, one);

// get a max_supply_amount of 105% of market value, marking the 5% slippage of `price_impact_limit`.
let relative_price = setter_price_into / settcurrency_price_into;
// get a min_target_amount of 105% of market value,
// marking the 5% slippage of `price_impact_limit`.
let (pool_0, pool_1) = T::Dex::get_liquidity_pool(currency_id, setter_currency_id);
let relative_price = pool_1 / pool_0;
let max_supply_amount_full = target_amount / relative_price;
let max_supply_amount_fives = max_supply_amount_full / 20;
let max_supply_amount = max_supply_amount_fives * 21;

// let Some(max_supply_amount) = T::Dex::get_swap_supply_amount(&swap_path, target_amount, price_impact_limit);

T::Currency::deposit(setter_currency_id, &Self::account_id(), max_supply_amount)?;
T::Dex::swap_with_exact_target(
&Self::account_id(),
Expand Down Expand Up @@ -568,18 +533,16 @@ impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
path
}
};
let one: Balance = 1;
let price_impact_limit = Some(T::MaxSlippageSwapWithDEX::get());
let stablecurrency_price_into = Self::get_price_balance_value(currency_id, one);
let dinar_price_into = Self::get_price_balance_value(dinar_currency_id, one);

// get a min_target_amount of 95% of market value, marking the 5% slippage of `price_impact_limit`.
let relative_price = dinar_price_into / stablecurrency_price_into;
// get a min_target_amount of 95% of market value,
// marking the 5% slippage of `price_impact_limit`.
let (pool_0, pool_1) = T::Dex::get_liquidity_pool(dinar_currency_id, currency_id);
let relative_price = pool_1 / pool_0;
let min_target_amount_full = supply_amount / relative_price;
let min_target_amount_fives = min_target_amount_full / 20;
let min_target_amount = min_target_amount_fives * 19;

// let Some(min_target_amount) = T::Dex::get_swap_target_amount(&swap_path, supply_amount, price_impact_limit);
T::Dex::swap_with_exact_supply(
&Self::account_id(),
swap_path,
Expand All @@ -589,9 +552,3 @@ impl<T: Config> SerpTreasuryExtended<T::AccountId> for Pallet<T> {
)
}
}

/// Calculate the amount of supply change from a fraction given as `nume_fraction`, `deno_fraction` and `supply`.
fn get_supply_change(nume_fraction: u128, deno_fraction: u128, supply: u128) -> u128 {
let fraction = nume_fraction / deno_fraction - 1;
fraction.saturating_mul(supply)
}

This file was deleted.

33 changes: 0 additions & 33 deletions lib-serml/serp/serp-treasury/src/peg_price_to_balance_convertor.rs

This file was deleted.

33 changes: 0 additions & 33 deletions lib-serml/serp/serp-treasury/src/price_to_balance_convertor.rs

This file was deleted.

21 changes: 0 additions & 21 deletions lib-serml/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,27 +229,6 @@ pub trait SerpTreasury<AccountId> {
/// SerpUp ratio for Setheum Foundation's Charity Fund
fn get_charity_fund_serpup(amount: Self::Balance, currency_id: Self::CurrencyId) -> DispatchResult;

/// get the `peg_price` of a currency in converted `Balance` data type.
fn get_peg_price_balance(currency_id: Self::CurrencyId) -> Price;

/// get the `market price` of a currency in converted `Balance` data type.
fn get_market_price_balance(currency_id: Self::CurrencyId) -> Price;

/// get the `market price` of a currency in converted `Balance` data type.
fn get_price_balance(currency_id: Self::CurrencyId) -> Price;

/// get the balance value of the`peg_price` of a currency
/// in converted `Balance` data type.
fn get_peg_price_balance_value(currency_id: Self::CurrencyId, balance: Self::Balance) -> Self::Balance;

/// get the balance value of the`market price` of a currency
/// in converted `Balance` data type.
fn get_market_price_balance_value(currency_id: Self::CurrencyId, balance: Self::Balance) -> Self::Balance;

/// get the balance value of the`market price` of a currency
/// in converted `Balance` data type.
fn get_price_balance_value(currency_id: Self::CurrencyId, balance: Self::Balance) -> Self::Balance;

/// Trigger SERP-TES when required for Setter (SETT) to serp_down or serp_up.
fn setter_on_tes() -> DispatchResult;

Expand Down

0 comments on commit 775af6f

Please sign in to comment.