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
138
139
140
141
142
143
144
145
146
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::{account, benchmarks};
use frame_system::RawOrigin;
use sp_std::prelude::*;
use crate::Pallet as XYK;
use primitives::{AssetId, Balance};
const SEED: u32 = 1;
fn funded_account<T: Config>(name: &'static str, index: u32) -> T::AccountId {
let caller: T::AccountId = account(name, index, SEED);
T::Currency::update_balance(1, &caller, 1_000_000_000_000_000).unwrap();
T::Currency::update_balance(2, &caller, 1_000_000_000_000_000).unwrap();
caller
}
benchmarks! {
create_pool {
let caller = funded_account::<T>("caller", 0);
let asset_a: AssetId = 1;
let asset_b: AssetId = 2;
let amount_a : Balance = 10 * 1_000_000_000;
let amount_b : Balance = 20 * 1_000_000_000;
}: _(RawOrigin::Signed(caller.clone()), asset_a, amount_a, asset_b, amount_b)
verify {
assert_eq!(T::Currency::free_balance(asset_a, &caller), 999990000000000);
}
add_liquidity {
let maker = funded_account::<T>("maker", 0);
let caller = funded_account::<T>("caller", 0);
let asset_a: AssetId = 1;
let asset_b: AssetId = 2;
let amount : Balance = 10 * 1_000_000_000;
let max_limit : Balance = 10 * 1_000_000_000_000;
XYK::<T>::create_pool(RawOrigin::Signed(maker).into(), asset_a, 1_000_000_000,asset_b, 1_000_000_000)?;
}: _(RawOrigin::Signed(caller.clone()), asset_a, asset_b, amount, max_limit)
verify {
assert_eq!(T::Currency::free_balance(asset_a, &caller), 999990000000000);
assert_eq!(T::Currency::free_balance(asset_b, &caller), 999990000000000 - 1); }
remove_liquidity {
let maker = funded_account::<T>("maker", 0);
let caller = funded_account::<T>("caller", 0);
let asset_a: AssetId = 1;
let asset_b: AssetId = 2;
let amount : Balance = 1_000_000_000;
XYK::<T>::create_pool(RawOrigin::Signed(maker).into(), 1, 10_000_000_000, 2, 20_000_000_000)?;
XYK::<T>::add_liquidity(RawOrigin::Signed(caller.clone()).into(), 1, 2, 5_000_000_000, 10_100_000_000)?;
assert_eq!(T::Currency::free_balance(asset_a, &caller), 999995000000000);
assert_eq!(T::Currency::free_balance(asset_b, &caller), 999990000000000 - 1);}: _(RawOrigin::Signed(caller.clone()), asset_a, asset_b, amount)
verify {
assert_eq!(T::Currency::free_balance(asset_a, &caller), 999996000000000);
assert_eq!(T::Currency::free_balance(asset_b, &caller), 999992000000000 - 1);}
sell {
let maker = funded_account::<T>("maker", 0);
let caller = funded_account::<T>("caller", 0);
let asset_a: AssetId = 1;
let asset_b: AssetId = 2;
let amount : Balance = 1_000_000_000;
let discount = false;
let min_bought: Balance = 10 * 1_000;
XYK::<T>::create_pool(RawOrigin::Signed(maker).into(), asset_a, 1_000_000_000_000, asset_b, 3_000_000_000_000)?;
}: _(RawOrigin::Signed(caller.clone()), asset_a, asset_b, amount, min_bought, discount)
verify{
assert_eq!(T::Currency::free_balance(asset_a, &caller), 999999000000000);
assert_eq!(T::Currency::free_balance(asset_b, &caller), 1000002991008993);
}
buy {
let maker = funded_account::<T>("maker", 0);
let caller = funded_account::<T>("caller", 0);
let asset_a: AssetId = 1;
let asset_b: AssetId = 2;
let amount : Balance = 1_000_000_000;
let discount = false;
let max_sold: Balance = 6_000_000_000;
XYK::<T>::create_pool(RawOrigin::Signed(maker).into(), asset_a, 1_000_000_000_000, asset_b, 3_000_000_000_000)?;
}: _(RawOrigin::Signed(caller.clone()), asset_a, asset_b, amount, max_sold, discount)
verify{
assert_eq!(T::Currency::free_balance(asset_a, &caller), 1000001000000000);
assert_eq!(T::Currency::free_balance(asset_b, &caller), 999996990990990);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::mock::{ExtBuilder, System, Test};
use frame_support::assert_ok;
#[test]
fn test_benchmarks() {
ExtBuilder::default().build().execute_with(|| {
System::set_block_number(1);
assert_ok!(Pallet::<Test>::test_benchmark_create_pool());
assert_ok!(Pallet::<Test>::test_benchmark_add_liquidity());
assert_ok!(Pallet::<Test>::test_benchmark_remove_liquidity());
assert_ok!(Pallet::<Test>::test_benchmark_sell());
assert_ok!(Pallet::<Test>::test_benchmark_buy());
});
}
}