Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

333 extend the orml tokens pallet so users are able to mint and burn custom assets #359

Merged
24 changes: 24 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"pallets/parachain-staking",
"pallets/vesting-manager",
"pallets/orml-currencies-allowance-extension",
"pallets/orml-tokens-management-extension",
"runtime/common",
"runtime/amplitude",
"runtime/foucoco",
Expand Down
17 changes: 16 additions & 1 deletion pallets/orml-currencies-allowance-extension/src/mock.rs
gianfra-t marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,27 @@ impl ExtBuilder {
}
}

pub const USER1 = 1;
pub const USER2 = 2;
pub const USER2 = 3;

pub const USER_INITIAL_BALANCE = 100000;

pub fn run_test<T>(test: T)
where
T: FnOnce(),
{
ExtBuilder::build().execute_with(|| {
System::set_block_number(1);
test();

let mut storage = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();

pallet_balances::GenesisConfig::<Test> {
balances: vec![(USER1,USER_INITIAL_BALANCE),
(USER1,USER_INITIAL_BALANCE),
(USER1,USER_INITIAL_BALANCE)]
}
.assimilate_storage(&mut storage)
.unwrap();
});
}
61 changes: 61 additions & 0 deletions pallets/orml-tokens-management-extension/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[package]
authors = ["Pendulum Chain"]
edition = "2021"
name = "orml-tokens-management-extension"
version = "1.0.0"

[dependencies]
codec = {package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive", "max-encoded-len"]}
scale-info = {version = "2.2.0", default-features = false, features = ["derive"]}
serde = {version = "1.0.130", default-features = false, features = ["derive"], optional = true}
sha2 = {version = "0.8.2", default-features = false}

# Substrate dependencies
frame-support = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false}
frame-system = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false}
sp-core = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false}
sp-runtime = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false}
sp-std = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false}

frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false, optional = true }

orml-currencies = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.40", default-features = false }
orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.40", default-features = false }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v0.9.40", default-features = false }



[dev-dependencies]
mocktopus = "0.8.0"
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
sp-io = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false}

pallet-balances = {git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40"}

# Spacewalk libraries
spacewalk-primitives = { git = "https://github.com/pendulum-chain/spacewalk", default-features = false, rev = "d05b0015d15ca39cc780889bcc095335e9862a36"}


[features]
default = ["std"]
std = [
"serde",
"codec/std",
"sha2/std",
"sp-core/std",
"sp-std/std",
"sp-runtime/std",
"frame-support/std",
"frame-system/std",
"orml-currencies/std",
"orml-tokens/std",
"orml-traits/std",
"frame-benchmarking/std",
"spacewalk-primitives/std"
]

runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
100 changes: 100 additions & 0 deletions pallets/orml-tokens-management-extension/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#![allow(warnings)]
use super::{Pallet as TokenExtension, *};

use crate::types::{AccountIdOf, BalanceOf, CurrencyOf};
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use frame_support::assert_ok;
use frame_system::RawOrigin;
use orml_traits::{
arithmetic::{One, Zero},
MultiCurrency,
};
use sp_runtime::traits::Get;
use sp_std::prelude::*;

const AMOUNT_MINTED: u32 = 10000;
const AMOUNT_BURNED: u32 = 5000;

fn get_test_currency<T: Config>() -> CurrencyOf<T> {
<T as orml_currencies::Config>::GetNativeCurrencyId::get()
}

// mint some tokens to the account
fn set_up_account<T: Config>(account: &AccountIdOf<T>) {
let token_currency_id = get_test_currency::<T>();

assert_ok!(<orml_currencies::Pallet<T> as MultiCurrency<AccountIdOf<T>>>::deposit(
token_currency_id,
&account,
AMOUNT_MINTED.into()
));
}

benchmarks! {
create {
let token_currency_id = get_test_currency::<T>();
let test_account = account("Tester", 0, 0);
set_up_account::<T>(&test_account);
let origin = RawOrigin::Signed(test_account);
}: _(origin,token_currency_id)
verify {
assert!(crate::Pallet::<T>::currency_details(token_currency_id).is_some());
}

mint {
let token_currency_id = get_test_currency::<T>();
let test_account = account("Tester", 0, 0);
set_up_account::<T>(&test_account);
let origin = RawOrigin::Signed(test_account);
let destination = account::<AccountIdOf<T>>("Receiver", 0, 0);
assert_ok!(TokenExtension::<T>::create(origin.clone().into(), token_currency_id));

}: _(origin,token_currency_id, destination.clone(),AMOUNT_MINTED.into())
verify {
assert_eq!(<orml_currencies::Pallet<T> as MultiCurrency<AccountIdOf<T>>>::total_balance(token_currency_id, &destination), AMOUNT_MINTED.into());
}

burn {
let token_currency_id = get_test_currency::<T>();
let test_account = account("Tester", 0, 0);
set_up_account::<T>(&test_account);
let origin = RawOrigin::Signed(test_account);
let destination = account::<AccountIdOf<T>>("Receiver", 0, 0);
assert_ok!(TokenExtension::<T>::create(origin.clone().into(), token_currency_id));
assert_ok!(TokenExtension::<T>::mint(origin.clone().into(), token_currency_id, destination.clone(), AMOUNT_MINTED.into()));

}: _(origin,token_currency_id, destination.clone(),AMOUNT_BURNED.into())
verify {
assert_eq!(<orml_currencies::Pallet<T> as MultiCurrency<AccountIdOf<T>>>::total_balance(token_currency_id, &destination), (AMOUNT_MINTED-AMOUNT_BURNED).into());
}

transfer_ownership {
let token_currency_id = get_test_currency::<T>();
let test_account = account("Tester", 0, 0);
set_up_account::<T>(&test_account);
let origin = RawOrigin::Signed(test_account);
let new_owner = account::<AccountIdOf<T>>("NewOwner", 0, 0);
set_up_account::<T>(&new_owner);
assert_ok!(TokenExtension::<T>::create(origin.clone().into(), token_currency_id));

}: _(origin,token_currency_id, new_owner)
verify {
assert!(crate::Pallet::<T>::currency_details(token_currency_id).is_some());
}

set_managers {
let token_currency_id = get_test_currency::<T>();
let test_account = account("Tester", 0, 0);
set_up_account::<T>(&test_account);
let origin = RawOrigin::Signed(test_account);
let new_issuer = account::<AccountIdOf<T>>("Issuer", 0, 0);
let new_admin = account::<AccountIdOf<T>>("Admin", 0, 0);
assert_ok!(TokenExtension::<T>::create(origin.clone().into(), token_currency_id));

}: _(origin,token_currency_id, new_issuer, new_admin)
verify {
assert!(crate::Pallet::<T>::currency_details(token_currency_id).is_some());
}
}

impl_benchmark_test_suite!(TokenExtension, crate::mock::ExtBuilder::build(), crate::mock::Test);
Loading
Loading