Skip to content

Commit

Permalink
Merge pull request #508 from Setheum-Labs/0.9.0-runtimes
Browse files Browse the repository at this point in the history
0.9.0 runtimes
  • Loading branch information
balqaasem authored Aug 31, 2021
2 parents 383cd5a + 6d028c9 commit 47e714e
Show file tree
Hide file tree
Showing 24 changed files with 577 additions and 510 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Setheum's Blockchain Network node Implementation in Rust, Substrate FRAME and Se

<div align="center">

[![Setheum version](https://img.shields.io/badge/Setheum-0.8.0-brightgreen?logo=Parity%20Substrate)](https://setheum.xyz/)
[![Setheum version](https://img.shields.io/badge/Setheum-0.9.0-brightgreen?logo=Parity%20Substrate)](https://setheum.xyz/)
[![Substrate version](https://img.shields.io/badge/Substrate-3.0.0-brightgreen?logo=Parity%20Substrate)](https://substrate.dev/)
[![License](https://img.shields.io/github/license/Setheum-Labs/Setheum?color=green)](https://github.com/Setheum-Labs/Setheum/blob/master/LICENSE)
<br />
Expand Down Expand Up @@ -39,7 +39,7 @@ DRAM("Setheum Dirham", 12) = 1, // Staking Reward and Governance Token - System
SETR("Setter", 12) = 2,
```

#### The SetCurrencies - SERP Stablecoins
#### The SettCurrencies - SERP Stablecoins
```
SETUSD("SetDollar", 12) = 3,
SETEUR("SetEuro", 12) = 4,
Expand All @@ -48,7 +48,7 @@ DRAM("Setheum Dirham", 12) = 1, // Staking Reward and Governance Token - System
SETSAR("SetRiyal", 12) = 7,
```

1. The Setter - The Setter is a basket currency pegged to the Top 10 Strongest and most valuable currencies. It serves as the medium of Exchange and the Defacto stablecoin of the Setheum Ecosystem. All other Setheum system stablecoins orbit around the Setter (SETR) and the SettMint for minting Setheum Currencies (system stablecoins) accepts only the Setter as the Minting Reserve Asset. Only with the Setter (SETR) can a user participate in the DNAR Auctions to stabilize the price of the Setter, while the Setter is Auctioned to stabilize the price of all the other SetCurrencies (system stablecoins). It's the star that brightens many planets - 10 to be exact
1. The Setter - The Setter is a basket currency pegged to the Top 10 Strongest and most valuable currencies. It serves as the medium of Exchange and the Defacto stablecoin of the Setheum Ecosystem. All other Setheum system stablecoins orbit around the Setter (SETR) and the SettMint for minting Setheum Currencies (system stablecoins) accepts only the Setter as the Minting Reserve Asset. Only with the Setter (SETR) can a user participate in the DNAR Auctions to stabilize the price of the Setter, while the Setter is Auctioned to stabilize the price of all the other SettCurrencies (system stablecoins). It's the star that brightens many planets - 10 to be exact

2. [The SERP](./lib-serp) - The SERP (Setheum Elastic Reserve Protocol) is algorithmically responsible for stabilizing the prices of the Setheum Stable Currencies. No human interferrance is needed for this, it's all algorithmically handled by the SERP. The SERP is the backbone of Setheum, it is based on my TES (Token Elasticity of Supply) algorithm based on PES (Price Elasticity of Supply) such that the demand curve or price of a currency determines the supply serping point, meaning the supply curve of a SetCurrency will be adjusted according to the demand curve of that specific SetCurrency. The result will be burning or minting an amount equivalent to the serping point produced by the SERP-TES, the burning amount will be bought back by the SERP automatically through the built-in-DEX and the bought amount will be burnt to meet the satisfaction of the demand curve to prop the price back up to its peg, the opposite is done to lower the price of an under-supplied currency that is on demand and above its peg on the demand curve, for this the mint amount is divided into receipients including the SettPayTreasury where CashDrops are deposited for users to claim, the System Treasury under Governance, the Charity Fund stewarded by the Setheum Foundation, and the WelfareTreasury, more on the Welfare Treasury below.

Expand Down
2 changes: 1 addition & 1 deletion lib-serml/dex/dex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "setheum-dex"
version = "0.9.0"
version = "0.8.0"
authors = ["Setheum Labs"]
edition = "2018"

Expand Down
96 changes: 33 additions & 63 deletions lib-serml/dex/dex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use orml_traits::{MultiCurrency, MultiCurrencyExtended};
use primitives::{Balance, CurrencyId, TradingPair};
use sp_core::{H160, U256};
use sp_runtime::{
traits::{AccountIdConversion, Bounded, One, Zero},
traits::{AccountIdConversion, One, Zero},
ArithmeticError, DispatchError, DispatchResult, FixedPointNumber, RuntimeDebug, SaturatedConversion,
};
use sp_std::{convert::TryInto, prelude::*, vec};
Expand Down Expand Up @@ -144,8 +144,6 @@ pub mod module {
InsufficientTargetAmount,
/// Supply amount is more than max_supply_amount
ExcessiveSupplyAmount,
/// The swap will cause unacceptable price impact
ExceedPriceImpactLimit,
/// Liquidity is not enough
InsufficientLiquidity,
/// The supply amount is zero
Expand Down Expand Up @@ -307,10 +305,10 @@ pub mod module {
path: Vec<CurrencyId>,
#[pallet::compact] supply_amount: Balance,
#[pallet::compact] min_target_amount: Balance,
) -> DispatchResultWithPostInfo {
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_swap_with_exact_supply(&who, &path, supply_amount, min_target_amount, None)?;
Ok(().into())
Self::do_swap_with_exact_supply(&who, &path, supply_amount, min_target_amount)?;
Ok(())
}

/// Trading with DEX, swap with exact target amount
Expand All @@ -325,10 +323,10 @@ pub mod module {
path: Vec<CurrencyId>,
#[pallet::compact] target_amount: Balance,
#[pallet::compact] max_supply_amount: Balance,
) -> DispatchResultWithPostInfo {
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_swap_with_exact_target(&who, &path, target_amount, max_supply_amount, None)?;
Ok(().into())
Self::do_swap_with_exact_target(&who, &path, target_amount, max_supply_amount)?;
Ok(())
}

/// Add liquidity to Enabled trading pair.
Expand All @@ -351,7 +349,7 @@ pub mod module {
#[pallet::compact] max_amount_a: Balance,
#[pallet::compact] max_amount_b: Balance,
#[pallet::compact] min_share_increment: Balance,
) -> DispatchResultWithPostInfo {
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_add_liquidity(
&who,
Expand All @@ -361,7 +359,7 @@ pub mod module {
max_amount_b,
min_share_increment,
)?;
Ok(().into())
Ok(())
}

/// Add provision to Provisioning trading pair.
Expand All @@ -380,10 +378,10 @@ pub mod module {
currency_id_b: CurrencyId,
#[pallet::compact] amount_a: Balance,
#[pallet::compact] amount_b: Balance,
) -> DispatchResultWithPostInfo {
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_add_provision(&who, currency_id_a, currency_id_b, amount_a, amount_b)?;
Ok(().into())
Ok(())
}

/// Claim dex share for founders who have participated in trading pair provision.
Expand All @@ -398,10 +396,10 @@ pub mod module {
owner: T::AccountId,
currency_id_a: CurrencyId,
currency_id_b: CurrencyId,
) -> DispatchResultWithPostInfo {
) -> DispatchResult {
let _ = ensure_signed(origin)?;
Self::do_claim_dex_share(&owner, currency_id_a, currency_id_b)?;
Ok(().into())
Ok(())
}

/// Remove liquidity from specific liquidity pool in the form of burning
Expand All @@ -422,7 +420,7 @@ pub mod module {
#[pallet::compact] remove_share: Balance,
#[pallet::compact] min_withdrawn_a: Balance,
#[pallet::compact] min_withdrawn_b: Balance,
) -> DispatchResultWithPostInfo {
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_remove_liquidity(
&who,
Expand All @@ -432,7 +430,7 @@ pub mod module {
min_withdrawn_a,
min_withdrawn_b,
)?;
Ok(().into())
Ok(())
}

/// List a new provisioning trading pair.
Expand All @@ -447,7 +445,7 @@ pub mod module {
target_provision_a: Balance,
target_provision_b: Balance,
not_before: T::BlockNumber,
) -> DispatchResultWithPostInfo {
) -> DispatchResult {
T::ListingOrigin::ensure_origin(origin)?;

let trading_pair =
Expand Down Expand Up @@ -494,7 +492,7 @@ pub mod module {
}),
);
Self::deposit_event(Event::ListProvisioning(trading_pair));
Ok(().into())
Ok(())
}

/// List a new trading pair, trading pair will become Enabled status
Expand All @@ -510,7 +508,7 @@ pub mod module {
target_provision_a: Balance,
target_provision_b: Balance,
not_before: T::BlockNumber,
) -> DispatchResultWithPostInfo {
) -> DispatchResult {
T::ListingOrigin::ensure_origin(origin)?;
let trading_pair =
TradingPair::from_currency_ids(currency_id_a, currency_id_b).ok_or(Error::<T>::InvalidCurrencyId)?;
Expand Down Expand Up @@ -541,7 +539,7 @@ pub mod module {
_ => return Err(Error::<T>::MustBeProvisioning.into()),
}

Ok(().into())
Ok(())
}

/// Enable a Provisioning trading pair if meet the condition.
Expand All @@ -551,7 +549,7 @@ pub mod module {
origin: OriginFor<T>,
currency_id_a: CurrencyId,
currency_id_b: CurrencyId,
) -> DispatchResultWithPostInfo {
) -> DispatchResult {
let _ = ensure_signed(origin)?;

let trading_pair =
Expand Down Expand Up @@ -618,7 +616,7 @@ pub mod module {
_ => return Err(Error::<T>::MustBeProvisioning.into()),
}

Ok(().into())
Ok(())
}

/// Enable a trading pair
Expand All @@ -630,7 +628,7 @@ pub mod module {
origin: OriginFor<T>,
currency_id_a: CurrencyId,
currency_id_b: CurrencyId,
) -> DispatchResultWithPostInfo {
) -> DispatchResult {
T::ListingOrigin::ensure_origin(origin)?;
let trading_pair =
TradingPair::from_currency_ids(currency_id_a, currency_id_b).ok_or(Error::<T>::InvalidCurrencyId)?;
Expand All @@ -648,7 +646,7 @@ pub mod module {

TradingPairStatuses::<T>::insert(trading_pair, TradingPairStatus::Enabled);
Self::deposit_event(Event::EnableTradingPair(trading_pair));
Ok(().into())
Ok(())
}

/// Disable a `Enabled` trading pair.
Expand All @@ -658,7 +656,7 @@ pub mod module {
origin: OriginFor<T>,
currency_id_a: CurrencyId,
currency_id_b: CurrencyId,
) -> DispatchResultWithPostInfo {
) -> DispatchResult {
T::ListingOrigin::ensure_origin(origin)?;
let trading_pair =
TradingPair::from_currency_ids(currency_id_a, currency_id_b).ok_or(Error::<T>::InvalidCurrencyId)?;
Expand All @@ -672,7 +670,7 @@ pub mod module {

TradingPairStatuses::<T>::insert(trading_pair, TradingPairStatus::Disabled);
Self::deposit_event(Event::DisableTradingPair(trading_pair));
Ok(().into())
Ok(())
}
}
}
Expand Down Expand Up @@ -1022,7 +1020,6 @@ impl<T: Config> Pallet<T> {
fn get_target_amounts(
path: &[CurrencyId],
supply_amount: Balance,
price_impact_limit: Option<Ratio>,
) -> sp_std::result::Result<Vec<Balance>, DispatchError> {
let path_length = path.len();
ensure!(
Expand Down Expand Up @@ -1051,13 +1048,6 @@ impl<T: Config> Pallet<T> {
let target_amount = Self::get_target_amount(supply_pool, target_pool, target_amounts[i]);
ensure!(!target_amount.is_zero(), Error::<T>::ZeroTargetAmount);

// check price impact if limit exists
if let Some(limit) = price_impact_limit {
let price_impact =
Ratio::checked_from_rational(target_amount, target_pool).unwrap_or_else(Ratio::max_value);
ensure!(price_impact <= limit, Error::<T>::ExceedPriceImpactLimit);
}

target_amounts[i + 1] = target_amount;
i += 1;
}
Expand All @@ -1068,7 +1058,6 @@ impl<T: Config> Pallet<T> {
fn get_supply_amounts(
path: &[CurrencyId],
target_amount: Balance,
price_impact_limit: Option<Ratio>,
) -> sp_std::result::Result<Vec<Balance>, DispatchError> {
let path_length = path.len();
ensure!(
Expand Down Expand Up @@ -1097,13 +1086,6 @@ impl<T: Config> Pallet<T> {
let supply_amount = Self::get_supply_amount(supply_pool, target_pool, supply_amounts[i]);
ensure!(!supply_amount.is_zero(), Error::<T>::ZeroSupplyAmount);

// check price impact if limit exists
if let Some(limit) = price_impact_limit {
let price_impact =
Ratio::checked_from_rational(supply_amounts[i], target_pool).unwrap_or_else(Ratio::max_value);
ensure!(price_impact <= limit, Error::<T>::ExceedPriceImpactLimit);
};

supply_amounts[i - 1] = supply_amount;
i -= 1;
}
Expand Down Expand Up @@ -1164,9 +1146,8 @@ impl<T: Config> Pallet<T> {
path: &[CurrencyId],
supply_amount: Balance,
min_target_amount: Balance,
price_impact_limit: Option<Ratio>,
) -> sp_std::result::Result<Balance, DispatchError> {
let amounts = Self::get_target_amounts(&path, supply_amount, price_impact_limit)?;
let amounts = Self::get_target_amounts(&path, supply_amount)?;
ensure!(
amounts[amounts.len() - 1] >= min_target_amount,
Error::<T>::InsufficientTargetAmount
Expand Down Expand Up @@ -1194,9 +1175,8 @@ impl<T: Config> Pallet<T> {
path: &[CurrencyId],
target_amount: Balance,
max_supply_amount: Balance,
price_impact_limit: Option<Ratio>,
) -> sp_std::result::Result<Balance, DispatchError> {
let amounts = Self::get_supply_amounts(&path, target_amount, price_impact_limit)?;
let amounts = Self::get_supply_amounts(&path, target_amount)?;
ensure!(amounts[0] <= max_supply_amount, Error::<T>::ExcessiveSupplyAmount);
let module_account_id = Self::account_id();
let actual_supply_amount = amounts[0];
Expand Down Expand Up @@ -1225,22 +1205,14 @@ impl<T: Config> DEXManager<T::AccountId, CurrencyId, Balance> for Pallet<T> {
T::CurrencyIdMapping::encode_evm_address(trading_pair.dex_share_currency_id())
}

fn get_swap_target_amount(
path: &[CurrencyId],
supply_amount: Balance,
price_impact_limit: Option<Ratio>,
) -> Option<Balance> {
Self::get_target_amounts(&path, supply_amount, price_impact_limit)
fn get_swap_target_amount(path: &[CurrencyId], supply_amount: Balance) -> Option<Balance> {
Self::get_target_amounts(&path, supply_amount)
.ok()
.map(|amounts| amounts[amounts.len() - 1])
}

fn get_swap_supply_amount(
path: &[CurrencyId],
target_amount: Balance,
price_impact_limit: Option<Ratio>,
) -> Option<Balance> {
Self::get_supply_amounts(&path, target_amount, price_impact_limit)
fn get_swap_supply_amount(path: &[CurrencyId], target_amount: Balance) -> Option<Balance> {
Self::get_supply_amounts(&path, target_amount)
.ok()
.map(|amounts| amounts[0])
}
Expand All @@ -1250,19 +1222,17 @@ impl<T: Config> DEXManager<T::AccountId, CurrencyId, Balance> for Pallet<T> {
path: &[CurrencyId],
supply_amount: Balance,
min_target_amount: Balance,
price_impact_limit: Option<Ratio>,
) -> sp_std::result::Result<Balance, DispatchError> {
Self::do_swap_with_exact_supply(who, path, supply_amount, min_target_amount, price_impact_limit)
Self::do_swap_with_exact_supply(who, path, supply_amount, min_target_amount)
}

fn swap_with_exact_target(
who: &T::AccountId,
path: &[CurrencyId],
target_amount: Balance,
max_supply_amount: Balance,
price_impact_limit: Option<Ratio>,
) -> sp_std::result::Result<Balance, DispatchError> {
Self::do_swap_with_exact_target(who, path, target_amount, max_supply_amount, price_impact_limit)
Self::do_swap_with_exact_target(who, path, target_amount, max_supply_amount)
}

// `do_add_liquidity` is used in genesis_build,
Expand Down
2 changes: 1 addition & 1 deletion lib-serml/dex/dex/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use super::*;
use frame_support::{construct_runtime, ord_parameter_types, parameter_types};
use frame_system::EnsureSignedBy;
use orml_traits::{parameter_type_with_key, MultiReservableCurrency};
use orml_traits::parameter_type_with_key;
use primitives::{Amount, TokenSymbol};
use sp_core::H256;
use sp_runtime::{testing::Header, traits::IdentityLookup};
Expand Down
Loading

0 comments on commit 47e714e

Please sign in to comment.