1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use crate::{AccountId, AssetId, Balance, Runtime, Tokens};

use sp_std::prelude::*;

use frame_benchmarking::BenchmarkError;
use frame_benchmarking::{account, whitelisted_caller};
use frame_system::RawOrigin;

use frame_support::assert_ok;
use orml_benchmarking::runtime_benchmarks;
use orml_traits::MultiCurrency;
use orml_traits::MultiCurrencyExtended;

use super::*;

use sp_runtime::traits::{SaturatedConversion, StaticLookup};

const SEED: u32 = 0;

pub fn lookup_of_account(who: AccountId) -> <<Runtime as frame_system::Config>::Lookup as StaticLookup>::Source {
	<Runtime as frame_system::Config>::Lookup::unlookup(who)
}

pub fn update_balance(currency_id: AssetId, who: &AccountId, balance: Balance) {
	assert_ok!(<Tokens as MultiCurrencyExtended<_>>::update_balance(
		currency_id,
		who,
		balance.saturated_into()
	));
}

runtime_benchmarks! {
	{ Runtime, orml_tokens }

	transfer {
		let amount: Balance = BSX;

		let asset_id = register_asset(b"TST".to_vec(), 1u128).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?;

		let from: AccountId = whitelisted_caller();
		update_balance(asset_id, &from, amount);

		let to: AccountId = account("to", 0, SEED);
		let to_lookup = lookup_of_account(to.clone());
	}: _(RawOrigin::Signed(from), to_lookup, asset_id, amount)
	verify {
		assert_eq!(<Tokens as MultiCurrency<_>>::total_balance(asset_id, &to), amount);
	}

	transfer_all {
		let amount: Balance = BSX;

		let asset_id = register_asset(b"TST".to_vec(), 1u128).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?;

		let from: AccountId = whitelisted_caller();
		update_balance(asset_id, &from, amount);

		let to: AccountId = account("to", 0, SEED);
		let to_lookup = lookup_of_account(to);
	}: _(RawOrigin::Signed(from.clone()), to_lookup, asset_id, false)
	verify {
		assert_eq!(<Tokens as MultiCurrency<_>>::total_balance(asset_id, &from), 0);
	}

	transfer_keep_alive {
		let from: AccountId = whitelisted_caller();
		let asset_id = register_asset(b"TST".to_vec(), 1u128).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?;
		update_balance(asset_id, &from, 2 * BSX);

		let to: AccountId = account("to", 0, SEED);
		let to_lookup = lookup_of_account(to.clone());
	}: _(RawOrigin::Signed(from), to_lookup, asset_id, BSX)
	verify {
		assert_eq!(<Tokens as MultiCurrency<_>>::total_balance(asset_id, &to), BSX);
	}

	force_transfer {
		let from: AccountId = account("from", 0, SEED);
		let from_lookup = lookup_of_account(from.clone());
		let asset_id = register_asset(b"TST".to_vec(), 1u128).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?;
		update_balance(asset_id, &from, 2 * BSX);

		let to: AccountId = account("to", 0, SEED);
		let to_lookup = lookup_of_account(to.clone());
	}: _(RawOrigin::Root, from_lookup, to_lookup, asset_id, BSX)
	verify {
		assert_eq!(<Tokens as MultiCurrency<_>>::total_balance(asset_id, &to), BSX);
	}

	set_balance {
		let who: AccountId = account("who", 0, SEED);
		let who_lookup = lookup_of_account(who.clone());

		let asset_id = register_asset(b"TST".to_vec(), 1u128).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?;

	}: _(RawOrigin::Root, who_lookup, asset_id, BSX, BSX)
	verify {
		assert_eq!(<Tokens as MultiCurrency<_>>::total_balance(asset_id, &who), 2 * BSX);
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use orml_benchmarking::impl_benchmark_test_suite;

	fn new_test_ext() -> sp_io::TestExternalities {
		frame_system::GenesisConfig::default()
			.build_storage::<crate::Runtime>()
			.unwrap()
			.into()
	}

	impl_benchmark_test_suite!(new_test_ext(),);
}