Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions node/primitives/src/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ macro_rules! create_currency_id {
type Error = ();
fn try_from(id: CurrencyId) -> Result<AssetId, ()> {
let _index = match id {
$(CurrencyId::Token(TokenSymbol::$symbol) => Ok((0_u32, TokenSymbol::$symbol as u32)),)*
$(CurrencyId::Native(TokenSymbol::$symbol) => Ok((0_u32, TokenSymbol::$symbol as u32)),)*
$(CurrencyId::VToken(TokenSymbol::$symbol) => Ok((1_u32, TokenSymbol::$symbol as u32)),)*
$(CurrencyId::Native(TokenSymbol::$symbol) => Ok((2_u32, TokenSymbol::$symbol as u32)),)*
$(CurrencyId::Token(TokenSymbol::$symbol) => Ok((2_u32, TokenSymbol::$symbol as u32)),)*
$(CurrencyId::Stable(TokenSymbol::$symbol) => Ok((3_u32, TokenSymbol::$symbol as u32)),)*
$(CurrencyId::VSToken(TokenSymbol::$symbol) => Ok((4_u32, TokenSymbol::$symbol as u32)),)*
_ => Err(()),
Expand Down Expand Up @@ -110,9 +110,9 @@ macro_rules! create_currency_id {
_ => Err(()),
};
match c_discr {
0 => Ok(CurrencyId::Token(token_symbol?)),
0 => Ok(CurrencyId::Native(token_symbol?)),
1 => Ok(CurrencyId::VToken(token_symbol?)),
2 => Ok(CurrencyId::Native(token_symbol?)),
2 => Ok(CurrencyId::Token(token_symbol?)),
3 => Ok(CurrencyId::Stable(token_symbol?)),
4 => Ok(CurrencyId::VSToken(token_symbol?)),
_ => Err(()),
Expand Down Expand Up @@ -245,9 +245,9 @@ impl Default for TokenSymbol {
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub enum CurrencyId {
Token(TokenSymbol),
VToken(TokenSymbol),
Native(TokenSymbol),
VToken(TokenSymbol),
Token(TokenSymbol),
Stable(TokenSymbol),
VSToken(TokenSymbol),
VSBond(TokenSymbol, ParaId, LeasePeriod, LeasePeriod),
Expand Down Expand Up @@ -289,9 +289,9 @@ impl CurrencyId {

pub fn discriminant(&self) -> u8 {
match *self {
Self::Token(..) => 0,
Self::Native(..) => 0,
Self::VToken(..) => 1,
Self::Native(..) => 2,
Self::Token(..) => 2,
Self::Stable(..) => 3,
Self::VSToken(..) => 4,
Self::VSBond(..) => 5,
Expand Down Expand Up @@ -361,9 +361,9 @@ impl TryFrom<u64> for CurrencyId {
let token_symbol = TokenSymbol::try_from(t_discr)?;

match c_discr {
0 => Ok(Self::Token(token_symbol)),
0 => Ok(Self::Native(token_symbol)),
1 => Ok(Self::VToken(token_symbol)),
2 => Ok(Self::Native(token_symbol)),
2 => Ok(Self::Token(token_symbol)),
3 => Ok(Self::Stable(token_symbol)),
4 => Ok(Self::VSToken(token_symbol)),
5 => Ok(Self::VSBond(token_symbol, pid, lp1, lp2)),
Expand Down
48 changes: 24 additions & 24 deletions node/primitives/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ fn currency_id_from_string_should_work() {

#[test]
fn currency_id_to_u64_should_work() {
let e00 = CurrencyId::Token(TokenSymbol::ASG);
let e01 = CurrencyId::Token(TokenSymbol::BNC);
let e02 = CurrencyId::Token(TokenSymbol::AUSD);
let e03 = CurrencyId::Token(TokenSymbol::DOT);
let e04 = CurrencyId::Token(TokenSymbol::KSM);
let e05 = CurrencyId::Token(TokenSymbol::ETH);
let e00 = CurrencyId::Native(TokenSymbol::ASG);
let e01 = CurrencyId::Native(TokenSymbol::BNC);
let e02 = CurrencyId::Native(TokenSymbol::AUSD);
let e03 = CurrencyId::Native(TokenSymbol::DOT);
let e04 = CurrencyId::Native(TokenSymbol::KSM);
let e05 = CurrencyId::Native(TokenSymbol::ETH);

assert_eq!(0x0000_0000_0000_0000, e00.currency_id());
assert_eq!(0x0001_0000_0000_0000, e01.currency_id());
Expand All @@ -58,12 +58,12 @@ fn currency_id_to_u64_should_work() {
assert_eq!(0x0104_0000_0000_0000, e14.currency_id());
assert_eq!(0x0105_0000_0000_0000, e15.currency_id());

let e20 = CurrencyId::Native(TokenSymbol::ASG);
let e21 = CurrencyId::Native(TokenSymbol::BNC);
let e22 = CurrencyId::Native(TokenSymbol::AUSD);
let e23 = CurrencyId::Native(TokenSymbol::DOT);
let e24 = CurrencyId::Native(TokenSymbol::KSM);
let e25 = CurrencyId::Native(TokenSymbol::ETH);
let e20 = CurrencyId::Token(TokenSymbol::ASG);
let e21 = CurrencyId::Token(TokenSymbol::BNC);
let e22 = CurrencyId::Token(TokenSymbol::AUSD);
let e23 = CurrencyId::Token(TokenSymbol::DOT);
let e24 = CurrencyId::Token(TokenSymbol::KSM);
let e25 = CurrencyId::Token(TokenSymbol::ETH);

assert_eq!(0x0200_0000_0000_0000, e20.currency_id());
assert_eq!(0x0201_0000_0000_0000, e21.currency_id());
Expand Down Expand Up @@ -117,12 +117,12 @@ fn currency_id_to_u64_should_work() {

#[test]
fn u64_to_currency_id_should_work() {
let e00 = CurrencyId::Token(TokenSymbol::ASG);
let e01 = CurrencyId::Token(TokenSymbol::BNC);
let e02 = CurrencyId::Token(TokenSymbol::AUSD);
let e03 = CurrencyId::Token(TokenSymbol::DOT);
let e04 = CurrencyId::Token(TokenSymbol::KSM);
let e05 = CurrencyId::Token(TokenSymbol::ETH);
let e00 = CurrencyId::Native(TokenSymbol::ASG);
let e01 = CurrencyId::Native(TokenSymbol::BNC);
let e02 = CurrencyId::Native(TokenSymbol::AUSD);
let e03 = CurrencyId::Native(TokenSymbol::DOT);
let e04 = CurrencyId::Native(TokenSymbol::KSM);
let e05 = CurrencyId::Native(TokenSymbol::ETH);

assert_eq!(e00, CurrencyId::try_from(0x0000_0000_0000_0000).unwrap());
assert_eq!(e01, CurrencyId::try_from(0x0001_0000_0000_0000).unwrap());
Expand All @@ -145,12 +145,12 @@ fn u64_to_currency_id_should_work() {
assert_eq!(e14, CurrencyId::try_from(0x0104_0000_0000_0000).unwrap());
assert_eq!(e15, CurrencyId::try_from(0x0105_0000_0000_0000).unwrap());

let e20 = CurrencyId::Native(TokenSymbol::ASG);
let e21 = CurrencyId::Native(TokenSymbol::BNC);
let e22 = CurrencyId::Native(TokenSymbol::AUSD);
let e23 = CurrencyId::Native(TokenSymbol::DOT);
let e24 = CurrencyId::Native(TokenSymbol::KSM);
let e25 = CurrencyId::Native(TokenSymbol::ETH);
let e20 = CurrencyId::Token(TokenSymbol::ASG);
let e21 = CurrencyId::Token(TokenSymbol::BNC);
let e22 = CurrencyId::Token(TokenSymbol::AUSD);
let e23 = CurrencyId::Token(TokenSymbol::DOT);
let e24 = CurrencyId::Token(TokenSymbol::KSM);
let e25 = CurrencyId::Token(TokenSymbol::ETH);

assert_eq!(e20, CurrencyId::try_from(0x0200_0000_0000_0000).unwrap());
assert_eq!(e21, CurrencyId::try_from(0x0201_0000_0000_0000).unwrap());
Expand Down
77 changes: 77 additions & 0 deletions pallets/flexible-fee/src/fee_dealer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// This file is part of Bifrost.

// Copyright (C) 2019-2021 Liebi Technologies (UK) Ltd.
// 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/>.

// The swap pool algorithm implements Balancer protocol
// For more details, refer to https://balancer.finance/whitepaper/

#![cfg_attr(not(feature = "std"), no_std)]

use sp_runtime::traits::UniqueSaturatedFrom;

use super::*;
use crate::Config;

pub trait FeeDealer<AccountId, Balance> {
fn ensure_can_charge_fee(
who: &AccountId,
fee: Balance,
reason: WithdrawReasons,
) -> DispatchResult;
}

pub struct FixedCurrencyFeeRate<T: Config>(PhantomData<T>);

impl<T: Config> FeeDealer<T::AccountId, PalletBalanceOf<T>> for FixedCurrencyFeeRate<T> {
/// Make sure there are enough BNC to be deducted if the user has assets in other form of tokens
/// rather than BNC.
fn ensure_can_charge_fee(
who: &T::AccountId,
fee: PalletBalanceOf<T>,
_reason: WithdrawReasons,
) -> DispatchResult {
let fee_currency_id: CurrencyId = T::AlternativeFeeCurrencyId::get();
let (fee_currency_base, native_currency_base): (u32, u32) =
T::AltFeeCurrencyExchangeRate::get();

let fee_currency_balance = T::MultiCurrency::free_balance(fee_currency_id, who);

let consume_fee_currency_amount =
fee.saturating_mul(fee_currency_base.into()) / native_currency_base.into();
ensure!(
consume_fee_currency_amount <=
PalletBalanceOf::<T>::unique_saturated_from(fee_currency_balance.into()),
Error::<T>::NotEnoughBalance
);

// deduct fee currency and increase native currency amount
// This withdraw operation allows death. So it will succeed given the remaining amount less
// than the existential deposit.
T::MultiCurrency::withdraw(
fee_currency_id,
who,
T::Balance::from(consume_fee_currency_amount),
)?;
T::MultiCurrency::deposit(T::NativeCurrencyId::get(), who, T::Balance::from(fee))?;

crate::Pallet::<T>::deposit_event(Event::FixedRateFeeExchanged(
fee_currency_id,
consume_fee_currency_amount,
));
Ok(())
}
}
Loading