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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
#![cfg_attr(not(feature = "std"), no_std)]
// `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
#![recursion_limit = "256"]

use codec::{Decode, Encode, MaxEncodedLen};
pub use constants::{fee::*, parachains::*};
pub use currency::*;
#[cfg(feature = "runtime-benchmarks")]
use frame_support::traits::OriginTrait;
use frame_support::{
	dispatch::{DispatchClass, DispatchResult},
	ensure, parameter_types,
	traits::{
		tokens::currency::{MultiTokenCurrency, MultiTokenImbalanceWithZeroTrait},
		Contains, EnsureOrigin, EnsureOriginWithArg, ExistenceRequirement, Get, Imbalance,
		WithdrawReasons,
	},
	unsigned::TransactionValidityError,
	weights::{constants::WEIGHT_REF_TIME_PER_SECOND, ConstantMultiplier, Weight},
	PalletId,
};
#[cfg(any(feature = "std", test))]
pub use frame_system::Call as SystemCall;
use frame_system::{
	limits::{BlockLength, BlockWeights},
	EnsureRoot,
};
use mangata_support::traits::{AssetRegistryApi, FeeLockTriggerTrait, PreValidateSwaps};
pub use mangata_types::assets::{CustomMetadata, XcmMetadata, XykMetadata};
pub use orml_tokens;
use orml_tokens::MultiTokenCurrencyExtended;
use orml_traits::{
	asset_registry::{AssetMetadata, AssetProcessor},
	parameter_type_with_key,
};
pub use pallet_issuance::IssuanceInfo;
pub use pallet_sudo_mangata;
pub use pallet_sudo_origin;
use pallet_transaction_payment_mangata::{ConstFeeMultiplier, Multiplier, OnChargeTransaction};
pub use pallet_xyk;
use pallet_xyk::AssetMetadataMutationTrait;
pub use polkadot_runtime_common::BlockHashCount;
use scale_info::TypeInfo;
pub use sp_consensus_aura::sr25519::AuthorityId as AuraId;
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
pub use sp_runtime::{
	generic,
	traits::{
		AccountIdConversion, BlakeTwo256, DispatchInfoOf, IdentifyAccount, PostDispatchInfoOf,
		Saturating, Verify, Zero,
	},
	transaction_validity::InvalidTransaction,
	BoundedVec, DispatchError, FixedPointNumber, MultiAddress, MultiSignature, OpaqueExtrinsic,
	Perbill, Percent, Permill, RuntimeDebug,
};
use sp_std::{cmp::Ordering, marker::PhantomData, prelude::*};
pub use types::*;

pub mod constants;
mod weights;
pub mod xcm_config;

pub mod currency {
	use super::Balance;

	pub const MILLICENTS: Balance = CENTS / 1000;
	pub const CENTS: Balance = DOLLARS / 100; // assume this is worth about a cent.
	pub const DOLLARS: Balance = super::consts::UNIT;

	pub const fn deposit(items: u32, bytes: u32) -> Balance {
		items as Balance * 5000 * DOLLARS + (bytes as Balance) * 60 * CENTS
	}
}

pub mod types {
	use super::*;

	pub type TokenId = u32;
	pub type Balance = u128;
	pub type Amount = i128;

	// /// Alias to 512-bit hash when used in the context of a transaction signature on the chain.
	pub type Signature = MultiSignature;

	// /// Some way of identifying an account on the chain. We intentionally make it equivalent
	// /// to the public key of our transaction signing scheme.
	pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;

	// /// Index of a transaction in the chain.
	pub type Nonce = u32;

	// /// A hash of some data used by the chain.
	pub type Hash = sp_core::H256;

	// /// An index to a block.
	pub type BlockNumber = u32;

	// /// The address format for describing accounts.
	pub type Address = MultiAddress<AccountId, ()>;
}

pub mod tokens {
	use super::*;
	pub const MGX_TOKEN_ID: TokenId = 0;
	pub const RELAY_TOKEN_ID: TokenId = 4;
	pub const KAR_TOKEN_ID: TokenId = 6;
	pub const TUR_TOKEN_ID: TokenId = 7;

	parameter_types! {
		pub const MgxTokenId: TokenId = MGX_TOKEN_ID;
		pub const RelayTokenId: TokenId = RELAY_TOKEN_ID;
		pub const TurTokenId: TokenId = TUR_TOKEN_ID;
	}
}

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
/// of data like extrinsics, allowing for them to continue syncing the network through upgrades
/// to even the core data structures.
pub mod opaque {
	use super::*;
	use sp_runtime::{
		generic,
		traits::{BlakeTwo256, Hash as HashT},
	};

	pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic;
	/// Opaque block header type.
	pub type Header = generic::HeaderVer<BlockNumber, BlakeTwo256>;
	/// Opaque block type.
	pub type Block = generic::Block<Header, UncheckedExtrinsic>;
	/// Opaque block identifier type.
	pub type BlockId = generic::BlockId<Block>;
	/// Opaque block hash type.
	pub type Hash = <BlakeTwo256 as HashT>::Output;
}

pub mod runtime_types {
	use super::*;

	pub type SignedExtra<Runtime> = (
		frame_system::CheckSpecVersion<Runtime>,
		frame_system::CheckTxVersion<Runtime>,
		frame_system::CheckGenesis<Runtime>,
		frame_system::CheckEra<Runtime>,
		frame_system::CheckNonce<Runtime>,
		frame_system::CheckWeight<Runtime>,
		pallet_transaction_payment_mangata::ChargeTransactionPayment<Runtime>,
	);

	pub type SignedPayload<Runtime, RuntimeCall> =
		generic::SignedPayload<RuntimeCall, SignedExtra<Runtime>>;
	pub type UncheckedExtrinsic<Runtime, RuntimeCall> =
		generic::UncheckedExtrinsic<Address, RuntimeCall, Signature, SignedExtra<Runtime>>;
	pub type CheckedExtrinsic<Runtime, RuntimeCall> =
		generic::CheckedExtrinsic<AccountId, RuntimeCall, SignedExtra<Runtime>>;
	pub type Header = generic::HeaderVer<BlockNumber, BlakeTwo256>;
	pub type Block<Runtime, RuntimeCall> =
		generic::Block<Header, UncheckedExtrinsic<Runtime, RuntimeCall>>;
	pub type SignedBlock<Runtime, RuntimeCall> = generic::SignedBlock<Block<Runtime, RuntimeCall>>;
	pub type BlockId<Runtime, RuntimeCall> = generic::BlockId<Block<Runtime, RuntimeCall>>;

	pub type OpaqueBlock = generic::Block<Header, sp_runtime::OpaqueExtrinsic>;
	pub type OpaqueBlockId = generic::BlockId<OpaqueBlock>;
}

pub mod consts {
	use super::*;
	/// This determines the average expected block time that we are targeting.
	/// Blocks will be produced at a minimum duration defined by `SLOT_DURATION`.
	/// `SLOT_DURATION` is picked up by `pallet_timestamp` which is in turn picked
	/// up by `pallet_aura` to implement `fn slot_duration()`.
	///
	/// Change this to adjust the block time.
	pub const MILLISECS_PER_BLOCK: u64 = 12000;

	// Time is measured by number of blocks.
	pub const MINUTES: BlockNumber = 60_000 / (MILLISECS_PER_BLOCK as BlockNumber);
	pub const HOURS: BlockNumber = MINUTES * 60;
	pub const DAYS: BlockNumber = HOURS * 24;

	// Unit = the base number of indivisible units for balance
	pub const UNIT: Balance = 1_000_000_000_000_000_000;
	pub const MILLIUNIT: Balance = 1_000_000_000_000_000;
	pub const MICROUNIT: Balance = 1_000_000_000_000;

	/// We allow for 0.5 of a second of compute with a 12 second average block time.
	/// NOTE: reduced by half comparing to origin impl as we want to fill block only up to 50%
	/// so there is room for new extrinsics in the next block
	pub const MAXIMUM_BLOCK_WEIGHT: Weight = Weight::from_parts(
		WEIGHT_REF_TIME_PER_SECOND.saturating_div(4),
		polkadot_primitives::v5::MAX_POV_SIZE as u64,
	);

	/// The existential deposit. Set to 1/10 of the Connected Relay Chain.
	pub const EXISTENTIAL_DEPOSIT: Balance = MILLIUNIT;
}

pub enum CallType {
	AtomicSell {
		sold_asset_id: TokenId,
		sold_asset_amount: Balance,
		bought_asset_id: TokenId,
		min_amount_out: Balance,
	},
	AtomicBuy {
		sold_asset_id: TokenId,
		bought_asset_amount: Balance,
		bought_asset_id: TokenId,
		max_amount_in: Balance,
	},
	MultiSell {
		swap_token_list: Vec<TokenId>,
		sold_asset_amount: Balance,
		min_amount_out: Balance,
	},
	MultiBuy {
		swap_token_list: Vec<TokenId>,
		bought_asset_amount: Balance,
		max_amount_in: Balance,
	},
	CompoundRewards,
	ProvideLiquidityWithConversion,
	UnlockFee,
	UtilityInnerCall,
	Other,
}

pub mod config {
	use super::*;

	pub type TreasuryPalletIdOf<T> = <T as ::pallet_treasury::Config>::PalletId;

	pub struct TreasuryAccountIdOf<T: ::pallet_treasury::Config>(PhantomData<T>);
	impl<T: ::pallet_treasury::Config> Get<AccountId> for TreasuryAccountIdOf<T> {
		fn get() -> AccountId {
			TreasuryPalletIdOf::<T>::get().into_account_truncating()
		}
	}

	pub type ExistentialDepositsOf<T> = <T as ::orml_tokens::Config>::ExistentialDeposits;
	pub type MaxLocksOf<T> = <T as ::orml_tokens::Config>::MaxLocks;
	pub type SessionLenghtOf<T> = <T as ::parachain_staking::Config>::BlocksPerRound;

	pub mod frame_system {
		use super::*;

		/// We assume that ~5% of the block weight is consumed by `on_initialize` handlers. This is
		/// used to limit the maximal weight of a single extrinsic.
		pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(5);

		/// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used by
		/// `Operational` extrinsics.
		pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);

		pub type MaxConsumers = frame_support::traits::ConstU32<16>;

		parameter_types! {

			// This part is copied from Substrate's `bin/node/runtime/src/lib.rs`.
			//  The `RuntimeBlockLength` and `RuntimeBlockWeights` exist here because the
			// `DeletionWeightLimit` and `DeletionQueueDepth` depend on those to parameterize
			// the lazy contract deletion.
			pub RuntimeBlockLength: BlockLength =
				BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
			pub RuntimeBlockWeights: BlockWeights = BlockWeights::builder()
				.base_block(weights::VerBlockExecutionWeight::get())
				.for_class(DispatchClass::all(), |weights| {
					weights.base_extrinsic = weights::VerExtrinsicBaseWeight::get();
				})
				.for_class(DispatchClass::Normal, |weights| {
					weights.max_total = Some(NORMAL_DISPATCH_RATIO * consts::MAXIMUM_BLOCK_WEIGHT);
				})
				.for_class(DispatchClass::Operational, |weights| {
					weights.max_total = Some(consts::MAXIMUM_BLOCK_WEIGHT);
					// Operational transactions have some extra reserved space, so that they
					// are included even if block reached `MAXIMUM_BLOCK_WEIGHT`.
					weights.reserved = Some(
						consts::MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * consts::MAXIMUM_BLOCK_WEIGHT
					);
				})
				.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
				.build_or_panic();
			pub const SS58Prefix: u16 = 42;
		}
	}

	pub mod pallet_timestamp {
		use super::*;

		// NOTE: Currently it is not possible to change the slot duration after the chain has started.
		//       Attempting to do so will brick block production.
		parameter_types! {
			pub const MinimumPeriod: u64 = consts::MILLISECS_PER_BLOCK / 2;
		}
	}

	pub mod pallet_treasury {
		use super::*;
		parameter_types! {
		pub const TreasuryPalletId: PalletId = PalletId(*b"py/trsry");
		}

		parameter_types! {
			pub const ProposalBond: Permill = Permill::from_percent(5);
			pub const ProposalBondMinimum: Balance = 1 * currency::DOLLARS;
			pub const ProposalBondMaximum: Option<Balance> = None;
			pub const SpendPeriod: BlockNumber = 1 * consts::DAYS;
			pub const Burn: Permill = Permill::from_percent(0);
			pub const MaxApprovals: u32 = 100;
		}
	}

	pub mod orml_tokens {
		use super::*;
		parameter_types! {
			pub const MaxLocks: u32 = 50;
		}

		parameter_type_with_key! {
			pub ExistentialDeposits: |_currency_id: TokenId| -> Balance {
				0
			};
		}

		pub struct DustRemovalWhitelist<T: Get<AccountId>>(PhantomData<T>);
		impl<T: Get<AccountId>> Contains<AccountId> for DustRemovalWhitelist<T> {
			fn contains(a: &AccountId) -> bool {
				*a == T::get()
			}
		}

		pub type ReserveIdentifier = [u8; 8];
	}

	pub mod pallet_xyk {

		use super::*;
		parameter_types! {
			pub const BnbTreasurySubAccDerive: [u8; 4] = *b"bnbt";
		}
		pub type PoolFeePercentage = frame_support::traits::ConstU128<20>;
		pub type TreasuryFeePercentage = frame_support::traits::ConstU128<5>;
		pub type BuyAndBurnFeePercentage = frame_support::traits::ConstU128<5>;

		pub struct TestTokensFilter;
		impl Contains<TokenId> for TestTokensFilter {
			fn contains(token_id: &TokenId) -> bool {
				// we dont want to allow doing anything with dummy assets previously
				// used for testing
				*token_id == 2 || *token_id == 3
			}
		}

		pub struct AssetRegisterFilter<Runtime>(PhantomData<Runtime>);
		impl<T> Contains<TokenId> for AssetRegisterFilter<T>
		where
			T: ::orml_asset_registry::Config<
				CustomMetadata = CustomMetadata,
				AssetId = TokenId,
				Balance = Balance,
			>,
		{
			fn contains(t: &TokenId) -> bool {
				let meta: Option<_> = ::orml_asset_registry::Metadata::<T>::get(*t);
				if let Some(xyk) = meta.and_then(|m| m.additional.xyk) {
					return xyk.operations_disabled
				}
				return false
			}
		}

		pub struct AssetMetadataMutation<Runtime>(PhantomData<Runtime>);

		impl<T> AssetMetadataMutationTrait<TokenId> for AssetMetadataMutation<T>
		where
			T: ::orml_asset_registry::Config<
				CustomMetadata = CustomMetadata,
				AssetId = TokenId,
				Balance = Balance,
				StringLimit = orml_asset_registry::StringLimit,
			>,
		{
			fn set_asset_info(
				asset: TokenId,
				name: Vec<u8>,
				symbol: Vec<u8>,
				decimals: u32,
			) -> DispatchResult {
				let metadata = AssetMetadata {
					name: BoundedVec::truncate_from(name),
					symbol: BoundedVec::truncate_from(symbol),
					decimals,
					existential_deposit: Default::default(),
					additional: Default::default(),
					location: None,
				};
				::orml_asset_registry::Pallet::<T>::do_register_asset_without_asset_processor(
					metadata, asset,
				)?;
				Ok(())
			}
		}
	}

	pub mod pallet_bootstrap {
		use super::*;

		parameter_types! {
			pub const BootstrapUpdateBuffer: BlockNumber = 300;
			pub const DefaultBootstrapPromotedPoolWeight: u8 = 0u8;
			pub const ClearStorageLimit: u32 = 100u32;
		}

		pub struct EnableAssetPoolApi<Runtime>(PhantomData<Runtime>);
		impl<T> AssetRegistryApi<TokenId> for EnableAssetPoolApi<T>
		where
			T: ::orml_asset_registry::Config<
				CustomMetadata = CustomMetadata,
				AssetId = TokenId,
				Balance = Balance,
			>,
		{
			fn enable_pool_creation(assets: (TokenId, TokenId)) -> bool {
				for &asset in [assets.0, assets.1].iter() {
					let meta_maybe: Option<_> = ::orml_asset_registry::Metadata::<T>::get(asset);
					if let Some(xyk) = meta_maybe.clone().and_then(|m| m.additional.xyk) {
						let mut additional = meta_maybe.unwrap().additional;
						if xyk.operations_disabled {
							additional.xyk = Some(XykMetadata { operations_disabled: false });
							match ::orml_asset_registry::Pallet::<T>::do_update_asset(
								asset,
								None,
								None,
								None,
								None,
								None,
								Some(additional),
							) {
								Ok(_) => {},
								Err(e) => {
									log::error!(target: "bootstrap", "cannot modify {} asset: {:?}!", asset, e);
									return false
								},
							}
						}
					}
				}
				true
			}
		}
	}

	pub mod pallet_transaction_payment_mangata {
		use crate::*;

		parameter_types! {
			pub const OperationalFeeMultiplier: u8 = 5;
			pub const TransactionByteFee: Balance = 5 * consts::MILLIUNIT;
		pub ConstFeeMultiplierValue: Multiplier = Multiplier::saturating_from_rational(1, 1);
		}

		pub type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
		pub type FeeMultiplierUpdate = ConstFeeMultiplier<ConstFeeMultiplierValue>;

		pub type ORMLCurrencyAdapterNegativeImbalance<Runtime> =
			<::orml_tokens::MultiTokenCurrencyAdapter<Runtime> as MultiTokenCurrency<
				<Runtime as ::frame_system::Config>::AccountId,
			>>::NegativeImbalance;

		pub trait OnMultiTokenUnbalanced<
			TokenIdType,
			Imbalance: ::frame_support::traits::TryDrop + MultiTokenImbalanceWithZeroTrait<TokenIdType>,
		>
		{
			/// Handler for some imbalances. The different imbalances might have different origins or
			/// meanings, dependent on the context. Will default to simply calling on_unbalanced for all
			/// of them. Infallible.
			fn on_unbalanceds<B>(token_id: TokenIdType, amounts: impl Iterator<Item = Imbalance>)
			where
				Imbalance: ::frame_support::traits::Imbalance<B>,
			{
				Self::on_unbalanced(amounts.fold(Imbalance::from_zero(token_id), |i, x| x.merge(i)))
			}

			/// Handler for some imbalance. Infallible.
			fn on_unbalanced(amount: Imbalance) {
				amount.try_drop().unwrap_or_else(Self::on_nonzero_unbalanced)
			}

			/// Actually handle a non-zero imbalance. You probably want to implement this rather than
			/// `on_unbalanced`.
			fn on_nonzero_unbalanced(amount: Imbalance) {
				drop(amount);
			}
		}

		pub struct ToAuthor<Runtime>(PhantomData<Runtime>);
		impl<T: ::orml_tokens::Config + ::pallet_authorship::Config>
			OnMultiTokenUnbalanced<T::CurrencyId, ORMLCurrencyAdapterNegativeImbalance<T>> for ToAuthor<T>
		{
			fn on_nonzero_unbalanced(amount: ORMLCurrencyAdapterNegativeImbalance<T>) {
				if let Some(author) = ::pallet_authorship::Pallet::<T>::author() {
					<::orml_tokens::MultiTokenCurrencyAdapter<T> as MultiTokenCurrency<
						<T as ::frame_system::Config>::AccountId,
					>>::resolve_creating(amount.0, &author, amount);
				}
			}
		}

		#[derive(Encode, Decode, TypeInfo)]
		pub enum LiquidityInfoEnum<C: MultiTokenCurrency<T::AccountId>, T: frame_system::Config> {
			Imbalance((C::CurrencyId, NegativeImbalanceOf<C, T>)),
			FeeLock,
		}

		pub struct FeeHelpers<T, C, OU, OCA, OFLA>(PhantomData<(T, C, OU, OCA, OFLA)>);
		impl<T, C, OU, OCA, OFLA> FeeHelpers<T, C, OU, OCA, OFLA>
		where
			T: pallet_transaction_payment_mangata::Config
				+ pallet_xyk::Config<Currency = C>
				+ pallet_fee_lock::Config<Tokens = C>,
			T::LengthToFee: frame_support::weights::WeightToFee<
				Balance = <C as MultiTokenCurrency<T::AccountId>>::Balance,
			>,
			C: MultiTokenCurrency<T::AccountId, Balance = Balance, CurrencyId = TokenId>,
			C::PositiveImbalance: Imbalance<
				<C as MultiTokenCurrency<T::AccountId>>::Balance,
				Opposite = C::NegativeImbalance,
			>,
			C::NegativeImbalance: Imbalance<
				<C as MultiTokenCurrency<T::AccountId>>::Balance,
				Opposite = C::PositiveImbalance,
			>,
			OU: OnMultiTokenUnbalanced<C::CurrencyId, NegativeImbalanceOf<C, T>>,
			NegativeImbalanceOf<C, T>: MultiTokenImbalanceWithZeroTrait<C::CurrencyId>,
			OCA: OnChargeTransaction<
				T,
				LiquidityInfo = Option<LiquidityInfoEnum<C, T>>,
				Balance = <C as MultiTokenCurrency<T::AccountId>>::Balance,
			>,
			OFLA: FeeLockTriggerTrait<
				T::AccountId,
				<C as MultiTokenCurrency<T::AccountId>>::Balance,
				<C as MultiTokenCurrency<T::AccountId>>::CurrencyId,
			>,
			// T: frame_system::Config<RuntimeCall = RuntimeCall>,
			T::AccountId: From<sp_runtime::AccountId32> + Into<sp_runtime::AccountId32>,
			sp_runtime::AccountId32: From<T::AccountId>,
		{
			pub fn handle_sell_asset(
				who: &T::AccountId,
				fee_lock_metadata: pallet_fee_lock::FeeLockMetadataInfo<T>,
				sold_asset_id: TokenId,
				sold_asset_amount: Balance,
				bought_asset_id: TokenId,
				min_amount_out: Balance,
			) -> Result<Option<LiquidityInfoEnum<C, T>>, TransactionValidityError> {
				if fee_lock_metadata.is_whitelisted(sold_asset_id) ||
					fee_lock_metadata.is_whitelisted(bought_asset_id)
				{
					let (_, _, _, _, _, bought_asset_amount) =
						<pallet_xyk::Pallet<T> as PreValidateSwaps<
							T::AccountId,
							Balance,
							TokenId,
						>>::pre_validate_sell_asset(
							&who.clone(),
							sold_asset_id,
							bought_asset_id,
							sold_asset_amount,
							min_amount_out,
						)
						.map_err(|_| {
							TransactionValidityError::Invalid(
								InvalidTransaction::SwapPrevalidation.into(),
							)
						})?;
					if Self::is_high_value_swap(
						&fee_lock_metadata,
						sold_asset_id,
						sold_asset_amount,
					) || Self::is_high_value_swap(
						&fee_lock_metadata,
						bought_asset_id,
						bought_asset_amount,
					) {
						let _ = OFLA::unlock_fee(who);
					} else {
						OFLA::process_fee_lock(who).map_err(|_| {
							TransactionValidityError::Invalid(
								InvalidTransaction::ProcessFeeLock.into(),
							)
						})?;
					}
				} else {
					OFLA::process_fee_lock(who).map_err(|_| {
						TransactionValidityError::Invalid(InvalidTransaction::ProcessFeeLock.into())
					})?;
				}
				Ok(Some(LiquidityInfoEnum::FeeLock))
			}

			pub fn is_high_value_swap(
				fee_lock_metadata: &pallet_fee_lock::FeeLockMetadataInfo<T>,
				asset_id: u32,
				asset_amount: u128,
			) -> bool {
				if let (true, Some(valuation)) = (
					fee_lock_metadata.is_whitelisted(asset_id),
					OFLA::get_swap_valuation_for_token(asset_id, asset_amount),
				) {
					valuation >= fee_lock_metadata.swap_value_threshold
				} else {
					false
				}
			}

			pub fn handle_buy_asset(
				who: &T::AccountId,
				fee_lock_metadata: pallet_fee_lock::FeeLockMetadataInfo<T>,
				sold_asset_id: TokenId,
				bought_asset_amount: Balance,
				bought_asset_id: TokenId,
				max_amount_in: Balance,
			) -> Result<Option<LiquidityInfoEnum<C, T>>, TransactionValidityError> {
				if fee_lock_metadata.is_whitelisted(sold_asset_id) ||
					fee_lock_metadata.is_whitelisted(bought_asset_id)
				{
					let (_, _, _, _, _, sold_asset_amount) =
						<pallet_xyk::Pallet<T> as PreValidateSwaps<
							T::AccountId,
							Balance,
							TokenId,
						>>::pre_validate_buy_asset(
							&who.clone(),
							sold_asset_id,
							bought_asset_id,
							bought_asset_amount,
							max_amount_in,
						)
						.map_err(|_| {
							TransactionValidityError::Invalid(
								InvalidTransaction::SwapPrevalidation.into(),
							)
						})?;
					if Self::is_high_value_swap(
						&fee_lock_metadata,
						sold_asset_id,
						sold_asset_amount,
					) || Self::is_high_value_swap(
						&fee_lock_metadata,
						bought_asset_id,
						bought_asset_amount,
					) {
						let _ = OFLA::unlock_fee(who);
					} else {
						OFLA::process_fee_lock(who).map_err(|_| {
							TransactionValidityError::Invalid(
								InvalidTransaction::ProcessFeeLock.into(),
							)
						})?;
					}
				} else {
					// "swap on non-curated token" branch
					OFLA::process_fee_lock(who).map_err(|_| {
						TransactionValidityError::Invalid(InvalidTransaction::ProcessFeeLock.into())
					})?;
				}
				Ok(Some(LiquidityInfoEnum::FeeLock))
			}

			pub fn handle_multiswap_buy_asset(
				who: &T::AccountId,
				_fee_lock_metadata: pallet_fee_lock::FeeLockMetadataInfo<T>,
				swap_token_list: Vec<TokenId>,
				bought_asset_amount: Balance,
				max_amount_in: Balance,
			) -> Result<Option<LiquidityInfoEnum<C, T>>, TransactionValidityError> {
				// ensure swap cannot fail
				// This is to ensure that xyk swap fee is always charged
				// We also ensure that the user has enough funds to transact
				let _ = <pallet_xyk::Pallet<T> as PreValidateSwaps<
					T::AccountId,
					Balance,
					TokenId,
				>>::pre_validate_multiswap_buy_asset(
					&who.clone(),
					swap_token_list,
					bought_asset_amount,
					max_amount_in,
				)
				.map_err(|_| {
					TransactionValidityError::Invalid(InvalidTransaction::SwapPrevalidation.into())
				})?;

				// This is the "low value swap on curated token" branch
				OFLA::process_fee_lock(who).map_err(|_| {
					TransactionValidityError::Invalid(InvalidTransaction::ProcessFeeLock.into())
				})?;
				Ok(Some(LiquidityInfoEnum::FeeLock))
			}

			pub fn handle_multiswap_sell_asset(
				who: &<T>::AccountId,
				_fee_lock_metadata: pallet_fee_lock::FeeLockMetadataInfo<T>,
				swap_token_list: Vec<TokenId>,
				sold_asset_amount: Balance,
				min_amount_out: Balance,
			) -> Result<Option<LiquidityInfoEnum<C, T>>, TransactionValidityError> {
				// ensure swap cannot fail
				// This is to ensure that xyk swap fee is always charged
				// We also ensure that the user has enough funds to transact
				let _ = <pallet_xyk::Pallet<T> as PreValidateSwaps<
					T::AccountId,
					Balance,
					TokenId,
				>>::pre_validate_multiswap_sell_asset(
					&who.clone(),
					swap_token_list.clone(),
					sold_asset_amount,
					min_amount_out,
				)
				.map_err(|_| {
					TransactionValidityError::Invalid(InvalidTransaction::SwapPrevalidation.into())
				})?;

				// This is the "low value swap on curated token" branch
				OFLA::process_fee_lock(who).map_err(|_| {
					TransactionValidityError::Invalid(InvalidTransaction::ProcessFeeLock.into())
				})?;
				Ok(Some(LiquidityInfoEnum::FeeLock))
			}
		}

		const SINGLE_HOP_MULTISWAP: usize = 2;
		#[derive(Encode, Decode, Clone, TypeInfo)]
		pub struct OnChargeHandler<C, OU, OCA, OFLA>(PhantomData<(C, OU, OCA, OFLA)>);

		/// Default implementation for a Currency and an OnUnbalanced handler.
		///
		/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: fee and
		/// then tip.
		impl<T, C, OU, OCA, OFLA> OnChargeTransaction<T> for OnChargeHandler<C, OU, OCA, OFLA>
		where
			T: pallet_transaction_payment_mangata::Config
				+ pallet_xyk::Config<Currency = C>
				+ pallet_fee_lock::Config<Tokens = C>,
			<T as frame_system::Config>::RuntimeCall: Into<crate::CallType>,
			T::LengthToFee: frame_support::weights::WeightToFee<
				Balance = <C as MultiTokenCurrency<T::AccountId>>::Balance,
			>,
			C: MultiTokenCurrency<T::AccountId, Balance = Balance, CurrencyId = TokenId>,
			C::PositiveImbalance: Imbalance<
				<C as MultiTokenCurrency<T::AccountId>>::Balance,
				Opposite = C::NegativeImbalance,
			>,
			C::NegativeImbalance: Imbalance<
				<C as MultiTokenCurrency<T::AccountId>>::Balance,
				Opposite = C::PositiveImbalance,
			>,
			OU: OnMultiTokenUnbalanced<C::CurrencyId, NegativeImbalanceOf<C, T>>,
			NegativeImbalanceOf<C, T>: MultiTokenImbalanceWithZeroTrait<TokenId>,
			OCA: OnChargeTransaction<
				T,
				LiquidityInfo = Option<LiquidityInfoEnum<C, T>>,
				Balance = <C as MultiTokenCurrency<T::AccountId>>::Balance,
			>,
			OFLA: FeeLockTriggerTrait<
				T::AccountId,
				<C as MultiTokenCurrency<T::AccountId>>::Balance,
				<C as MultiTokenCurrency<T::AccountId>>::CurrencyId,
			>,
			// T: frame_system::Config<RuntimeCall = RuntimeCall>,
			T::AccountId: From<sp_runtime::AccountId32> + Into<sp_runtime::AccountId32>,
			Balance: From<<C as MultiTokenCurrency<T::AccountId>>::Balance>,
			sp_runtime::AccountId32: From<T::AccountId>,
		{
			type LiquidityInfo = Option<LiquidityInfoEnum<C, T>>;
			type Balance = <C as MultiTokenCurrency<T::AccountId>>::Balance;

			/// Withdraw the predicted fee from the transaction origin.
			///
			/// Note: The `fee` already includes the `tip`.
			fn withdraw_fee(
				who: &T::AccountId,
				call: &T::RuntimeCall,
				info: &DispatchInfoOf<T::RuntimeCall>,
				fee: Self::Balance,
				tip: Self::Balance,
			) -> Result<Self::LiquidityInfo, TransactionValidityError> {
				let call_type: crate::CallType = (*call).clone().into();

				match call_type {
					crate::CallType::MultiSell { .. } |
					crate::CallType::MultiBuy { .. } |
					crate::CallType::AtomicBuy { .. } |
					crate::CallType::AtomicSell { .. } => {
						ensure!(
							tip.is_zero(),
							TransactionValidityError::Invalid(
								InvalidTransaction::TippingNotAllowedForSwaps.into(),
							)
						);
					},
					_ => {},
				};

				// call.is_unlock_fee();

				// THIS IS NOT PROXY PALLET COMPATIBLE, YET
				// Also ugly implementation to keep it maleable for now
				match (call_type, pallet_fee_lock::FeeLockMetadata::<T>::get()) {
					(
						crate::CallType::AtomicSell {
							sold_asset_id,
							sold_asset_amount,
							bought_asset_id,
							min_amount_out,
						},
						Some(fee_lock_metadata),
					) => FeeHelpers::<T, C, OU, OCA, OFLA>::handle_sell_asset(
						who,
						fee_lock_metadata,
						sold_asset_id,
						sold_asset_amount,
						bought_asset_id,
						min_amount_out,
					),
					(
						crate::CallType::AtomicBuy {
							sold_asset_id,
							bought_asset_amount,
							bought_asset_id,
							max_amount_in,
						},
						Some(fee_lock_metadata),
					) => FeeHelpers::<T, C, OU, OCA, OFLA>::handle_buy_asset(
						who,
						fee_lock_metadata,
						sold_asset_id,
						bought_asset_amount,
						bought_asset_id,
						max_amount_in,
					),
					(
						crate::CallType::MultiBuy {
							swap_token_list,
							bought_asset_amount,
							max_amount_in,
						},
						Some(fee_lock_metadata),
					) if swap_token_list.len() == SINGLE_HOP_MULTISWAP => {
						let sold_asset_id =
							swap_token_list.get(0).ok_or(TransactionValidityError::Invalid(
								InvalidTransaction::SwapPrevalidation.into(),
							))?;
						let bought_asset_id =
							swap_token_list.get(1).ok_or(TransactionValidityError::Invalid(
								InvalidTransaction::SwapPrevalidation.into(),
							))?;
						FeeHelpers::<T, C, OU, OCA, OFLA>::handle_buy_asset(
							who,
							fee_lock_metadata,
							*sold_asset_id,
							bought_asset_amount,
							*bought_asset_id,
							max_amount_in,
						)
					},
					(
						crate::CallType::MultiBuy {
							swap_token_list,
							bought_asset_amount,
							max_amount_in,
						},
						Some(fee_lock_metadata),
					) => FeeHelpers::<T, C, OU, OCA, OFLA>::handle_multiswap_buy_asset(
						who,
						fee_lock_metadata,
						swap_token_list.clone(),
						bought_asset_amount,
						max_amount_in,
					),
					(
						crate::CallType::MultiSell {
							swap_token_list,
							sold_asset_amount,
							min_amount_out,
						},
						Some(fee_lock_metadata),
					) if swap_token_list.len() == SINGLE_HOP_MULTISWAP => {
						let sold_asset_id =
							swap_token_list.get(0).ok_or(TransactionValidityError::Invalid(
								InvalidTransaction::SwapPrevalidation.into(),
							))?;
						let bought_asset_id =
							swap_token_list.get(1).ok_or(TransactionValidityError::Invalid(
								InvalidTransaction::SwapPrevalidation.into(),
							))?;
						FeeHelpers::<T, C, OU, OCA, OFLA>::handle_sell_asset(
							who,
							fee_lock_metadata,
							*sold_asset_id,
							sold_asset_amount,
							*bought_asset_id,
							min_amount_out,
						)
					},
					(
						crate::CallType::MultiSell {
							swap_token_list,
							sold_asset_amount,
							min_amount_out,
						},
						Some(fee_lock_metadata),
					) => FeeHelpers::<T, C, OU, OCA, OFLA>::handle_multiswap_sell_asset(
						who,
						fee_lock_metadata,
						swap_token_list.clone(),
						sold_asset_amount,
						min_amount_out,
					),
					(crate::CallType::UnlockFee, _) => {
						let imb = C::withdraw(
							tokens::MgxTokenId::get().into(),
							who,
							tip,
							WithdrawReasons::TIP,
							ExistenceRequirement::KeepAlive,
						)
						.map_err(|_| {
							TransactionValidityError::Invalid(InvalidTransaction::Payment.into())
						})?;

						OU::on_unbalanceds(tokens::MgxTokenId::get().into(), Some(imb).into_iter());
						OFLA::can_unlock_fee(who).map_err(|_| {
							TransactionValidityError::Invalid(InvalidTransaction::UnlockFee.into())
						})?;
						Ok(Some(LiquidityInfoEnum::FeeLock))
					},
					_ => OCA::withdraw_fee(who, call, info, fee, tip),
				}
			}

			/// Hand the fee and the tip over to the `[OnUnbalanced]` implementation.
			/// Since the predicted fee might have been too high, parts of the fee may
			/// be refunded.
			///
			/// Note: The `corrected_fee` already includes the `tip`.
			fn correct_and_deposit_fee(
				who: &T::AccountId,
				dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
				post_info: &PostDispatchInfoOf<T::RuntimeCall>,
				corrected_fee: Self::Balance,
				tip: Self::Balance,
				already_withdrawn: Self::LiquidityInfo,
			) -> Result<(), TransactionValidityError> {
				match already_withdrawn {
					Some(LiquidityInfoEnum::Imbalance(_)) => OCA::correct_and_deposit_fee(
						who,
						dispatch_info,
						post_info,
						corrected_fee,
						tip,
						already_withdrawn,
					),
					Some(LiquidityInfoEnum::FeeLock) => Ok(()),
					None => Ok(()),
				}
			}
		}

		#[derive(Encode, Decode, Clone, TypeInfo)]
		pub struct ThreeCurrencyOnChargeAdapter<C, OU, T1, T2, T3, SF2, SF3, TE>(
			PhantomData<(C, OU, T1, T2, T3, SF2, SF3, TE)>,
		);

		type NegativeImbalanceOf<C, T> =
			<C as MultiTokenCurrency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;

		pub trait TriggerEvent<AccountIdT> {
			fn trigger(who: AccountIdT, fee: u128, tip: u128);
		}

		/// Default implementation for a Currency and an OnUnbalanced handler.
		///
		/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: fee and
		/// then tip.
		impl<T, C, OU, T1, T2, T3, SF2, SF3, TE> OnChargeTransaction<T>
	for ThreeCurrencyOnChargeAdapter<C, OU, T1, T2, T3, SF2, SF3, TE>
where
	T: pallet_transaction_payment_mangata::Config,
	// TE: TriggerEvent<<T as frame_system::Config>::AccountId>,
	TE: TriggerEvent<<T as frame_system::Config>::AccountId>,
	// <<T as pallet_transaction_payment_mangata::Config>::OnChargeTransaction as OnChargeTransaction<T>>::Balance : From<u128>,
	T::LengthToFee: frame_support::weights::WeightToFee<
		Balance = <C as MultiTokenCurrency<<T as frame_system::Config>::AccountId>>::Balance,
	>,
	C: MultiTokenCurrency<<T as frame_system::Config>::AccountId>,
	C::PositiveImbalance: Imbalance<
		<C as MultiTokenCurrency<<T as frame_system::Config>::AccountId>>::Balance,
		Opposite = C::NegativeImbalance,
	>,
	C::NegativeImbalance: Imbalance<
		<C as MultiTokenCurrency<<T as frame_system::Config>::AccountId>>::Balance,
		Opposite = C::PositiveImbalance,
	>,
	OU: OnMultiTokenUnbalanced<C::CurrencyId, NegativeImbalanceOf<C, T>>,
	NegativeImbalanceOf<C, T>: MultiTokenImbalanceWithZeroTrait<TokenId>,
	<C as MultiTokenCurrency<<T as frame_system::Config>::AccountId>>::Balance:
		scale_info::TypeInfo,
	T1: Get<C::CurrencyId>,
	T2: Get<C::CurrencyId>,
	T3: Get<C::CurrencyId>,
	SF2: Get<C::Balance>,
	SF3: Get<C::Balance>,
	// Balance: From<<C as MultiTokenCurrency<<T as frame_system::Config>::AccountId>>::Balance>,
	// Balance: From<TokenId>,
	// sp_runtime::AccountId32: From<<T as frame_system::Config>::AccountId>,
{
	type LiquidityInfo = Option<LiquidityInfoEnum<C, T>>;
	type Balance = <C as MultiTokenCurrency<<T as frame_system::Config>::AccountId>>::Balance;
	/// Withdraw the predicted fee from the transaction origin.
	///
	/// Note: The `fee` already includes the `tip`.
	fn withdraw_fee(
		who: &T::AccountId,
		_call: &T::RuntimeCall,
		_info: &DispatchInfoOf<T::RuntimeCall>,
		fee: Self::Balance,
		tip: Self::Balance,
	) -> Result<Self::LiquidityInfo, TransactionValidityError> {
		if fee.is_zero() {
			return Ok(None)
		}

		let withdraw_reason = if tip.is_zero() {
			WithdrawReasons::TRANSACTION_PAYMENT
		} else {
			WithdrawReasons::TRANSACTION_PAYMENT | WithdrawReasons::TIP
		};

		match C::withdraw(
			T1::get(),
			who,
			fee,
			withdraw_reason,
			ExistenceRequirement::KeepAlive,
		) {
			Ok(imbalance) => Ok(Some(LiquidityInfoEnum::Imbalance((T1::get(), imbalance)))),
			// TODO make sure atleast 1 planck KSM is charged
			Err(_) => match C::withdraw(
				T2::get(),
				who,
				fee / SF2::get(),
				withdraw_reason,
				ExistenceRequirement::KeepAlive,
			) {
				Ok(imbalance) => Ok(Some(LiquidityInfoEnum::Imbalance((T2::get(), imbalance)))),
				Err(_) => match C::withdraw(
					T3::get(),
					who,
					fee / SF3::get(),
					withdraw_reason,
					ExistenceRequirement::KeepAlive,
				) {
					Ok(imbalance) => Ok(Some(LiquidityInfoEnum::Imbalance((T3::get(), imbalance)))),
					Err(_) => Err(InvalidTransaction::Payment.into()),
				},
			},
		}
	}

	/// Hand the fee and the tip over to the `[OnUnbalanced]` implementation.
	/// Since the predicted fee might have been too high, parts of the fee may
	/// be refunded.
	///
	/// Note: The `corrected_fee` already includes the `tip`.
	fn correct_and_deposit_fee(
		who: &T::AccountId,
		_dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
		_post_info: &PostDispatchInfoOf<T::RuntimeCall>,
		corrected_fee: Self::Balance,
		tip: Self::Balance,
		already_withdrawn: Self::LiquidityInfo,
	) -> Result<(), TransactionValidityError> {
		if let Some(LiquidityInfoEnum::Imbalance((token_id, paid))) = already_withdrawn {
			let (corrected_fee, tip) = if token_id == T3::get() {
				(corrected_fee / SF3::get(), tip / SF3::get())
			} else if token_id == T2::get() {
				(corrected_fee / SF2::get(), tip / SF2::get())
			} else {
				(corrected_fee, tip)
			};
			// Calculate how much refund we should return
			let refund_amount = paid.peek().saturating_sub(corrected_fee);
			// refund to the the account that paid the fees. If this fails, the
			// account might have dropped below the existential balance. In
			// that case we don't refund anything.
			let refund_imbalance = C::deposit_into_existing(token_id, &who, refund_amount)
				.unwrap_or_else(|_| C::PositiveImbalance::from_zero(token_id));
			// merge the imbalance caused by paying the fees and refunding parts of it again.
			let adjusted_paid = paid
				.offset(refund_imbalance)
				.same()
				.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?;
			// Call someone else to handle the imbalance (fee and tip separately)
			let (tip_imb, fee) = adjusted_paid.split(tip);
			OU::on_unbalanceds(token_id, Some(fee).into_iter().chain(Some(tip_imb)));

			// TODO: get rid of workaround
			// workround for nested type issue, ideally below should be used
			// TransactionPayment::deposit_event(
			// 	pallet_transaction_payment_mangata::Event::<T>::TransactionFeePaid {
			// 		who,
			// 		actual_fee: fee,
			// 		tip,
			// 	},
			// );
			TE::trigger(who.clone(), corrected_fee.into(), tip.into());
		}
		Ok(())
	}
}
	}

	pub mod pallet_fee_lock {
		use crate::*;
		parameter_types! {
			pub const MaxCuratedTokens: u32 = 100;
		}
	}

	pub mod cumulus_pallet_parachain_system {
		use crate::*;
		parameter_types! {
			pub const ReservedXcmpWeight: Weight = consts::MAXIMUM_BLOCK_WEIGHT.saturating_div(4);
			pub const ReservedDmpWeight: Weight = consts::MAXIMUM_BLOCK_WEIGHT.saturating_div(4);
		}
	}

	pub mod pallet_aura {
		use crate::*;
		parameter_types! {
			pub const MaxAuthorities: u32 = 100_000;
		}
	}

	pub mod pallet_sudo_origin {
		use crate::*;
		pub type SudoOrigin<CouncilCollective> =
			pallet_collective_mangata::EnsureProportionMoreThan<AccountId, CouncilCollective, 1, 2>;
	}

	pub mod pallet_collective_mangata {
		use crate::*;
		#[cfg(not(feature = "fast-runtime"))]
		parameter_types! {
			pub const CouncilProposalCloseDelay: BlockNumber = 3 * consts::DAYS;
		}

		#[cfg(feature = "fast-runtime")]
		parameter_types! {
			pub const CouncilProposalCloseDelay: BlockNumber = 6 * consts::MINUTES;
		}

		#[cfg(feature = "runtime-benchamarks")]
		parameter_types! {
			pub const CouncilProposalCloseDelay: BlockNumber = 0.into();
		}

		parameter_types! {
			pub const CouncilMotionDuration: BlockNumber = 5 * consts::DAYS;
			pub const CouncilMaxProposals: u32 = 100;
			pub const CouncilMaxMembers: u32 = 100;
			pub MaxProposalWeight: Weight = Perbill::from_percent(50) * config::frame_system::RuntimeBlockWeights::get().max_block;
		}

		pub type SetMembersOrigin<AccountId> = EnsureRoot<AccountId>;
	}

	pub mod pallet_maintenance {
		use crate::*;
		pub struct FoundationAccountsProvider<T: frame_system::Config>(PhantomData<T>);
		impl<T: frame_system::Config> Get<Vec<T::AccountId>> for FoundationAccountsProvider<T> {
			fn get() -> Vec<T::AccountId> {
				let accounts: Vec<_> = [
					hex_literal::hex![
						"c8d02dfbff5ce2fda651c7dd7719bc5b17b9c1043fded805bfc86296c5909871"
					],
					hex_literal::hex![
						"c4690c56c36cec7ed5f6ed5d5eebace0c317073a962ebea1d00f1a304974897b"
					],
					hex_literal::hex![
						"fc741134c82b81b7ab7efbf334b0c90ff8dbf22c42ad705ea7c04bf27ed4161a"
					],
				]
				.iter()
				.map(|acc| sp_runtime::AccountId32::from(*acc))
				.collect();

				accounts
					.into_iter()
					.map(|acc| {
						T::AccountId::decode(&mut acc.as_ref())
							// &mut sp_runtime::AccountId32::as_ref(
							// &sp_runtime::AccountId32::from(acc),
							// )
							// )
							.unwrap()
					})
					.collect()
			}
		}
	}

	pub mod parachain_staking {
		use crate::*;

		pub type StakingIssuanceVaultOf<Runtime> =
			<Runtime as pallet_issuance::Config>::StakingIssuanceVault;
		#[cfg(feature = "fast-runtime")]
		parameter_types! {
			/// Default SessionLenght is every 2 minutes (10 * 12 second block times)
			pub const BlocksPerRound: u32 = 2 * consts::MINUTES;
		}

		#[cfg(not(feature = "fast-runtime"))]
		parameter_types! {
			/// Default SessionLenght is every 4 hours (1200 * 12 second block times)
			pub const BlocksPerRound: u32 = 4 * consts::HOURS;
		}

		parameter_types! {
			pub const DefaultPayoutLimit: u32 = 3;
			/// Collator candidate exit delay (number of rounds)
			pub const LeaveCandidatesDelay: u32 = 2;
			/// Collator candidate bond increases/decreases delay (number of rounds)
			pub const CandidateBondDelay: u32 = 2;
			/// Delegator exit delay (number of rounds)
			pub const LeaveDelegatorsDelay: u32 = 2;
			/// Delegation revocations delay (number of rounds)
			pub const RevokeDelegationDelay: u32 = 2;
			/// Delegation bond increases/decreases delay (number of rounds)
			pub const DelegationBondDelay: u32 = 2;
			/// Reward payments delay (number of rounds)
			pub const RewardPaymentDelay: u32 = 2;
			/// Minimum collators selected per round, default at genesis and minimum forever after
			pub const MinSelectedCandidates: u32 = 50;
			/// Maximum collator candidates allowed
			pub const MaxCollatorCandidates: u32 = 100;
			/// Maximum delegators allowed per candidate
			pub const MaxTotalDelegatorsPerCandidate: u32 = 30;
			/// Maximum delegators counted per candidate
			pub const MaxDelegatorsPerCandidate: u32 = 30;
			/// Maximum delegations per delegator
			pub const MaxDelegationsPerDelegator: u32 = 30;
			/// Default fixed percent a collator takes off the top of due rewards
			pub const DefaultCollatorCommission: Perbill = Perbill::from_percent(20);
			/// Minimum stake required to become a collator
			pub const MinCollatorStk: u128 = 10 * currency::DOLLARS;
			/// Minimum stake required to be reserved to be a candidate
			pub const MinCandidateStk: u128 = if cfg!(feature = "runtime-benchmarks") {
				// For benchmarking
				1 * currency::DOLLARS
			} else {
				// ACTUAL
				1_500_000 * currency::DOLLARS
			};
			/// Minimum stake required to be reserved to be a delegator
			pub const MinDelegatorStk: u128 = 1 * currency::CENTS;
		}
	}

	pub mod pallet_issuance {
		use crate::*;
		parameter_types! {
			pub const HistoryLimit: u32 = 10u32;

			pub const LiquidityMiningIssuanceVaultId: PalletId = PalletId(*b"py/lqmiv");
			pub LiquidityMiningIssuanceVault: AccountId = LiquidityMiningIssuanceVaultId::get().into_account_truncating();
			pub const StakingIssuanceVaultId: PalletId = PalletId(*b"py/stkiv");
			pub StakingIssuanceVault: AccountId = StakingIssuanceVaultId::get().into_account_truncating();

			pub const TotalCrowdloanAllocation: Balance = 330_000_000 * DOLLARS;
			pub const IssuanceCap: Balance = 4_000_000_000 * DOLLARS;
			pub const LinearIssuanceBlocks: u32 = 13_140_000u32; // 5 years
			pub const LiquidityMiningSplit: Perbill = Perbill::from_parts(555555556);
			pub const StakingSplit: Perbill = Perbill::from_parts(444444444);
			pub const ImmediateTGEReleasePercent: Percent = Percent::from_percent(20);
			pub const TGEReleasePeriod: u32 = 5_256_000u32; // 2 years
			pub const TGEReleaseBegin: u32 = 100_800u32; // Two weeks into chain start
		}
	}

	pub mod orml_asset_registry {
		use crate::*;

		parameter_types! {
			pub const StringLimit: u32 = 50;
		}

		pub type AssetMetadataOf = AssetMetadata<Balance, CustomMetadata, StringLimit>;
		type CurrencyAdapter<Runtime> = orml_tokens::MultiTokenCurrencyAdapter<Runtime>;

		pub struct SequentialIdWithCreation<T>(PhantomData<T>);
		impl<T> AssetProcessor<TokenId, AssetMetadataOf> for SequentialIdWithCreation<T>
		where
			T: orml_asset_registry::Config,
			T: orml_tokens::Config,
			T: pallet_treasury::Config,
			TokenId: From<<T as orml_tokens::Config>::CurrencyId>,
		{
			fn pre_register(
				id: Option<TokenId>,
				asset_metadata: AssetMetadataOf,
			) -> Result<(TokenId, AssetMetadataOf), DispatchError> {
				let next_id = CurrencyAdapter::<T>::get_next_currency_id();
				let asset_id = id.unwrap_or(next_id.into());
				let treasury_account =
					config::TreasuryPalletIdOf::<T>::get().into_account_truncating();

				match asset_id.cmp(&next_id.into()) {
					Ordering::Equal =>
						CurrencyAdapter::<T>::create(&treasury_account, Default::default())
							.and_then(|created_asset_id| {
								match created_asset_id.cmp(&asset_id.into()) {
									Ordering::Equal => Ok((asset_id, asset_metadata)),
									_ =>
										Err(orml_asset_registry::Error::<T>::InvalidAssetId.into()),
								}
							}),
					Ordering::Less => Ok((asset_id, asset_metadata)),
					_ => Err(orml_asset_registry::Error::<T>::InvalidAssetId.into()),
				}
			}
		}

		pub struct AssetAuthority<T>(PhantomData<T>);
		impl<T> EnsureOriginWithArg<T::RuntimeOrigin, Option<u32>> for AssetAuthority<T>
		where
			T: frame_system::Config,
		{
			type Success = ();

			fn try_origin(
				origin: T::RuntimeOrigin,
				_asset_id: &Option<u32>,
			) -> Result<Self::Success, T::RuntimeOrigin> {
				<EnsureRoot<_> as EnsureOrigin<T::RuntimeOrigin>>::try_origin(origin)
			}

			#[cfg(feature = "runtime-benchmarks")]
			fn try_successful_origin(_: &Option<u32>) -> Result<T::RuntimeOrigin, ()> {
				Ok(T::RuntimeOrigin::root())
			}
		}
	}

	pub mod pallet_identity {
		use crate::*;
		parameter_types! {
			// Add item in storage and take 270 bytes, Registry { [], Balance, Info { [], [u8,32] * 7, [u8,20] }}
			pub const BasicDeposit: Balance = deposit(1, 270);
			// No item in storage, extra field takes 66 bytes, ([u8,32], [u8,32])
			pub const FieldDeposit: Balance = deposit(0, 66);
			// Add item in storage, and takes 97 bytes, AccountId + (AccountId, [u8,32])
			pub const SubAccountDeposit: Balance = deposit(1, 97);
			pub const MaxSubAccounts: u32 = 100;
			pub const MaxAdditionalFields: u32 = 100;
			pub const MaxRegistrars: u32 = 20;
		}

		pub type IdentityForceOrigin = EnsureRoot<AccountId>;
		pub type IdentityRegistrarOrigin = EnsureRoot<AccountId>;
	}

	pub mod pallet_utility_mangata {
		use super::*;

		#[derive(
			Copy,
			Clone,
			Eq,
			PartialEq,
			Ord,
			PartialOrd,
			Encode,
			Decode,
			RuntimeDebug,
			MaxEncodedLen,
			TypeInfo,
		)]
		pub struct DisallowedInBatch<Runtime>(PhantomData<Runtime>);

		impl<T> Contains<T::RuntimeCall> for DisallowedInBatch<T>
		where
			T: ::frame_system::Config,
			<T as ::frame_system::Config>::RuntimeCall: Into<crate::CallType>,
		{
			fn contains(c: &T::RuntimeCall) -> bool {
				let call: crate::CallType = (c.clone()).into();

				match call {
					CallType::MultiSell { .. } |
					CallType::MultiBuy { .. } |
					CallType::AtomicBuy { .. } |
					CallType::AtomicSell { .. } |
					CallType::CompoundRewards |
					CallType::ProvideLiquidityWithConversion => true,
					_ => false,
				}
			}
		}
	}

	pub mod pallet_vesting_mangata {
		use super::*;
		parameter_types! {
			pub const MinVestedTransfer: Balance = 100 * currency::DOLLARS;
			pub UnvestedFundsAllowedWithdrawReasons: WithdrawReasons =
				WithdrawReasons::except(WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE);
		}
	}

	pub mod pallet_crowdloan_rewards {
		use super::*;
		parameter_types! {
			pub const Initialized: bool = false;
			pub const InitializationPayment: Perbill = Perbill::from_parts(214285700);
			pub const MaxInitContributorsBatchSizes: u32 = 100;
			pub const MinimumReward: Balance = 0;
			pub const RelaySignaturesThreshold: Perbill = Perbill::from_percent(100);
			pub const SigantureNetworkIdentifier: &'static [u8] = b"mangata-";
		}
	}

	pub mod pallet_proxy {
		use super::*;
		// Proxy Pallet
		/// The type used to represent the kinds of proxying allowed.
		#[derive(
			Copy,
			Clone,
			Eq,
			PartialEq,
			Ord,
			PartialOrd,
			Encode,
			Decode,
			RuntimeDebug,
			MaxEncodedLen,
			TypeInfo,
		)]
		pub enum ProxyType {
			AutoCompound,
		}

		impl Default for ProxyType {
			fn default() -> Self {
				Self::AutoCompound
			}
		}

		parameter_types! {
			pub const ProxyDepositBase: Balance = deposit(1, 16);
			pub const ProxyDepositFactor: Balance = deposit(0, 33);
			pub const AnnouncementDepositBase: Balance = deposit(1, 16);
			pub const AnnouncementDepositFactor: Balance = deposit(0, 68);
		}
	}

	pub mod pallet_proof_of_stake {
		use super::*;

		parameter_types! {
			pub const RewardsSchedulesLimit: u32 = 10_000u32;
			// NOTE: 1725 is how much USDT you get for one MGX as of 12.2023
			pub const Min3rdPartyRewardValutationPerSession: u128 = 10 * 1725 * currency::DOLLARS;
			pub const Min3rdPartyRewardVolume: u128 = 10_000 * 1725 * currency::DOLLARS;
			pub const SchedulesPerBlock: u32 = 5;
		}
	}
}