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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::{account, benchmarks};
use frame_system::RawOrigin;
use primitives::AssetId;
use crate::Pallet as LBP;
const SEED: u32 = 1;
const ASSET_HDX: AssetId = 0;
const ASSET_A_ID: AssetId = 1;
const ASSET_B_ID: AssetId = 2;
const ASSET_A_AMOUNT: Balance = 1_000_000_000;
const ASSET_B_AMOUNT: Balance = 2_000_000_000;
const INITIAL_WEIGHT: LBPWeight = 20_000_000;
const FINAL_WEIGHT: LBPWeight = 90_000_000;
const DEFAULT_FEE: (u32, u32) = (2, 1_000);
fn funded_account<T: Config>(name: &'static str, index: u32) -> T::AccountId {
let caller: T::AccountId = account(name, index, SEED);
T::MultiCurrency::update_balance(ASSET_HDX, &caller, 1_000_000_000_000_000).unwrap();
T::MultiCurrency::update_balance(ASSET_A_ID, &caller, 1_000_000_000_000_000).unwrap();
T::MultiCurrency::update_balance(ASSET_B_ID, &caller, 1_000_000_000_000_000).unwrap();
caller
}
benchmarks! {
create_pool {
let caller = funded_account::<T>("caller", 0);
let pool_id = LBP::<T>::pair_account_from_assets(ASSET_A_ID, ASSET_B_ID);
}: _(RawOrigin::Root, caller.clone(), ASSET_A_ID, ASSET_A_AMOUNT, ASSET_B_ID, ASSET_B_AMOUNT, INITIAL_WEIGHT, FINAL_WEIGHT, WeightCurveType::Linear, DEFAULT_FEE, caller, 0)
verify {
assert!(PoolData::<T>::contains_key(&pool_id));
}
update_pool_data {
let caller = funded_account::<T>("caller", 0);
let fee_collector = funded_account::<T>("fee_collector", 0);
let pool_id = LBP::<T>::pair_account_from_assets(ASSET_A_ID, ASSET_B_ID);
let new_start = Some(T::BlockNumber::from(50_u32));
let new_end = Some(T::BlockNumber::from(100_u32));
let new_initial_weight = 45_250_600;
let new_final_weight = 55_250_600;
let fee = (5, 1000);
LBP::<T>::create_pool(RawOrigin::Root.into(), caller.clone(), ASSET_A_ID, ASSET_A_AMOUNT, ASSET_B_ID, ASSET_B_AMOUNT, INITIAL_WEIGHT, FINAL_WEIGHT, WeightCurveType::Linear, fee, caller.clone(), 0)?;
ensure!(PoolData::<T>::contains_key(&pool_id), "Pool does not exist.");
}: _(RawOrigin::Signed(caller.clone()), pool_id.clone(), Some(caller.clone()), new_start, new_end, Some(new_initial_weight), Some(new_final_weight), Some(DEFAULT_FEE), Some(fee_collector), Some(1))
verify {
let pool_data = LBP::<T>::pool_data(pool_id).unwrap();
assert_eq!(pool_data.start, new_start);
assert_eq!(pool_data.end, new_end);
assert_eq!(pool_data.initial_weight, new_initial_weight);
assert_eq!(pool_data.final_weight, new_final_weight);
}
add_liquidity {
let caller = funded_account::<T>("caller", 0);
let pool_id = LBP::<T>::pair_account_from_assets(ASSET_A_ID, ASSET_B_ID);
LBP::<T>::create_pool(RawOrigin::Root.into(), caller.clone(), ASSET_A_ID, ASSET_A_AMOUNT, ASSET_B_ID, ASSET_B_AMOUNT, INITIAL_WEIGHT, FINAL_WEIGHT, WeightCurveType::Linear, DEFAULT_FEE, caller.clone(), 0)?;
ensure!(PoolData::<T>::contains_key(&pool_id), "Pool does not exist.");
}: _(RawOrigin::Signed(caller), (ASSET_A_ID, 1_000_000_000_u128), (ASSET_B_ID, 2_000_000_000_u128))
verify {
assert_eq!(T::MultiCurrency::free_balance(ASSET_A_ID, &pool_id), 2_000_000_000_u128);
assert_eq!(T::MultiCurrency::free_balance(ASSET_B_ID, &pool_id), 4_000_000_000_u128);
}
remove_liquidity {
let caller = funded_account::<T>("caller", 0);
let pool_id = LBP::<T>::pair_account_from_assets(ASSET_A_ID, ASSET_B_ID);
LBP::<T>::create_pool(RawOrigin::Root.into(), caller.clone(), ASSET_A_ID, ASSET_A_AMOUNT, ASSET_B_ID, ASSET_B_AMOUNT, INITIAL_WEIGHT, FINAL_WEIGHT, WeightCurveType::Linear, DEFAULT_FEE, caller.clone(), 0)?;
ensure!(PoolData::<T>::contains_key(&pool_id), "Pool does not exist.");
}: _(RawOrigin::Signed(caller.clone()), pool_id.clone())
verify {
assert!(!PoolData::<T>::contains_key(&pool_id));
assert_eq!(T::MultiCurrency::free_balance(ASSET_A_ID, &caller), 1000000000000000);
assert_eq!(T::MultiCurrency::free_balance(ASSET_B_ID, &caller), 1000000000000000);
}
sell {
let caller = funded_account::<T>("caller", 0);
let fee_collector = funded_account::<T>("fee_collector", 0);
let asset_in: AssetId = ASSET_A_ID;
let asset_out: AssetId = ASSET_B_ID;
let amount : Balance = 100_000_000;
let max_limit: Balance = 10_000_000;
let pool_id = LBP::<T>::pair_account_from_assets(ASSET_A_ID, ASSET_B_ID);
LBP::<T>::create_pool(RawOrigin::Root.into(), caller.clone(), ASSET_A_ID, ASSET_A_AMOUNT, ASSET_B_ID, ASSET_B_AMOUNT, INITIAL_WEIGHT, FINAL_WEIGHT, WeightCurveType::Linear, DEFAULT_FEE, fee_collector.clone(), 0)?;
ensure!(PoolData::<T>::contains_key(&pool_id), "Pool does not exist.");
let start = T::BlockNumber::from(1u32);
let end = T::BlockNumber::from(11u32);
LBP::<T>::update_pool_data(RawOrigin::Signed(caller.clone()).into(), pool_id, None, Some(start), Some(end), None, None, None, None, None)?;
}: _(RawOrigin::Signed(caller.clone()), asset_in, asset_out, amount, max_limit)
verify{
assert_eq!(T::MultiCurrency::free_balance(asset_in, &caller), 999998900000000);
assert_eq!(T::MultiCurrency::free_balance(asset_out, &caller), 999998047091811);
assert_eq!(T::MultiCurrency::free_balance(asset_in, &fee_collector), 1000000000200000);
}
buy {
let caller = funded_account::<T>("caller", 0);
let fee_collector = funded_account::<T>("fee_collector", 0);
let asset_in: AssetId = ASSET_A_ID;
let asset_out: AssetId = ASSET_B_ID;
let amount : Balance = 100_000_000;
let max_limit: Balance = 1_000_000_000;
let pool_id = LBP::<T>::pair_account_from_assets(ASSET_A_ID, ASSET_B_ID);
LBP::<T>::create_pool(RawOrigin::Root.into(), caller.clone(), ASSET_A_ID, ASSET_A_AMOUNT, ASSET_B_ID, ASSET_B_AMOUNT, INITIAL_WEIGHT, FINAL_WEIGHT, WeightCurveType::Linear, DEFAULT_FEE, fee_collector.clone(), 0)?;
ensure!(PoolData::<T>::contains_key(&pool_id), "Pool does not exist.");
let start = T::BlockNumber::from(1u32);
let end = T::BlockNumber::from(11u32);
LBP::<T>::update_pool_data(RawOrigin::Signed(caller.clone()).into(), pool_id, None, Some(start), Some(end), None, None, None, None, None)?;
}: _(RawOrigin::Signed(caller.clone()), asset_out, asset_in, amount, max_limit)
verify{
assert_eq!(T::MultiCurrency::free_balance(asset_out, &caller), 999998100000000);
assert_eq!(T::MultiCurrency::free_balance(asset_in, &caller), 999998772262325);
assert_eq!(T::MultiCurrency::free_balance(asset_in, &fee_collector), 1000000000455474);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::{new_test_ext, Test};
use frame_support::assert_ok;
#[test]
fn test_benchmarks() {
new_test_ext().execute_with(|| {
assert_ok!(Pallet::<Test>::test_benchmark_create_pool());
assert_ok!(Pallet::<Test>::test_benchmark_update_pool_data());
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());
});
}
}