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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg(feature = "runtime-benchmarks")]
#![allow(clippy::all)]
#[cfg(test)]
mod mock;
use codec::{Decode, Encode};
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};
use frame_support::traits::{Get, OnInitialize};
use frame_system::RawOrigin;
use hex_literal::hex;
use pool_xyk::PoolProviders;
use pswap_distribution::DistributionWeightParams;
use pswap_distribution::{Call, ClaimableShares, ShareholderAccounts};
use sp_std::prelude::*;
use traits::MultiCurrencyExtended;
use common::fixnum::ops::One;
use common::{balance, fixed, AssetInfoProvider, Fixed, FromGenericPair, PSWAP};
use assets::Pallet as Assets;
use permissions::Pallet as Permissions;
use pswap_distribution::Pallet as PSwap;
use sp_std::convert::TryFrom;
use technical::Pallet as Technical;
use tokens::Pallet as Tokens;
pub struct Pallet<T: Config>(pswap_distribution::Pallet<T>);
pub trait Config: pswap_distribution::Config + pool_xyk::Config {}
fn alice<T: Config>() -> T::AccountId {
let bytes = hex!("d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d");
T::AccountId::decode(&mut &bytes[..]).expect("Failed to decode account ID")
}
fn create_account<T: Config>(prefix: Vec<u8>, index: u128) -> T::AccountId {
let tech_account: T::TechAccountId =
T::TechAccountId::from_generic_pair(prefix, index.encode());
Technical::<T>::tech_account_id_to_account_id(&tech_account).unwrap()
}
fn setup_benchmark_pool_xyk<T: Config + pool_xyk::Config>() {
#[cfg(not(test))]
{
use common::{XOR, XST};
let authority = alice::<T>();
pool_xyk::Pallet::<T>::initialize_pool(
RawOrigin::Signed(authority.clone()).into(),
common::DEXId::Polkaswap.into(),
XOR.into(),
PSWAP.into(),
)
.unwrap();
pool_xyk::Pallet::<T>::initialize_pool(
RawOrigin::Signed(authority.clone()).into(),
common::DEXId::Polkaswap.into(),
XOR.into(),
XST.into(),
)
.unwrap();
Assets::<T>::mint_to(&XOR.into(), &authority, &authority, balance!(20000)).unwrap();
Assets::<T>::mint_to(&XST.into(), &authority, &authority, balance!(100000)).unwrap();
Assets::<T>::mint_to(&PSWAP.into(), &authority, &authority, balance!(1000000)).unwrap();
pool_xyk::Pallet::<T>::deposit_liquidity_unchecked(
authority.clone(),
common::DEXId::Polkaswap.into(),
XOR.into(),
XST.into(),
balance!(10000),
balance!(100000),
balance!(10000),
balance!(100000),
)
.unwrap();
pool_xyk::Pallet::<T>::deposit_liquidity_unchecked(
authority.clone(),
common::DEXId::Polkaswap.into(),
XOR.into(),
PSWAP.into(),
balance!(10000),
balance!(1000000),
balance!(10000),
balance!(1000000),
)
.unwrap();
}
}
fn add_subscribtion<T: Config + pool_xyk::Config>(pool_index: u128, shareholders: u128) {
let authority = alice::<T>();
let pool_fee_account = create_account::<T>(b"pool_fee".to_vec(), pool_index);
frame_system::Pallet::<T>::inc_providers(&pool_fee_account);
let pool_account = create_account::<T>(b"pool".to_vec(), pool_index);
frame_system::Pallet::<T>::inc_providers(&pool_account);
Assets::<T>::mint_to(&PSWAP.into(), &authority, &pool_fee_account, balance!(1000)).unwrap();
PSwap::<T>::subscribe(
pool_fee_account,
common::DEXId::Polkaswap.into(),
pool_account.clone(),
None,
)
.unwrap();
for j in 0u128..shareholders {
let liquidity_provider = create_account::<T>(b"liquidity_provider".to_vec(), j);
frame_system::Pallet::<T>::inc_providers(&liquidity_provider);
pool_xyk::Pallet::<T>::mint(&pool_account, &liquidity_provider, balance!(100)).unwrap();
}
}
fn prepare_for_distribution<T: Config + pool_xyk::Config>(weight_params: DistributionWeightParams) {
let authority = alice::<T>();
frame_system::Pallet::<T>::inc_providers(&authority);
Permissions::<T>::assign_permission(
authority.clone(),
&authority,
permissions::MINT,
permissions::Scope::Unlimited,
)
.unwrap();
setup_benchmark_pool_xyk::<T>();
let mut pool_index = 0;
frame_system::Pallet::<T>::set_block_number(0u32.into());
for i in 0..weight_params.distributed {
let shareholders = if i + 1 == weight_params.distributed {
let remaining = weight_params.shareholders
- (weight_params.shareholders / weight_params.distributed)
* (weight_params.distributed - 1);
remaining
} else {
weight_params.shareholders / weight_params.distributed
};
add_subscribtion::<T>(pool_index, shareholders as u128);
pool_index += 1;
}
frame_system::Pallet::<T>::set_block_number(1u32.into());
for _ in 0..weight_params.skipped {
add_subscribtion::<T>(pool_index, 10);
pool_index += 1;
}
}
fn validate_distribution<T: Config>(weight_params: DistributionWeightParams) {
for i in 0..weight_params.distributed {
let pool_account = create_account::<T>(b"pool".to_vec(), i as u128);
let shareholders = if i + 1 == weight_params.distributed {
let remaining = weight_params.shareholders
- (weight_params.shareholders / weight_params.distributed)
* (weight_params.distributed - 1);
remaining
} else {
weight_params.shareholders / weight_params.distributed
};
for j in 0..shareholders {
let liquidity_provider = create_account::<T>(b"liquidity_provider".to_vec(), j as u128);
frame_system::Pallet::<T>::inc_providers(&liquidity_provider);
let _ =
PSwap::<T>::claim_incentive(RawOrigin::Signed(liquidity_provider.clone()).into());
assert_eq!(
PoolProviders::<T>::get(&pool_account, &liquidity_provider).unwrap(),
balance!(100)
);
assert!(
Assets::<T>::free_balance(&PSWAP.into(), &liquidity_provider).unwrap()
> balance!(0)
);
}
}
}
benchmarks! {
claim_incentive {
let caller = alice::<T>();
frame_system::Pallet::<T>::inc_providers(&caller);
ShareholderAccounts::<T>::insert(caller.clone(), Fixed::ONE);
ClaimableShares::<T>::put(Fixed::ONE);
let pswap_rewards_account = T::GetTechnicalAccountId::get();
let pswap_asset_id: T::AssetId = PSWAP.into();
let pswap_currency = <T::AssetId as Into<<T as tokens::Config>::CurrencyId>>::into(pswap_asset_id);
let pswap_amount = <T as tokens::Config>::Amount::try_from(balance!(500)).map_err(|_|()).unwrap();
Tokens::<T>::update_balance(pswap_currency, &pswap_rewards_account, pswap_amount).unwrap();
}: _(
RawOrigin::Signed(caller.clone())
)
verify {
assert_eq!(ClaimableShares::<T>::get(), fixed!(0));
}
on_initialize {
let a in 1..50;
let b in 1..10;
let c in 10..100;
let weight_params = DistributionWeightParams {
skipped: a,
distributed: b,
shareholders: c
};
prepare_for_distribution::<T>(weight_params.clone());
let distribution_freq = T::GetDefaultSubscriptionFrequency::get();
}: {
PSwap::<T>::on_initialize(distribution_freq);
}
verify {
validate_distribution::<T>(weight_params);
}
}
impl_benchmark_test_suite!(
Pallet,
crate::mock::ExtBuilder::default().build(),
crate::mock::Runtime
);