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
1 change: 1 addition & 0 deletions Cargo.lock

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

14 changes: 1 addition & 13 deletions node/service/src/chain_spec/asgard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ fn development_config_genesis(id: ParaId) -> GenesisConfig {
.flat_map(|x| {
vec![
(x.clone(), CurrencyId::Stable(TokenSymbol::KUSD), ENDOWMENT * 10_000),
(x.clone(), CurrencyId::Token(TokenSymbol::DOT), ENDOWMENT * 4_000_000),
(x.clone(), CurrencyId::VSToken(TokenSymbol::DOT), ENDOWMENT * 4_000_000),
(x.clone(), CurrencyId::Token(TokenSymbol::ETH), ENDOWMENT),
(x.clone(), CurrencyId::Token(TokenSymbol::KSM), ENDOWMENT),
]
})
Expand Down Expand Up @@ -218,17 +215,8 @@ fn local_config_genesis(id: ParaId) -> GenesisConfig {
.flat_map(|x| {
vec![
(x.clone(), CurrencyId::Stable(TokenSymbol::KUSD), ENDOWMENT * 4_000_000),
(x.clone(), CurrencyId::VSToken(TokenSymbol::DOT), ENDOWMENT * 4_000_000),
(x.clone(), CurrencyId::VToken(TokenSymbol::DOT), ENDOWMENT * 4_000_000),
(x.clone(), CurrencyId::Token(TokenSymbol::DOT), ENDOWMENT * 4_000_000),
(x.clone(), CurrencyId::Token(TokenSymbol::ETH), ENDOWMENT * 4_000_000),
(x.clone(), CurrencyId::Token(TokenSymbol::KSM), ENDOWMENT * 4_000_000),
(x.clone(), CurrencyId::Token(TokenSymbol::ASG), ENDOWMENT * 4_000_000),
(
x.clone(),
CurrencyId::VSBond(TokenSymbol::KSM, 3000, 13, 20),
ENDOWMENT * 4_000_000,
),
(x.clone(), CurrencyId::Native(TokenSymbol::ASG), ENDOWMENT * 4_000_000),
]
})
.collect();
Expand Down
7 changes: 7 additions & 0 deletions pallets/token-issuer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10", default-features = false, optional = true }
node-primitives = { path = "../../node/primitives", default-features = false }
orml-traits = { version = "0.4.1-dev", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.10", default-features = false }
Expand All @@ -34,4 +35,10 @@ std = [
"orml-tokens/std",
"orml-currencies/std",
"pallet-balances/std"
]

runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
106 changes: 106 additions & 0 deletions pallets/token-issuer/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// 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/>.

#![cfg(feature = "runtime-benchmarks")]

use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_support::{dispatch::UnfilteredDispatchable, sp_runtime::traits::UniqueSaturatedFrom};
use frame_system::RawOrigin;
use node_primitives::{CurrencyId, TokenSymbol};

use super::*;
#[allow(unused_imports)]
use crate::Pallet as TokenIssuer;

benchmarks! {
add_to_issue_whitelist {
let origin = T::ControlOrigin::successful_origin();
let account: T::AccountId = whitelisted_caller();
let currency_id = CurrencyId::Token(TokenSymbol::KSM);
let call = Call::<T>::add_to_issue_whitelist(currency_id, account);
}: {call.dispatch_bypass_filter(origin)?}

remove_from_issue_whitelist {
let origin = T::ControlOrigin::successful_origin();
let account: T::AccountId = whitelisted_caller();
let currency_id = CurrencyId::Token(TokenSymbol::KSM);
let add_call = Call::<T>::add_to_issue_whitelist(currency_id, account.clone());
add_call.dispatch_bypass_filter(origin.clone())?;

let remove_call = Call::<T>::remove_from_issue_whitelist(currency_id, account);
}: {remove_call.dispatch_bypass_filter(origin)?}

add_to_transfer_whitelist {
let origin = T::ControlOrigin::successful_origin();
let account: T::AccountId = whitelisted_caller();
let currency_id = CurrencyId::Token(TokenSymbol::KSM);
let call = Call::<T>::add_to_transfer_whitelist(currency_id, account);
}: {call.dispatch_bypass_filter(origin)?}

remove_from_transfer_whitelist {
let origin = T::ControlOrigin::successful_origin();
let account: T::AccountId = whitelisted_caller();
let currency_id = CurrencyId::Token(TokenSymbol::KSM);
let add_call = Call::<T>::add_to_transfer_whitelist(currency_id, account.clone());
add_call.dispatch_bypass_filter(origin.clone())?;

let remove_call = Call::<T>::remove_from_transfer_whitelist(currency_id, account);
}: {remove_call.dispatch_bypass_filter(origin)?}

issue {
let origin = T::ControlOrigin::successful_origin();
let caller: T::AccountId = whitelisted_caller();
let currency_id = CurrencyId::Token(TokenSymbol::KSM);
let add_call = Call::<T>::add_to_issue_whitelist(currency_id.clone(), caller.clone());
add_call.dispatch_bypass_filter(origin.clone())?;

let original_balance = T::MultiCurrency::free_balance(currency_id.clone(), &caller);
let token_amount = BalanceOf::<T>::unique_saturated_from(1000u32 as u128);
}: _(RawOrigin::Signed(caller.clone()), caller.clone(), currency_id.clone(), token_amount)
verify {
assert_eq!(T::MultiCurrency::free_balance(currency_id.clone(), &caller), token_amount + original_balance);
}

transfer {
let origin = T::ControlOrigin::successful_origin();
let caller: T::AccountId = whitelisted_caller();
let currency_id = CurrencyId::Token(TokenSymbol::KSM);

// add caller to the transfer whitelist
let add_transfer_call = Call::<T>::add_to_transfer_whitelist(currency_id.clone(), caller.clone());
add_transfer_call.dispatch_bypass_filter(origin.clone())?;

// transfer some ksm from caller account to receiver account
let receiver: T::AccountId = account("bechmarking_account_1", 0, 0);
let transfer_token_amount = BalanceOf::<T>::unique_saturated_from(800u32 as u128);
let caller_original_balance = T::MultiCurrency::free_balance(currency_id.clone(), &caller);
let receiver_original_balance = T::MultiCurrency::free_balance(currency_id.clone(), &receiver);
}: _(RawOrigin::Signed(caller.clone()), receiver.clone(), currency_id.clone(), transfer_token_amount)
verify {
assert_eq!(T::MultiCurrency::free_balance(currency_id.clone(), &caller), caller_original_balance - transfer_token_amount);
assert_eq!(T::MultiCurrency::free_balance(currency_id.clone(), &receiver), transfer_token_amount+ receiver_original_balance);
}
}

impl_benchmark_test_suite!(
TokenIssuer,
crate::mock::ExtBuilder::default()
// .one_hundred_precision_for_each_currency_type_for_whitelist_account()
.build(),
crate::mock::Test
);
55 changes: 28 additions & 27 deletions pallets/token-issuer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,22 @@ extern crate alloc;
use alloc::vec::Vec;

use frame_support::{ensure, pallet_prelude::*, transactional};
use frame_system::{pallet_prelude::*, WeightInfo};
use frame_system::pallet_prelude::*;
use node_primitives::CurrencyId;
use orml_traits::MultiCurrency;
pub use weights::WeightInfo;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
mod mock;
mod tests;
mod weights;

pub use pallet::*;

type BalanceOf<T> = <<T as Config>::MultiCurrency as MultiCurrency<
<T as frame_system::Config>::AccountId,
>>::Balance;
type CurrencyIdOf<T> = <<T as Config>::MultiCurrency as MultiCurrency<
<T as frame_system::Config>::AccountId,
>>::CurrencyId;
type AccountIdOf<T> = <T as frame_system::Config>::AccountId;

#[frame_support::pallet]
Expand All @@ -50,7 +52,7 @@ pub mod pallet {
pub trait Config: frame_system::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
/// Currecny operation handler
type MultiCurrency: MultiCurrency<AccountIdOf<Self>>;
type MultiCurrency: MultiCurrency<AccountIdOf<Self>, CurrencyId = CurrencyId>;

/// The only origin that can edit token issuer list
type ControlOrigin: EnsureOrigin<Self::Origin>;
Expand All @@ -70,34 +72,33 @@ pub mod pallet {
}

#[pallet::event]
#[pallet::metadata(BalanceOf<T> = "Balance", CurrencyIdOf<T> = "CurrencyId")]
#[pallet::metadata(BalanceOf<T> = "Balance")]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
/// Successful added a new account to the issue whitelist. \[account, currency_id]\
AddedToIssueList(T::AccountId, CurrencyIdOf<T>),
AddedToIssueList(T::AccountId, CurrencyId),
/// Successful remove an account from the issue whitelist. \[account, currency_id]\
RemovedFromIssueList(T::AccountId, CurrencyIdOf<T>),
RemovedFromIssueList(T::AccountId, CurrencyId),
/// Successful added a new account to the transfer whitelist. \[account, currency_id]\
AddedToTransferList(T::AccountId, CurrencyIdOf<T>),
AddedToTransferList(T::AccountId, CurrencyId),
/// Successful remove an account from the transfer whitelist. \[account, currency_id]\
RemovedFromTransferList(T::AccountId, CurrencyIdOf<T>),
RemovedFromTransferList(T::AccountId, CurrencyId),
/// Token issue success, \[currency_id, dest, amount\]
Issued(T::AccountId, CurrencyIdOf<T>, BalanceOf<T>),
Issued(T::AccountId, CurrencyId, BalanceOf<T>),
/// Token transferred success, \[origin, dest, currency_id, amount\]
Transferred(T::AccountId, T::AccountId, CurrencyIdOf<T>, BalanceOf<T>),
Transferred(T::AccountId, T::AccountId, CurrencyId, BalanceOf<T>),
}

/// Accounts in the whitelist can issue the corresponding Currency.
#[pallet::storage]
#[pallet::getter(fn get_issue_whitelist)]
pub type IssueWhiteList<T> =
StorageMap<_, Blake2_128Concat, CurrencyIdOf<T>, Vec<AccountIdOf<T>>>;
pub type IssueWhiteList<T> = StorageMap<_, Blake2_128Concat, CurrencyId, Vec<AccountIdOf<T>>>;

/// Accounts in the whitelist can transfer the corresponding Currency.
#[pallet::storage]
#[pallet::getter(fn get_transfer_whitelist)]
pub type TransferWhiteList<T> =
StorageMap<_, Blake2_128Concat, CurrencyIdOf<T>, Vec<AccountIdOf<T>>>;
StorageMap<_, Blake2_128Concat, CurrencyId, Vec<AccountIdOf<T>>>;

#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
Expand All @@ -107,11 +108,11 @@ pub mod pallet {

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(1000)]
#[pallet::weight(T::WeightInfo::add_to_issue_whitelist())]
#[transactional]
pub fn add_to_issue_whitelist(
origin: OriginFor<T>,
currency_id: CurrencyIdOf<T>,
currency_id: CurrencyId,
account: AccountIdOf<T>,
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;
Expand All @@ -135,11 +136,11 @@ pub mod pallet {
Ok(())
}

#[pallet::weight(1000)]
#[pallet::weight(T::WeightInfo::remove_from_issue_whitelist())]
#[transactional]
pub fn remove_from_issue_whitelist(
origin: OriginFor<T>,
currency_id: CurrencyIdOf<T>,
currency_id: CurrencyId,
account: AccountIdOf<T>,
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;
Expand All @@ -158,11 +159,11 @@ pub mod pallet {
Ok(())
}

#[pallet::weight(1000)]
#[pallet::weight(T::WeightInfo::add_to_transfer_whitelist())]
#[transactional]
pub fn add_to_transfer_whitelist(
origin: OriginFor<T>,
currency_id: CurrencyIdOf<T>,
currency_id: CurrencyId,
account: AccountIdOf<T>,
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;
Expand All @@ -189,11 +190,11 @@ pub mod pallet {
Ok(())
}

#[pallet::weight(1000)]
#[pallet::weight(T::WeightInfo::remove_from_transfer_whitelist())]
#[transactional]
pub fn remove_from_transfer_whitelist(
origin: OriginFor<T>,
currency_id: CurrencyIdOf<T>,
currency_id: CurrencyId,
account: AccountIdOf<T>,
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;
Expand All @@ -218,12 +219,12 @@ pub mod pallet {
Ok(())
}

#[pallet::weight(1000)]
#[pallet::weight(T::WeightInfo::issue())]
#[transactional]
pub fn issue(
origin: OriginFor<T>,
dest: AccountIdOf<T>,
currency_id: CurrencyIdOf<T>,
currency_id: CurrencyId,
#[pallet::compact] amount: BalanceOf<T>,
) -> DispatchResult {
let issuer = ensure_signed(origin)?;
Expand All @@ -242,12 +243,12 @@ pub mod pallet {
///
/// The dispatch origin for this call must be `Root` by the
/// transactor.
#[pallet::weight(1000)]
#[pallet::weight(T::WeightInfo::transfer())]
#[transactional]
pub fn transfer(
origin: OriginFor<T>,
dest: AccountIdOf<T>,
currency_id: CurrencyIdOf<T>,
currency_id: CurrencyId,
#[pallet::compact] amount: BalanceOf<T>,
) -> DispatchResult {
let transferrer = ensure_signed(origin)?;
Expand Down
Loading