Skip to content

Commit

Permalink
Remove stake_increment_share, add Fees type for Edfis NEW, rename…
Browse files Browse the repository at this point in the history
… `Position` to `ECDPPosition`.

Remove `stake_increment_share`, add `Fees` type for Edfis NEW, rename `Position` to `ECDPPosition`.
  • Loading branch information
balqaasem committed Mar 3, 2024
1 parent 5e2b654 commit b69b08f
Show file tree
Hide file tree
Showing 7 changed files with 469 additions and 247 deletions.
21 changes: 4 additions & 17 deletions blockchain/modules/edfis-swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use frame_system::pallet_prelude::*;
use module_support::{SwapDexIncentives, SwapManager, Erc20InfoMapping, ExchangeRate, Ratio, SwapLimit};
use orml_traits::{Happened, MultiCurrency, MultiCurrencyExtended};
use parity_scale_codec::MaxEncodedLen;
use primitives::{Balance, CurrencyId, TradingPair};
use primitives::{Balance, CurrencyId, Fees, TradingPair};
use scale_info::TypeInfo;
use sp_core::{H160, U256};
use sp_runtime::{
Expand All @@ -63,6 +63,8 @@ pub struct ProvisioningParameters<Balance, BlockNumber> {
target_provision: (Balance, Balance),
/// accumulated provision amount for this Provisioning trading pair.
accumulated_provision: (Balance, Balance),
/// accumulated provision amount for this Provisioning trading pair.
trading_fee: (u32, u32),
/// The number of block that status can be converted to Enabled.
not_before: BlockNumber,
}
Expand Down Expand Up @@ -391,22 +393,15 @@ pub mod module {
/// - `max_amount_b`: maximum amount of currency_id_b is allowed to inject to liquidity
/// pool.
/// - `min_share_increment`: minimum acceptable share amount.
/// - `stake_increment_share`: indicates whether to stake increased dex share to earn
/// incentives
#[pallet::call_index(2)]
#[pallet::weight(if *stake_increment_share {
<T as Config>::WeightInfo::add_liquidity_and_stake()
} else {
<T as Config>::WeightInfo::add_liquidity()
})]
#[pallet::weight(<T as Config>::WeightInfo::add_liquidity())]
pub fn add_liquidity(
origin: OriginFor<T>,
currency_id_a: CurrencyId,
currency_id_b: CurrencyId,
#[pallet::compact] max_amount_a: Balance,
#[pallet::compact] max_amount_b: Balance,
#[pallet::compact] min_share_increment: Balance,
stake_increment_share: bool,
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_add_liquidity(
Expand All @@ -416,7 +411,6 @@ pub mod module {
max_amount_a,
max_amount_b,
min_share_increment,
stake_increment_share,
)?;
Ok(())
}
Expand Down Expand Up @@ -987,7 +981,6 @@ impl<T: Config> Pallet<T> {
max_amount_a: Balance,
max_amount_b: Balance,
min_share_increment: Balance,
stake_increment_share: bool,
) -> sp_std::result::Result<(Balance, Balance, Balance), DispatchError> {
let trading_pair =
TradingPair::from_currency_ids(currency_id_a, currency_id_b).ok_or(Error::<T>::InvalidCurrencyId)?;
Expand Down Expand Up @@ -1080,10 +1073,6 @@ impl<T: Config> Pallet<T> {
*pool_0 = pool_0.checked_add(pool_0_increment).ok_or(ArithmeticError::Overflow)?;
*pool_1 = pool_1.checked_add(pool_1_increment).ok_or(ArithmeticError::Overflow)?;

if stake_increment_share {
T::SwapDexIncentives::do_deposit_dex_share(who, dex_share_currency_id, share_increment)?;
}

Self::deposit_event(Event::AddLiquidity {
who: who.clone(),
currency_0: trading_pair.first(),
Expand Down Expand Up @@ -1511,7 +1500,6 @@ impl<T: Config> SwapManager<T::AccountId, Balance, CurrencyId> for Pallet<T> {
max_amount_a: Balance,
max_amount_b: Balance,
min_share_increment: Balance,
stake_increment_share: bool,
) -> sp_std::result::Result<(Balance, Balance, Balance), DispatchError> {
Self::do_add_liquidity(
who,
Expand All @@ -1520,7 +1508,6 @@ impl<T: Config> SwapManager<T::AccountId, Balance, CurrencyId> for Pallet<T> {
max_amount_a,
max_amount_b,
min_share_increment,
stake_increment_share,
)
}

Expand Down
4 changes: 2 additions & 2 deletions blockchain/modules/support/src/ecdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use parity_scale_codec::FullCodec;
use primitives::Position;
use primitives::ECDPPosition;
use sp_core::U256;
use sp_runtime::{DispatchError, DispatchResult};
use sp_std::{
Expand Down Expand Up @@ -199,7 +199,7 @@ pub trait SlickUsdEcdpManager<AccountId, CurrencyId, Amount, Balance> {
/// Close ECDP loan using DEX
fn close_loan_by_dex(who: AccountId, currency_id: CurrencyId, max_collateral_amount: Balance) -> DispatchResult;
/// Get open ECDP corresponding to an account and collateral `CurrencyId`
fn get_position(who: &AccountId, currency_id: CurrencyId) -> Position;
fn get_position(who: &AccountId, currency_id: CurrencyId) -> ECDPPosition;
/// Get liquidation ratio for collateral `CurrencyId`
fn get_collateral_parameters(currency_id: CurrencyId) -> Vec<U256>;
/// Get current ratio of collateral to debit of open ECDP
Expand Down
230 changes: 230 additions & 0 deletions blockchain/modules/support/src/edfis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم

// This file is part of Setheum.

// Copyright (C) 2019-Present Setheum Labs.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use frame_support::{ensure, traits::Get};
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_core::H160;
use sp_runtime::{DispatchError, DispatchResult, RuntimeDebug};
use sp_std::{cmp::PartialEq, prelude::*, result::Result};

#[derive(RuntimeDebug, Encode, Decode, Clone, Copy, PartialEq, Eq, TypeInfo)]
pub enum SwapLimit<Balance> {
/// use exact amount supply amount to swap. (exact_supply_amount, minimum_target_amount)
ExactSupply(Balance, Balance),
/// swap to get exact amount target. (maximum_supply_amount, exact_target_amount)
ExactTarget(Balance, Balance),
}

pub trait SwapManager<AccountId, Balance, CurrencyId> {
fn get_liquidity_pool(
currency_id_a: CurrencyId,
currency_id_b: CurrencyId
) -> (Balance, Balance);

fn get_liquidity_token_address(
currency_id_a: CurrencyId,
currency_id_b: CurrencyId
) -> Option<H160>;

fn get_swap_amount(
path: &[CurrencyId],
limit: SwapLimit<Balance>
) -> Option<(Balance, Balance)>;

fn get_best_price_swap_path(
supply_currency_id: CurrencyId,
target_currency_id: CurrencyId,
limit: SwapLimit<Balance>,
alternative_path_joint_list: Vec<Vec<CurrencyId>>,
) -> Option<(Vec<CurrencyId>, Balance, Balance)>;

fn swap_with_specific_path(
who: &AccountId,
path: &[CurrencyId],
limit: SwapLimit<Balance>,
) -> Result<(Balance, Balance), DispatchError>;

fn add_liquidity(
who: &AccountId,
currency_id_a: CurrencyId,
currency_id_b: CurrencyId,
max_amount_a: Balance,
max_amount_b: Balance,
min_share_increment: Balance,
) -> Result<(Balance, Balance, Balance), DispatchError>;

fn remove_liquidity(
who: &AccountId,
currency_id_a: CurrencyId,
currency_id_b: CurrencyId,
remove_share: Balance,
min_withdrawn_a: Balance,
min_withdrawn_b: Balance,
by_unstake: bool,
) -> Result<(Balance, Balance), DispatchError>;
}

pub trait Swap<AccountId, Balance, CurrencyId>
where
CurrencyId: Clone,
{
fn get_swap_amount(
supply_currency_id: CurrencyId,
target_currency_id: CurrencyId,
limit: SwapLimit<Balance>,
) -> Option<(Balance, Balance)>;

fn swap(
who: &AccountId,
supply_currency_id: CurrencyId,
target_currency_id: CurrencyId,
limit: SwapLimit<Balance>,
) -> Result<(Balance, Balance), DispatchError>;

fn swap_by_path(
who: &AccountId,
swap_path: &[CurrencyId],
limit: SwapLimit<Balance>,
) -> Result<(Balance, Balance), DispatchError>;
}

#[derive(Eq, PartialEq, RuntimeDebug)]
pub enum SwapError {
CannotSwap,
}

impl Into<DispatchError> for SwapError {
fn into(self) -> DispatchError {
DispatchError::Other("Cannot swap")
}
}

// Dex wrapper of Swap implementation
pub struct SpecificJointsSwap<Dex, Joints>(sp_std::marker::PhantomData<(Dex, Joints)>);

impl<AccountId, Balance, CurrencyId, Dex, Joints> Swap<AccountId, Balance, CurrencyId>
for SpecificJointsSwap<Dex, Joints>
where
Dex: SwapManager<AccountId, Balance, CurrencyId>,
Joints: Get<Vec<Vec<CurrencyId>>>,
Balance: Clone,
CurrencyId: Clone,
{
fn get_swap_amount(
supply_currency_id: CurrencyId,
target_currency_id: CurrencyId,
limit: SwapLimit<Balance>,
) -> Option<(Balance, Balance)> {
<Dex as SwapManager<AccountId, Balance, CurrencyId>>::get_best_price_swap_path(
supply_currency_id,
target_currency_id,
limit,
Joints::get(),
)
.map(|(_, supply_amount, target_amount)| (supply_amount, target_amount))
}

fn swap(
who: &AccountId,
supply_currency_id: CurrencyId,
target_currency_id: CurrencyId,
limit: SwapLimit<Balance>,
) -> sp_std::result::Result<(Balance, Balance), DispatchError> {
let path = <Dex as SwapManager<AccountId, Balance, CurrencyId>>::get_best_price_swap_path(
supply_currency_id,
target_currency_id,
limit.clone(),
Joints::get(),
)
.ok_or_else(|| Into::<DispatchError>::into(SwapError::CannotSwap))?
.0;

<Dex as SwapManager<AccountId, Balance, CurrencyId>>::swap_with_specific_path(who, &path, limit)
}

fn swap_by_path(
who: &AccountId,
swap_path: &[CurrencyId],
limit: SwapLimit<Balance>,
) -> Result<(Balance, Balance), DispatchError> {
<Dex as SwapManager<AccountId, Balance, CurrencyId>>::swap_with_specific_path(who, swap_path, limit)
}
}

#[cfg(feature = "std")]
impl<AccountId, CurrencyId, Balance> SwapManager<AccountId, Balance, CurrencyId> for ()
where
Balance: Default,
{
fn get_liquidity_pool(_currency_id_a: CurrencyId, _currency_id_b: CurrencyId) -> (Balance, Balance) {
Default::default()
}

fn get_liquidity_token_address(_currency_id_a: CurrencyId, _currency_id_b: CurrencyId) -> Option<H160> {
Some(Default::default())
}

fn get_swap_amount(_path: &[CurrencyId], _limit: SwapLimit<Balance>) -> Option<(Balance, Balance)> {
Some(Default::default())
}

fn get_best_price_swap_path(
_supply_currency_id: CurrencyId,
_target_currency_id: CurrencyId,
_limit: SwapLimit<Balance>,
_alternative_path_joint_list: Vec<Vec<CurrencyId>>,
) -> Option<(Vec<CurrencyId>, Balance, Balance)> {
Some(Default::default())
}

fn swap_with_specific_path(
_who: &AccountId,
_path: &[CurrencyId],
_limit: SwapLimit<Balance>,
) -> Result<(Balance, Balance), DispatchError> {
Ok(Default::default())
}

fn add_liquidity(
_who: &AccountId,
_currency_id_a: CurrencyId,
_currency_id_b: CurrencyId,
_max_amount_a: Balance,
_max_amount_b: Balance,
_min_share_increment: Balance,
) -> Result<(Balance, Balance, Balance), DispatchError> {
Ok(Default::default())
}

fn remove_liquidity(
_who: &AccountId,
_currency_id_a: CurrencyId,
_currency_id_b: CurrencyId,
_remove_share: Balance,
_min_withdrawn_a: Balance,
_min_withdrawn_b: Balance,
_by_unstake: bool,
) -> Result<(Balance, Balance), DispatchError> {
Ok(Default::default())
}
}
Loading

0 comments on commit b69b08f

Please sign in to comment.