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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crate::{AccountId, Amount, AssetId, Balance, Currencies, NativeAssetId, Runtime, NATIVE_EXISTENTIAL_DEPOSIT};
use sp_std::prelude::*;
use frame_benchmarking::{account, whitelisted_caller};
use frame_system::RawOrigin;
use sp_runtime::traits::UniqueSaturatedInto;
use frame_benchmarking::BenchmarkError;
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;
const NATIVE: AssetId = NativeAssetId::get();
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 set_balance(currency_id: AssetId, who: &AccountId, balance: Balance) {
assert_ok!(<Currencies as MultiCurrencyExtended<_>>::update_balance(
currency_id,
who,
balance.saturated_into()
));
}
runtime_benchmarks! {
{ Runtime, pallet_currencies }
transfer_non_native_currency {
let amount: Balance = 1_000 * BSX;
let from: AccountId = whitelisted_caller();
let asset_id = register_asset(b"TST".to_vec(), 1u128).map_err(|_| BenchmarkError::Stop("Failed to register asset"))?;
set_balance(asset_id, &from, amount);
let to: AccountId = account("to", 0, SEED);
let to_lookup = lookup_of_account(to.clone());
}: transfer(RawOrigin::Signed(from), to_lookup, asset_id, amount)
verify {
assert_eq!(<Currencies as MultiCurrency<_>>::total_balance(asset_id, &to), amount);
}
#[extra]
transfer_native_currency_worst_case {
let existential_deposit = NATIVE_EXISTENTIAL_DEPOSIT;
let amount: Balance = existential_deposit.saturating_mul(1000);
let from: AccountId = whitelisted_caller();
set_balance(NATIVE, &from, amount);
let to: AccountId = account("to", 0, SEED);
let to_lookup = lookup_of_account(to.clone());
}: transfer(RawOrigin::Signed(from), to_lookup, NATIVE, amount)
verify {
assert_eq!(<Currencies as MultiCurrency<_>>::total_balance(NATIVE, &to), amount);
}
transfer_native_currency {
let existential_deposit = NATIVE_EXISTENTIAL_DEPOSIT;
let amount: Balance = existential_deposit.saturating_mul(1000);
let from: AccountId = whitelisted_caller();
set_balance(NATIVE, &from, amount);
let to: AccountId = account("to", 0, SEED);
let to_lookup = lookup_of_account(to.clone());
}: _(RawOrigin::Signed(from), to_lookup, amount)
verify {
assert_eq!(<Currencies as MultiCurrency<_>>::total_balance(NATIVE, &to), amount);
}
update_balance_non_native_currency {
let balance: Balance = 2 * BSX;
let amount: Amount = balance.unique_saturated_into();
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"))?;
}: update_balance(RawOrigin::Root, who_lookup, asset_id, amount)
verify {
assert_eq!(<Currencies as MultiCurrency<_>>::total_balance(asset_id, &who), balance);
}
update_balance_native_currency_creating {
let existential_deposit = NATIVE_EXISTENTIAL_DEPOSIT;
let balance: Balance = existential_deposit.saturating_mul(1000);
let amount: Amount = balance.unique_saturated_into();
let who: AccountId = account("who", 0, SEED);
let who_lookup = lookup_of_account(who.clone());
}: update_balance(RawOrigin::Root, who_lookup, NATIVE, amount)
verify {
assert_eq!(<Currencies as MultiCurrency<_>>::total_balance(NATIVE, &who), balance);
}
update_balance_native_currency_killing {
let existential_deposit = NATIVE_EXISTENTIAL_DEPOSIT;
let balance: Balance = existential_deposit.saturating_mul(1000);
let amount: Amount = balance.unique_saturated_into();
let who: AccountId = account("who", 0, SEED);
let who_lookup = lookup_of_account(who.clone());
set_balance(NATIVE, &who, balance);
}: update_balance(RawOrigin::Root, who_lookup, NATIVE, -amount)
verify {
assert_eq!(<Currencies as MultiCurrency<_>>::free_balance(NATIVE, &who), 0);
}
}
#[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(),);
}