-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove
stake_increment_share
, add Fees
type for Edfis NEW, rename…
… `Position` to `ECDPPosition`. Remove `stake_increment_share`, add `Fees` type for Edfis NEW, rename `Position` to `ECDPPosition`.
- Loading branch information
Showing
7 changed files
with
469 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
Oops, something went wrong.