Skip to content

Commit 7bda6fe

Browse files
authored
Ksm fee (#197)
* swap Native CurrencyId index(2) to index(0) with Token CurrencyId * - Fix Flexible Fee Module unit tests - Wrap ensure_can_charge_fee function into FeeDealer trait * fix runtime errors caused by native currencyId index change * unitests done with flexible-fee module * cargo +nightly fmt --all
1 parent 76d638b commit 7bda6fe

File tree

7 files changed

+380
-148
lines changed

7 files changed

+380
-148
lines changed

node/primitives/src/currency.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ macro_rules! create_currency_id {
7575
type Error = ();
7676
fn try_from(id: CurrencyId) -> Result<AssetId, ()> {
7777
let _index = match id {
78-
$(CurrencyId::Token(TokenSymbol::$symbol) => Ok((0_u32, TokenSymbol::$symbol as u32)),)*
78+
$(CurrencyId::Native(TokenSymbol::$symbol) => Ok((0_u32, TokenSymbol::$symbol as u32)),)*
7979
$(CurrencyId::VToken(TokenSymbol::$symbol) => Ok((1_u32, TokenSymbol::$symbol as u32)),)*
80-
$(CurrencyId::Native(TokenSymbol::$symbol) => Ok((2_u32, TokenSymbol::$symbol as u32)),)*
80+
$(CurrencyId::Token(TokenSymbol::$symbol) => Ok((2_u32, TokenSymbol::$symbol as u32)),)*
8181
$(CurrencyId::Stable(TokenSymbol::$symbol) => Ok((3_u32, TokenSymbol::$symbol as u32)),)*
8282
$(CurrencyId::VSToken(TokenSymbol::$symbol) => Ok((4_u32, TokenSymbol::$symbol as u32)),)*
8383
_ => Err(()),
@@ -110,9 +110,9 @@ macro_rules! create_currency_id {
110110
_ => Err(()),
111111
};
112112
match c_discr {
113-
0 => Ok(CurrencyId::Token(token_symbol?)),
113+
0 => Ok(CurrencyId::Native(token_symbol?)),
114114
1 => Ok(CurrencyId::VToken(token_symbol?)),
115-
2 => Ok(CurrencyId::Native(token_symbol?)),
115+
2 => Ok(CurrencyId::Token(token_symbol?)),
116116
3 => Ok(CurrencyId::Stable(token_symbol?)),
117117
4 => Ok(CurrencyId::VSToken(token_symbol?)),
118118
_ => Err(()),
@@ -245,9 +245,9 @@ impl Default for TokenSymbol {
245245
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
246246
#[non_exhaustive]
247247
pub enum CurrencyId {
248-
Token(TokenSymbol),
249-
VToken(TokenSymbol),
250248
Native(TokenSymbol),
249+
VToken(TokenSymbol),
250+
Token(TokenSymbol),
251251
Stable(TokenSymbol),
252252
VSToken(TokenSymbol),
253253
VSBond(TokenSymbol, ParaId, LeasePeriod, LeasePeriod),
@@ -289,9 +289,9 @@ impl CurrencyId {
289289

290290
pub fn discriminant(&self) -> u8 {
291291
match *self {
292-
Self::Token(..) => 0,
292+
Self::Native(..) => 0,
293293
Self::VToken(..) => 1,
294-
Self::Native(..) => 2,
294+
Self::Token(..) => 2,
295295
Self::Stable(..) => 3,
296296
Self::VSToken(..) => 4,
297297
Self::VSBond(..) => 5,
@@ -361,9 +361,9 @@ impl TryFrom<u64> for CurrencyId {
361361
let token_symbol = TokenSymbol::try_from(t_discr)?;
362362

363363
match c_discr {
364-
0 => Ok(Self::Token(token_symbol)),
364+
0 => Ok(Self::Native(token_symbol)),
365365
1 => Ok(Self::VToken(token_symbol)),
366-
2 => Ok(Self::Native(token_symbol)),
366+
2 => Ok(Self::Token(token_symbol)),
367367
3 => Ok(Self::Stable(token_symbol)),
368368
4 => Ok(Self::VSToken(token_symbol)),
369369
5 => Ok(Self::VSBond(token_symbol, pid, lp1, lp2)),

node/primitives/src/tests.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ fn currency_id_from_string_should_work() {
3030

3131
#[test]
3232
fn currency_id_to_u64_should_work() {
33-
let e00 = CurrencyId::Token(TokenSymbol::ASG);
34-
let e01 = CurrencyId::Token(TokenSymbol::BNC);
35-
let e02 = CurrencyId::Token(TokenSymbol::AUSD);
36-
let e03 = CurrencyId::Token(TokenSymbol::DOT);
37-
let e04 = CurrencyId::Token(TokenSymbol::KSM);
38-
let e05 = CurrencyId::Token(TokenSymbol::ETH);
33+
let e00 = CurrencyId::Native(TokenSymbol::ASG);
34+
let e01 = CurrencyId::Native(TokenSymbol::BNC);
35+
let e02 = CurrencyId::Native(TokenSymbol::AUSD);
36+
let e03 = CurrencyId::Native(TokenSymbol::DOT);
37+
let e04 = CurrencyId::Native(TokenSymbol::KSM);
38+
let e05 = CurrencyId::Native(TokenSymbol::ETH);
3939

4040
assert_eq!(0x0000_0000_0000_0000, e00.currency_id());
4141
assert_eq!(0x0001_0000_0000_0000, e01.currency_id());
@@ -58,12 +58,12 @@ fn currency_id_to_u64_should_work() {
5858
assert_eq!(0x0104_0000_0000_0000, e14.currency_id());
5959
assert_eq!(0x0105_0000_0000_0000, e15.currency_id());
6060

61-
let e20 = CurrencyId::Native(TokenSymbol::ASG);
62-
let e21 = CurrencyId::Native(TokenSymbol::BNC);
63-
let e22 = CurrencyId::Native(TokenSymbol::AUSD);
64-
let e23 = CurrencyId::Native(TokenSymbol::DOT);
65-
let e24 = CurrencyId::Native(TokenSymbol::KSM);
66-
let e25 = CurrencyId::Native(TokenSymbol::ETH);
61+
let e20 = CurrencyId::Token(TokenSymbol::ASG);
62+
let e21 = CurrencyId::Token(TokenSymbol::BNC);
63+
let e22 = CurrencyId::Token(TokenSymbol::AUSD);
64+
let e23 = CurrencyId::Token(TokenSymbol::DOT);
65+
let e24 = CurrencyId::Token(TokenSymbol::KSM);
66+
let e25 = CurrencyId::Token(TokenSymbol::ETH);
6767

6868
assert_eq!(0x0200_0000_0000_0000, e20.currency_id());
6969
assert_eq!(0x0201_0000_0000_0000, e21.currency_id());
@@ -117,12 +117,12 @@ fn currency_id_to_u64_should_work() {
117117

118118
#[test]
119119
fn u64_to_currency_id_should_work() {
120-
let e00 = CurrencyId::Token(TokenSymbol::ASG);
121-
let e01 = CurrencyId::Token(TokenSymbol::BNC);
122-
let e02 = CurrencyId::Token(TokenSymbol::AUSD);
123-
let e03 = CurrencyId::Token(TokenSymbol::DOT);
124-
let e04 = CurrencyId::Token(TokenSymbol::KSM);
125-
let e05 = CurrencyId::Token(TokenSymbol::ETH);
120+
let e00 = CurrencyId::Native(TokenSymbol::ASG);
121+
let e01 = CurrencyId::Native(TokenSymbol::BNC);
122+
let e02 = CurrencyId::Native(TokenSymbol::AUSD);
123+
let e03 = CurrencyId::Native(TokenSymbol::DOT);
124+
let e04 = CurrencyId::Native(TokenSymbol::KSM);
125+
let e05 = CurrencyId::Native(TokenSymbol::ETH);
126126

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

148-
let e20 = CurrencyId::Native(TokenSymbol::ASG);
149-
let e21 = CurrencyId::Native(TokenSymbol::BNC);
150-
let e22 = CurrencyId::Native(TokenSymbol::AUSD);
151-
let e23 = CurrencyId::Native(TokenSymbol::DOT);
152-
let e24 = CurrencyId::Native(TokenSymbol::KSM);
153-
let e25 = CurrencyId::Native(TokenSymbol::ETH);
148+
let e20 = CurrencyId::Token(TokenSymbol::ASG);
149+
let e21 = CurrencyId::Token(TokenSymbol::BNC);
150+
let e22 = CurrencyId::Token(TokenSymbol::AUSD);
151+
let e23 = CurrencyId::Token(TokenSymbol::DOT);
152+
let e24 = CurrencyId::Token(TokenSymbol::KSM);
153+
let e25 = CurrencyId::Token(TokenSymbol::ETH);
154154

155155
assert_eq!(e20, CurrencyId::try_from(0x0200_0000_0000_0000).unwrap());
156156
assert_eq!(e21, CurrencyId::try_from(0x0201_0000_0000_0000).unwrap());
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// This file is part of Bifrost.
2+
3+
// Copyright (C) 2019-2021 Liebi Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5+
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
// The swap pool algorithm implements Balancer protocol
20+
// For more details, refer to https://balancer.finance/whitepaper/
21+
22+
#![cfg_attr(not(feature = "std"), no_std)]
23+
24+
use sp_runtime::traits::UniqueSaturatedFrom;
25+
26+
use super::*;
27+
use crate::Config;
28+
29+
pub trait FeeDealer<AccountId, Balance> {
30+
fn ensure_can_charge_fee(
31+
who: &AccountId,
32+
fee: Balance,
33+
reason: WithdrawReasons,
34+
) -> DispatchResult;
35+
}
36+
37+
pub struct FixedCurrencyFeeRate<T: Config>(PhantomData<T>);
38+
39+
impl<T: Config> FeeDealer<T::AccountId, PalletBalanceOf<T>> for FixedCurrencyFeeRate<T> {
40+
/// Make sure there are enough BNC to be deducted if the user has assets in other form of tokens
41+
/// rather than BNC.
42+
fn ensure_can_charge_fee(
43+
who: &T::AccountId,
44+
fee: PalletBalanceOf<T>,
45+
_reason: WithdrawReasons,
46+
) -> DispatchResult {
47+
let fee_currency_id: CurrencyId = T::AlternativeFeeCurrencyId::get();
48+
let (fee_currency_base, native_currency_base): (u32, u32) =
49+
T::AltFeeCurrencyExchangeRate::get();
50+
51+
let fee_currency_balance = T::MultiCurrency::free_balance(fee_currency_id, who);
52+
53+
let consume_fee_currency_amount =
54+
fee.saturating_mul(fee_currency_base.into()) / native_currency_base.into();
55+
ensure!(
56+
consume_fee_currency_amount <=
57+
PalletBalanceOf::<T>::unique_saturated_from(fee_currency_balance.into()),
58+
Error::<T>::NotEnoughBalance
59+
);
60+
61+
// deduct fee currency and increase native currency amount
62+
// This withdraw operation allows death. So it will succeed given the remaining amount less
63+
// than the existential deposit.
64+
T::MultiCurrency::withdraw(
65+
fee_currency_id,
66+
who,
67+
T::Balance::from(consume_fee_currency_amount),
68+
)?;
69+
T::MultiCurrency::deposit(T::NativeCurrencyId::get(), who, T::Balance::from(fee))?;
70+
71+
crate::Pallet::<T>::deposit_event(Event::FixedRateFeeExchanged(
72+
fee_currency_id,
73+
consume_fee_currency_amount,
74+
));
75+
Ok(())
76+
}
77+
}

0 commit comments

Comments
 (0)