pezcumulus-primitives-utility 0.7.0

Helper datatypes for Pezcumulus
Documentation
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute
// This file is part of Pezcumulus.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::*;
use mock::{setup_pool, AccountId, AssetId, Balance, Fungibles};
use pezframe_support::{parameter_types, traits::fungibles::Inspect};
use xcm::latest::AssetId as XcmAssetId;
use xcm_executor::AssetsInHolding;

fn create_holding_asset(asset_id: AssetId, amount: Balance) -> AssetsInHolding {
	create_asset(asset_id, amount).into()
}

fn create_asset(asset_id: AssetId, amount: Balance) -> Asset {
	Asset { id: create_asset_id(asset_id), fun: Fungible(amount) }
}

fn create_asset_id(asset_id: AssetId) -> XcmAssetId {
	AssetId(Location::new(0, [GeneralIndex(asset_id.into())]))
}

fn xcm_context() -> XcmContext {
	XcmContext { origin: None, message_id: [0u8; 32], topic: None }
}

fn weight_worth_of(fee: Balance) -> Weight {
	Weight::from_parts(fee.try_into().unwrap(), 0)
}

const TARGET_ASSET: AssetId = 1;
const CLIENT_ASSET: AssetId = 2;
const CLIENT_ASSET_2: AssetId = 3;

parameter_types! {
	pub const TargetAsset: AssetId = TARGET_ASSET;
}

pub type Trader = SwapFirstAssetTrader<
	TargetAsset,
	mock::Swap,
	mock::WeightToFee,
	mock::Fungibles,
	mock::FungiblesMatcher,
	(),
	AccountId,
>;

#[test]
fn holding_asset_swap_for_target() {
	let client_asset_total = 15;
	let fee = 5;

	setup_pool(CLIENT_ASSET, 1000, TARGET_ASSET, 1000);

	let holding_asset = create_holding_asset(CLIENT_ASSET, client_asset_total);
	let holding_change = create_holding_asset(CLIENT_ASSET, client_asset_total - fee);

	let target_total = Fungibles::total_issuance(TARGET_ASSET);
	let client_total = Fungibles::total_issuance(CLIENT_ASSET);

	let mut trader = Trader::new();
	assert_eq!(
		trader.buy_weight(weight_worth_of(fee), holding_asset, &xcm_context()).unwrap(),
		holding_change
	);

	assert_eq!(trader.total_fee.peek(), fee);
	assert_eq!(trader.last_fee_asset, Some(create_asset_id(CLIENT_ASSET)));

	assert_eq!(Fungibles::total_issuance(TARGET_ASSET), target_total);
	assert_eq!(Fungibles::total_issuance(CLIENT_ASSET), client_total + fee);
}

#[test]
fn holding_asset_swap_for_target_twice() {
	let client_asset_total = 20;
	let fee1 = 5;
	let fee2 = 6;

	setup_pool(CLIENT_ASSET, 1000, TARGET_ASSET, 1000);

	let holding_asset = create_holding_asset(CLIENT_ASSET, client_asset_total);
	let holding_change1 = create_holding_asset(CLIENT_ASSET, client_asset_total - fee1);
	let holding_change2 = create_holding_asset(CLIENT_ASSET, client_asset_total - fee1 - fee2);

	let target_total = Fungibles::total_issuance(TARGET_ASSET);
	let client_total = Fungibles::total_issuance(CLIENT_ASSET);

	let mut trader = Trader::new();
	assert_eq!(
		trader.buy_weight(weight_worth_of(fee1), holding_asset, &xcm_context()).unwrap(),
		holding_change1
	);
	assert_eq!(
		trader
			.buy_weight(weight_worth_of(fee2), holding_change1, &xcm_context())
			.unwrap(),
		holding_change2
	);

	assert_eq!(trader.total_fee.peek(), fee1 + fee2);
	assert_eq!(trader.last_fee_asset, Some(create_asset_id(CLIENT_ASSET)));

	assert_eq!(Fungibles::total_issuance(TARGET_ASSET), target_total);
	assert_eq!(Fungibles::total_issuance(CLIENT_ASSET), client_total + fee1 + fee2);
}

#[test]
fn buy_and_refund_twice_for_target() {
	let client_asset_total = 15;
	let fee = 5;
	let refund1 = 4;
	let refund2 = 2;

	setup_pool(CLIENT_ASSET, 1000, TARGET_ASSET, 1000);
	// create pool for refund swap.
	setup_pool(TARGET_ASSET, 1000, CLIENT_ASSET, 1000);

	let holding_asset = create_holding_asset(CLIENT_ASSET, client_asset_total);
	let holding_change = create_holding_asset(CLIENT_ASSET, client_asset_total - fee);
	let refund_asset = create_asset(CLIENT_ASSET, refund1);

	let target_total = Fungibles::total_issuance(TARGET_ASSET);
	let client_total = Fungibles::total_issuance(CLIENT_ASSET);

	let mut trader = Trader::new();
	assert_eq!(
		trader.buy_weight(weight_worth_of(fee), holding_asset, &xcm_context()).unwrap(),
		holding_change
	);

	assert_eq!(trader.total_fee.peek(), fee);
	assert_eq!(trader.last_fee_asset, Some(create_asset_id(CLIENT_ASSET)));

	assert_eq!(trader.refund_weight(weight_worth_of(refund1), &xcm_context()), Some(refund_asset));

	assert_eq!(trader.total_fee.peek(), fee - refund1);
	assert_eq!(trader.last_fee_asset, Some(create_asset_id(CLIENT_ASSET)));

	assert_eq!(trader.refund_weight(weight_worth_of(refund2), &xcm_context()), None);

	assert_eq!(trader.total_fee.peek(), fee - refund1);
	assert_eq!(trader.last_fee_asset, Some(create_asset_id(CLIENT_ASSET)));

	assert_eq!(Fungibles::total_issuance(TARGET_ASSET), target_total);
	assert_eq!(Fungibles::total_issuance(CLIENT_ASSET), client_total + fee - refund1);
}

#[test]
fn buy_with_various_assets_and_refund_for_target() {
	let client_asset_total = 10;
	let client_asset_2_total = 15;
	let fee1 = 5;
	let fee2 = 6;
	let refund1 = 6;
	let refund2 = 4;

	setup_pool(CLIENT_ASSET, 1000, TARGET_ASSET, 1000);
	setup_pool(CLIENT_ASSET_2, 1000, TARGET_ASSET, 1000);
	// create pool for refund swap.
	setup_pool(TARGET_ASSET, 1000, CLIENT_ASSET_2, 1000);

	let holding_asset = create_holding_asset(CLIENT_ASSET, client_asset_total);
	let holding_asset_2 = create_holding_asset(CLIENT_ASSET_2, client_asset_2_total);
	let holding_change = create_holding_asset(CLIENT_ASSET, client_asset_total - fee1);
	let holding_change_2 = create_holding_asset(CLIENT_ASSET_2, client_asset_2_total - fee2);
	// both refunds in the latest buy asset (`CLIENT_ASSET_2`).
	let refund_asset = create_asset(CLIENT_ASSET_2, refund1);
	let refund_asset_2 = create_asset(CLIENT_ASSET_2, refund2);

	let target_total = Fungibles::total_issuance(TARGET_ASSET);
	let client_total = Fungibles::total_issuance(CLIENT_ASSET);
	let client_total_2 = Fungibles::total_issuance(CLIENT_ASSET_2);

	let mut trader = Trader::new();
	// first purchase with `CLIENT_ASSET`.
	assert_eq!(
		trader.buy_weight(weight_worth_of(fee1), holding_asset, &xcm_context()).unwrap(),
		holding_change
	);

	assert_eq!(trader.total_fee.peek(), fee1);
	assert_eq!(trader.last_fee_asset, Some(create_asset_id(CLIENT_ASSET)));

	// second purchase with `CLIENT_ASSET_2`.
	assert_eq!(
		trader
			.buy_weight(weight_worth_of(fee2), holding_asset_2, &xcm_context())
			.unwrap(),
		holding_change_2
	);

	assert_eq!(trader.total_fee.peek(), fee1 + fee2);
	assert_eq!(trader.last_fee_asset, Some(create_asset_id(CLIENT_ASSET_2)));

	// first refund in the last asset used with `buy_weight`.
	assert_eq!(trader.refund_weight(weight_worth_of(refund1), &xcm_context()), Some(refund_asset));

	assert_eq!(trader.total_fee.peek(), fee1 + fee2 - refund1);
	assert_eq!(trader.last_fee_asset, Some(create_asset_id(CLIENT_ASSET_2)));

	// second refund in the last asset used with `buy_weight`.
	assert_eq!(
		trader.refund_weight(weight_worth_of(refund2), &xcm_context()),
		Some(refund_asset_2)
	);

	assert_eq!(trader.total_fee.peek(), fee1 + fee2 - refund1 - refund2);
	assert_eq!(trader.last_fee_asset, Some(create_asset_id(CLIENT_ASSET_2)));

	assert_eq!(Fungibles::total_issuance(TARGET_ASSET), target_total);
	assert_eq!(Fungibles::total_issuance(CLIENT_ASSET), client_total + fee1);
	assert_eq!(
		Fungibles::total_issuance(CLIENT_ASSET_2),
		client_total_2 + fee2 - refund1 - refund2
	);
}

#[test]
fn not_enough_to_refund() {
	let client_asset_total = 15;
	let fee = 5;
	let refund = 6;

	setup_pool(CLIENT_ASSET, 1000, TARGET_ASSET, 1000);

	let holding_asset = create_holding_asset(CLIENT_ASSET, client_asset_total);
	let holding_change = create_holding_asset(CLIENT_ASSET, client_asset_total - fee);

	let target_total = Fungibles::total_issuance(TARGET_ASSET);
	let client_total = Fungibles::total_issuance(CLIENT_ASSET);

	let mut trader = Trader::new();
	assert_eq!(
		trader.buy_weight(weight_worth_of(fee), holding_asset, &xcm_context()).unwrap(),
		holding_change
	);

	assert_eq!(trader.total_fee.peek(), fee);
	assert_eq!(trader.last_fee_asset, Some(create_asset_id(CLIENT_ASSET)));

	assert_eq!(trader.refund_weight(weight_worth_of(refund), &xcm_context()), None);

	assert_eq!(Fungibles::total_issuance(TARGET_ASSET), target_total);
	assert_eq!(Fungibles::total_issuance(CLIENT_ASSET), client_total + fee);
}

#[test]
fn not_exchangeable_to_refund() {
	let client_asset_total = 15;
	let fee = 5;
	let refund = 1;

	setup_pool(CLIENT_ASSET, 1000, TARGET_ASSET, 1000);

	let holding_asset = create_holding_asset(CLIENT_ASSET, client_asset_total);
	let holding_change = create_holding_asset(CLIENT_ASSET, client_asset_total - fee);

	let target_total = Fungibles::total_issuance(TARGET_ASSET);
	let client_total = Fungibles::total_issuance(CLIENT_ASSET);

	let mut trader = Trader::new();
	assert_eq!(
		trader.buy_weight(weight_worth_of(fee), holding_asset, &xcm_context()).unwrap(),
		holding_change
	);

	assert_eq!(trader.total_fee.peek(), fee);
	assert_eq!(trader.last_fee_asset, Some(create_asset_id(CLIENT_ASSET)));

	assert_eq!(trader.refund_weight(weight_worth_of(refund), &xcm_context()), None);

	assert_eq!(Fungibles::total_issuance(TARGET_ASSET), target_total);
	assert_eq!(Fungibles::total_issuance(CLIENT_ASSET), client_total + fee);
}

#[test]
fn nothing_to_refund() {
	let fee = 5;

	let mut trader = Trader::new();
	assert_eq!(trader.refund_weight(weight_worth_of(fee), &xcm_context()), None);
}

#[test]
fn holding_asset_not_exchangeable_for_target() {
	let holding_asset = create_holding_asset(CLIENT_ASSET, 10);

	let target_total = Fungibles::total_issuance(TARGET_ASSET);
	let client_total = Fungibles::total_issuance(CLIENT_ASSET);

	let mut trader = Trader::new();
	assert_eq!(
		trader
			.buy_weight(Weight::from_all(10), holding_asset, &xcm_context())
			.unwrap_err(),
		XcmError::FeesNotMet
	);

	assert_eq!(Fungibles::total_issuance(TARGET_ASSET), target_total);
	assert_eq!(Fungibles::total_issuance(CLIENT_ASSET), client_total);
}

#[test]
fn empty_holding_asset() {
	let mut trader = Trader::new();
	assert_eq!(
		trader
			.buy_weight(Weight::from_all(10), AssetsInHolding::new(), &xcm_context())
			.unwrap_err(),
		XcmError::AssetNotFound
	);
}

#[test]
fn fails_to_match_holding_asset() {
	let mut trader = Trader::new();
	let holding_asset = Asset { id: AssetId(Location::new(1, [Teyrchain(1)])), fun: Fungible(10) };
	assert_eq!(
		trader
			.buy_weight(Weight::from_all(10), holding_asset.into(), &xcm_context())
			.unwrap_err(),
		XcmError::AssetNotFound
	);
}

#[test]
fn holding_asset_equal_to_target_asset() {
	let mut trader = Trader::new();
	let holding_asset = create_holding_asset(TargetAsset::get(), 10);
	assert_eq!(
		trader
			.buy_weight(Weight::from_all(10), holding_asset, &xcm_context())
			.unwrap_err(),
		XcmError::FeesNotMet
	);
}

pub mod mock {
	use crate::*;
	use core::cell::RefCell;
	use pezframe_support::{
		ensure,
		traits::{
			fungibles::{Balanced, DecreaseIssuance, Dust, IncreaseIssuance, Inspect, Unbalanced},
			tokens::{
				DepositConsequence, Fortitude, Fortitude::Polite, Precision::Exact, Preservation,
				Preservation::Preserve, Provenance, WithdrawConsequence,
			},
		},
	};
	use pezsp_runtime::{traits::One, DispatchError};
	use std::collections::HashMap;
	use xcm::latest::Junction;

	pub type AccountId = u64;
	pub type AssetId = u32;
	pub type Balance = u128;
	pub type Credit = fungibles::Credit<AccountId, Fungibles>;

	thread_local! {
	   pub static TOTAL_ISSUANCE: RefCell<HashMap<AssetId, Balance>> = RefCell::new(HashMap::new());
	   pub static ACCOUNT: RefCell<HashMap<(AssetId, AccountId), Balance>> = RefCell::new(HashMap::new());
	   pub static SWAP: RefCell<HashMap<(AssetId, AssetId), AccountId>> = RefCell::new(HashMap::new());
	}

	pub struct Swap {}
	impl SwapCreditT<AccountId> for Swap {
		type Balance = Balance;
		type AssetKind = AssetId;
		type Credit = Credit;
		fn max_path_len() -> u32 {
			2
		}
		fn swap_exact_tokens_for_tokens(
			path: Vec<Self::AssetKind>,
			credit_in: Self::Credit,
			amount_out_min: Option<Self::Balance>,
		) -> Result<Self::Credit, (Self::Credit, DispatchError)> {
			ensure!(2 == path.len(), (credit_in, DispatchError::Unavailable));
			ensure!(
				credit_in.peek() >= amount_out_min.unwrap_or(Self::Balance::zero()),
				(credit_in, DispatchError::Unavailable)
			);
			let swap_res = SWAP.with(|b| b.borrow().get(&(path[0], path[1])).map(|v| *v));
			let pool_account = match swap_res {
				Some(a) => a,
				None => return Err((credit_in, DispatchError::Unavailable)),
			};
			let credit_out = match Fungibles::withdraw(
				path[1],
				&pool_account,
				credit_in.peek(),
				Exact,
				Preserve,
				Polite,
			) {
				Ok(c) => c,
				Err(_) => return Err((credit_in, DispatchError::Unavailable)),
			};
			Fungibles::resolve(&pool_account, credit_in)
				.map_err(|c| (c, DispatchError::Unavailable))?;
			Ok(credit_out)
		}
		fn swap_tokens_for_exact_tokens(
			path: Vec<Self::AssetKind>,
			credit_in: Self::Credit,
			amount_out: Self::Balance,
		) -> Result<(Self::Credit, Self::Credit), (Self::Credit, DispatchError)> {
			ensure!(2 == path.len(), (credit_in, DispatchError::Unavailable));
			ensure!(credit_in.peek() >= amount_out, (credit_in, DispatchError::Unavailable));
			let swap_res = SWAP.with(|b| b.borrow().get(&(path[0], path[1])).map(|v| *v));
			let pool_account = match swap_res {
				Some(a) => a,
				None => return Err((credit_in, DispatchError::Unavailable)),
			};
			let credit_out = match Fungibles::withdraw(
				path[1],
				&pool_account,
				amount_out,
				Exact,
				Preserve,
				Polite,
			) {
				Ok(c) => c,
				Err(_) => return Err((credit_in, DispatchError::Unavailable)),
			};
			let (credit_in, change) = credit_in.split(amount_out);
			Fungibles::resolve(&pool_account, credit_in)
				.map_err(|c| (c, DispatchError::Unavailable))?;
			Ok((credit_out, change))
		}
	}

	pub fn pool_account(asset1: AssetId, asset2: AssetId) -> AccountId {
		(1000 + asset1 * 10 + asset2 * 100).into()
	}

	pub fn setup_pool(asset1: AssetId, liquidity1: Balance, asset2: AssetId, liquidity2: Balance) {
		let account = pool_account(asset1, asset2);
		SWAP.with(|b| b.borrow_mut().insert((asset1, asset2), account));
		let debt1 = Fungibles::deposit(asset1, &account, liquidity1, Exact);
		let debt2 = Fungibles::deposit(asset2, &account, liquidity2, Exact);
		drop(debt1);
		drop(debt2);
	}

	pub struct WeightToFee;
	impl WeightToFeeT for WeightToFee {
		type Balance = Balance;
		fn weight_to_fee(weight: &Weight) -> Self::Balance {
			(weight.ref_time() + weight.proof_size()).into()
		}
	}

	pub struct Fungibles {}
	impl Inspect<AccountId> for Fungibles {
		type AssetId = AssetId;
		type Balance = Balance;
		fn total_issuance(asset: Self::AssetId) -> Self::Balance {
			TOTAL_ISSUANCE.with(|b| b.borrow().get(&asset).map_or(Self::Balance::zero(), |b| *b))
		}
		fn minimum_balance(_: Self::AssetId) -> Self::Balance {
			Self::Balance::one()
		}
		fn total_balance(asset: Self::AssetId, who: &AccountId) -> Self::Balance {
			ACCOUNT.with(|b| b.borrow().get(&(asset, *who)).map_or(Self::Balance::zero(), |b| *b))
		}
		fn balance(asset: Self::AssetId, who: &AccountId) -> Self::Balance {
			ACCOUNT.with(|b| b.borrow().get(&(asset, *who)).map_or(Self::Balance::zero(), |b| *b))
		}
		fn reducible_balance(
			asset: Self::AssetId,
			who: &AccountId,
			_: Preservation,
			_: Fortitude,
		) -> Self::Balance {
			ACCOUNT.with(|b| b.borrow().get(&(asset, *who)).map_or(Self::Balance::zero(), |b| *b))
		}
		fn can_deposit(
			_: Self::AssetId,
			_: &AccountId,
			_: Self::Balance,
			_: Provenance,
		) -> DepositConsequence {
			unimplemented!()
		}
		fn can_withdraw(
			_: Self::AssetId,
			_: &AccountId,
			_: Self::Balance,
		) -> WithdrawConsequence<Self::Balance> {
			unimplemented!()
		}
		fn asset_exists(_: Self::AssetId) -> bool {
			unimplemented!()
		}
	}

	impl Unbalanced<AccountId> for Fungibles {
		fn set_total_issuance(asset: Self::AssetId, amount: Self::Balance) {
			TOTAL_ISSUANCE.with(|b| b.borrow_mut().insert(asset, amount));
		}
		fn handle_dust(_: Dust<AccountId, Self>) {
			unimplemented!()
		}
		fn write_balance(
			asset: Self::AssetId,
			who: &AccountId,
			amount: Self::Balance,
		) -> Result<Option<Self::Balance>, DispatchError> {
			let _ = ACCOUNT.with(|b| b.borrow_mut().insert((asset, *who), amount));
			Ok(None)
		}
	}

	impl Balanced<AccountId> for Fungibles {
		type OnDropCredit = DecreaseIssuance<AccountId, Self>;
		type OnDropDebt = IncreaseIssuance<AccountId, Self>;
	}

	pub struct FungiblesMatcher;
	impl MatchesFungibles<AssetId, Balance> for FungiblesMatcher {
		fn matches_fungibles(
			a: &Asset,
		) -> core::result::Result<(AssetId, Balance), xcm_executor::traits::Error> {
			match a {
				Asset { fun: Fungible(amount), id: AssetId(inner_location) } => {
					match inner_location.unpack() {
						(0, [Junction::GeneralIndex(id)]) => {
							Ok(((*id).try_into().unwrap(), *amount))
						},
						_ => Err(xcm_executor::traits::Error::AssetNotHandled),
					}
				},
				_ => Err(xcm_executor::traits::Error::AssetNotHandled),
			}
		}
	}
}