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
use crate::chain_spec::{
	get_account_id_from_seed, get_collator_keys_from_seed, Extensions, SAFE_XCM_VERSION,
};

use common_runtime::{
	config::orml_asset_registry::AssetMetadataOf,
	constants::parachains,
	ksm_per_second,
	tokens::{KAR_TOKEN_ID, MGX_TOKEN_ID, RELAY_TOKEN_ID, TUR_TOKEN_ID},
	xcm_config::general_key,
	AccountId, AuraId, CustomMetadata, XcmMetadata,
};
use cumulus_primitives_core::ParaId;
use sc_service::ChainType;
use sp_core::{sr25519, ByteArray, Encode};
use sp_runtime::BoundedVec;
use xcm::prelude::{MultiLocation, Parachain, X1, X2};

use hex::FromHex;

pub mod kusama_mainnet_keys {
	pub const ALICE_SR25519: &str =
		"0x02d3074216e37c4e96c3a35be89fa27b6022fe08f02051989ed5b94768e69652";
	pub const BOB_SR25519: &str =
		"0xac1d5ec7cf53260c5ea1bb6be0d4fd8b23c50c088fad593de7cb60f76de4fe21";
	pub const CHARLIE_SR25519: &str =
		"0x708dbfb26bdf220b53443ff823da3e28845ddbd0d0aab1babd6074ac99b7b254";
	pub const SUDO_SR25519: &str =
		"0x8080dc038d21840c3139140f0fa982b3882c67fc3e558eae7dec4f5f63d11237";
	pub const RELAY_SR25519: &str =
		"0x2ac2b810caa998b14207a8fc6414a94c833974dc482f11a25a2264508c9dff40";
}

/// Specialized `ChainSpec` for the normal parachain runtime.
pub type ChainSpec =
	sc_service::GenericChainSpec<mangata_kusama_runtime::RuntimeGenesisConfig, Extensions>;

/// Generate the session keys from individual elements.
///
/// The input must be a tuple of individual keys (a single arg for now since we have just one key).
pub fn mangata_session_keys(keys: AuraId) -> mangata_kusama_runtime::SessionKeys {
	mangata_kusama_runtime::SessionKeys { aura: keys }
}

pub fn mangata_kusama_prod_config() -> ChainSpec {
	// Give your base currency a unit name and decimal places
	let mut properties = sc_chain_spec::Properties::new();
	properties.insert("tokenSymbol".into(), "MGX".into());
	properties.insert("tokenDecimals".into(), 18.into());
	properties.insert("ss58Format".into(), 42.into());

	ChainSpec::from_genesis(
		// Name
		"Mangata Kusama Mainnet",
		// ID
		"mangata_kusama_mainnet",
		ChainType::Live,
		move || {
			mangata_genesis(
				// initial collators.
				vec![
					(
						kusama_mainnet_keys::ALICE_SR25519.parse::<AccountId>().unwrap().into(),
						AuraId::from_slice(
							&<[u8; 32]>::from_hex(
								kusama_mainnet_keys::ALICE_SR25519.strip_prefix("0x").unwrap(),
							)
							.unwrap(),
						)
						.unwrap(),
					),
					(
						kusama_mainnet_keys::BOB_SR25519.parse::<AccountId>().unwrap().into(),
						AuraId::from_slice(
							&<[u8; 32]>::from_hex(
								kusama_mainnet_keys::BOB_SR25519.strip_prefix("0x").unwrap(),
							)
							.unwrap(),
						)
						.unwrap(),
					),
				],
				// Initial relay account
				kusama_mainnet_keys::RELAY_SR25519.parse::<AccountId>().unwrap().into(),
				// Sudo account
				kusama_mainnet_keys::SUDO_SR25519.parse::<AccountId>().unwrap().into(),
				// Tokens endowment
				vec![
					// MGX
					(
						0u32,
						300_000_000__000_000_000_000_000_000u128,
						kusama_mainnet_keys::ALICE_SR25519.parse::<AccountId>().unwrap().into(),
					),
					// ETH
					(
						1u32,
						0u128,
						kusama_mainnet_keys::ALICE_SR25519.parse::<AccountId>().unwrap().into(),
					),
					(
						0u32,
						400_000_000__000_000_000_000_000_000u128,
						kusama_mainnet_keys::SUDO_SR25519.parse::<AccountId>().unwrap().into(),
					),
					(
						0u32,
						100_000_000__000_000_000_000_000_000u128,
						kusama_mainnet_keys::RELAY_SR25519.parse::<AccountId>().unwrap().into(),
					),
					(
						0u32,
						100_000_000__000_000_000_000_000_000u128,
						kusama_mainnet_keys::BOB_SR25519.parse::<AccountId>().unwrap().into(),
					),
					(
						0u32,
						100_000_000__000_000_000_000_000_000u128,
						kusama_mainnet_keys::CHARLIE_SR25519.parse::<AccountId>().unwrap().into(),
					),
				],
				// Config for Staking
				// Make sure it works with initial-authorities as staking uses both
				vec![
					(
						// Who gets to stake initially
						kusama_mainnet_keys::ALICE_SR25519.parse::<AccountId>().unwrap().into(),
						// Id of MGA token,
						0u32,
						// How much mangata they pool
						100_000_000__000_000_000_000_000_000_u128,
						// Id of the dummy token,
						2u32,
						// How many dummy tokens they pool,
						200_000_000__000_000_000_000_000_000_u128,
						// Id of the liquidity token that is generated
						3u32,
						// How many liquidity tokens they stake,
						100_000_000__000_000_000_000_000_000_u128,
					),
					(
						// Who gets to stake initially
						kusama_mainnet_keys::BOB_SR25519.parse::<AccountId>().unwrap().into(),
						// Id of MGA token,
						0u32,
						// How much mangata they pool
						80_000_000__000_000_000_000_000_000_u128,
						// Id of the dummy token,
						2u32,
						// How many dummy tokens they pool,
						200_000_000__000_000_000_000_000_000_u128,
						// Id of the liquidity token that is generated
						3u32,
						// How many liquidity tokens they stake,
						50_000_000__000_000_000_000_000_000_u128,
					),
				],
				vec![
					(
						MGX_TOKEN_ID,
						AssetMetadataOf {
							decimals: 18,
							name: BoundedVec::truncate_from(b"Mangata".to_vec()),
							symbol: BoundedVec::truncate_from(b"MGX".to_vec()),
							additional: Default::default(),
							existential_deposit: Default::default(),
							location: None,
						},
					),
					(
						1,
						AssetMetadataOf {
							decimals: 18,
							name: BoundedVec::truncate_from(b"Ether".to_vec()),
							symbol: BoundedVec::truncate_from(b"ETH".to_vec()),
							additional: Default::default(),
							existential_deposit: Default::default(),
							location: None,
						},
					),
					(
						RELAY_TOKEN_ID,
						AssetMetadataOf {
							decimals: 12,
							name: BoundedVec::truncate_from(b"Kusama Native".to_vec()),
							symbol: BoundedVec::truncate_from(b"KSM".to_vec()),
							additional: CustomMetadata {
								// 10_000:1 MGX:KSM
								xcm: Some(XcmMetadata { fee_per_second: ksm_per_second() }),
								xyk: None,
							},
							existential_deposit: Default::default(),
							location: None,
						},
					),
				],
				parachains::mangata::ID.into(),
			)
		},
		Vec::new(),
		None,
		// Protocol ID
		Some("mangata-kusama-mainnet"),
		// ForkId
		None,
		// Properties
		Some(properties),
		Extensions {
			relay_chain: "kusama-local".into(), // You MUST set this to the correct network!
			para_id: parachains::mangata::ID,
		},
	)
}

pub fn mangata_kusama_local_config() -> ChainSpec {
	// Give your base currency a unit name and decimal places
	let mut properties = sc_chain_spec::Properties::new();
	properties.insert("tokenSymbol".into(), "MGXL".into());
	properties.insert("tokenDecimals".into(), 18u32.into());
	properties.insert("ss58Format".into(), 42u32.into());

	ChainSpec::from_genesis(
		// Name
		"Mangata Kusama Local",
		// ID
		"mangata_kusama_local",
		ChainType::Local,
		move || {
			mangata_genesis(
				// initial collators.
				vec![
					(
						get_account_id_from_seed::<sr25519::Public>("Alice"),
						get_collator_keys_from_seed("Alice"),
					),
					(
						get_account_id_from_seed::<sr25519::Public>("Bob"),
						get_collator_keys_from_seed("Bob"),
					),
				],
				// Initial relay account
				get_account_id_from_seed::<sr25519::Public>("Relay"),
				// Sudo account
				get_account_id_from_seed::<sr25519::Public>("Alice"),
				// Tokens endowment
				vec![
					// MGA
					(
						0u32,
						300_000_000__000_000_000_000_000_000u128,
						get_account_id_from_seed::<sr25519::Public>("Alice"),
					),
					// ETH
					(1u32, 0u128, get_account_id_from_seed::<sr25519::Public>("Alice")),
					(
						0u32,
						100_000_000__000_000_000_000_000_000u128,
						get_account_id_from_seed::<sr25519::Public>("Relay"),
					),
					(
						0u32,
						100_000_000__000_000_000_000_000_000u128,
						get_account_id_from_seed::<sr25519::Public>("Bob"),
					),
					(
						0u32,
						100_000_000__000_000_000_000_000_000u128,
						get_account_id_from_seed::<sr25519::Public>("Charlie"),
					),
				],
				// Config for Staking
				// Make sure it works with initial-authorities as staking uses both
				vec![
					(
						// Who gets to stake initially
						get_account_id_from_seed::<sr25519::Public>("Alice"),
						// Id of MGA token,
						0u32,
						// How much mangata they pool
						100_000_000__000_000_000_000_000_000_u128,
						// Id of the dummy token,
						2u32,
						// How many dummy tokens they pool,
						200_000_000__000_000_000_000_000_000_u128,
						// Id of the liquidity token that is generated
						3u32,
						// How many liquidity tokens they stake,
						100_000_000__000_000_000_000_000_000_u128,
					),
					(
						// Who gets to stake initially
						get_account_id_from_seed::<sr25519::Public>("Bob"),
						// Id of MGA token,
						0u32,
						// How much mangata they pool
						80_000_000__000_000_000_000_000_000_u128,
						// Id of the dummy token,
						2u32,
						// How many dummy tokens they pool,
						200_000_000__000_000_000_000_000_000_u128,
						// Id of the liquidity token that is generated
						3u32,
						// How many liquidity tokens they stake,
						50_000_000__000_000_000_000_000_000_u128,
					),
				],
				vec![
					(
						MGX_TOKEN_ID,
						AssetMetadataOf {
							decimals: 18,
							name: BoundedVec::truncate_from(b"Mangata".to_vec()),
							symbol: BoundedVec::truncate_from(b"MGXL".to_vec()),
							additional: Default::default(),
							existential_deposit: Default::default(),
							location: None,
						},
					),
					(
						1,
						AssetMetadataOf {
							decimals: 18,
							name: BoundedVec::truncate_from(b"Ether".to_vec()),
							symbol: BoundedVec::truncate_from(b"ETH".to_vec()),
							additional: Default::default(),
							existential_deposit: Default::default(),
							location: None,
						},
					),
					(
						RELAY_TOKEN_ID,
						AssetMetadataOf {
							decimals: 12,
							name: BoundedVec::truncate_from(b"Kusama Native".to_vec()),
							symbol: BoundedVec::truncate_from(b"KSM".to_vec()),
							additional: CustomMetadata {
								// 10_000:1 MGX:KSM
								xcm: Some(XcmMetadata { fee_per_second: ksm_per_second() }),
								xyk: None,
							},
							existential_deposit: Default::default(),
							location: None,
						},
					),
					// empty placeholder to issueToken & increment nextAssetId
					(
						5,
						AssetMetadataOf {
							decimals: 0,
							name: BoundedVec::new(),
							symbol: BoundedVec::new(),
							additional: Default::default(),
							existential_deposit: Default::default(),
							location: None,
						},
					),
					(
						KAR_TOKEN_ID,
						AssetMetadataOf {
							decimals: 12,
							name: BoundedVec::truncate_from(b"Karura".to_vec()),
							symbol: BoundedVec::truncate_from(b"KAR".to_vec()),
							additional: CustomMetadata {
								// 100:1 MGR:KAR
								xcm: Some(XcmMetadata { fee_per_second: ksm_per_second() * 100 }),
								xyk: None,
							},
							existential_deposit: Default::default(),
							location: Some(
								MultiLocation::new(
									1,
									X2(
										Parachain(parachains::karura::ID),
										general_key(parachains::karura::KAR_KEY),
									),
								)
								.into(),
							),
						},
					),
					(
						TUR_TOKEN_ID,
						AssetMetadataOf {
							decimals: 10,
							name: BoundedVec::truncate_from(b"Turing native token".to_vec()),
							symbol: BoundedVec::truncate_from(b"TUR".to_vec()),
							additional: CustomMetadata {
								// 100:1 TUR:ROC, 10/12 decimals
								xcm: Some(XcmMetadata { fee_per_second: ksm_per_second() }),
								xyk: None,
							},
							existential_deposit: Default::default(),
							location: Some(
								MultiLocation::new(1, X1(Parachain(parachains::turing::ID))).into(),
							),
						},
					),
				],
				parachains::mangata::ID.into(),
			)
		},
		// Bootnodes
		Vec::new(),
		// Telemetry
		None,
		// Protocol ID
		Some("mangata-kusama-local"),
		// ForkId
		None,
		// Properties
		Some(properties),
		// Extensions
		Extensions {
			relay_chain: "kusama-local".into(), // You MUST set this to the correct network!
			para_id: parachains::mangata::ID,
		},
	)
}

fn mangata_genesis(
	initial_authorities: Vec<(AccountId, AuraId)>,
	_relay_key: AccountId,
	root_key: AccountId,
	tokens_endowment: Vec<(u32, u128, AccountId)>,
	staking_accounts: Vec<(AccountId, u32, u128, u32, u128, u32, u128)>,
	register_assets: Vec<(u32, AssetMetadataOf)>,
	id: ParaId,
) -> mangata_kusama_runtime::RuntimeGenesisConfig {
	mangata_kusama_runtime::RuntimeGenesisConfig {
		system: mangata_kusama_runtime::SystemConfig {
			code: mangata_kusama_runtime::WASM_BINARY
				.expect("WASM binary was not build, please build it!")
				.to_vec(),
			..Default::default()
		},
		tokens: mangata_kusama_runtime::TokensConfig {
			tokens_endowment: tokens_endowment
				.iter()
				.cloned()
				.map(|(token_id, amount, account)| (account, token_id, amount))
				.collect(),
			created_tokens_for_staking: {
				let mut created_tokens_for_staking_token_1: Vec<(AccountId, u32, u128)> =
					staking_accounts
						.iter()
						.cloned()
						.map(|x| {
							let (who, _, _, token_id, initial_amount, _, _) = x;
							(who.clone(), token_id, initial_amount)
						})
						.collect();
				let mut created_tokens_for_staking_token_2: Vec<(AccountId, u32, u128)> =
					staking_accounts
						.iter()
						.cloned()
						.map(|x| {
							let (who, token_id, initial_amount, _, _, _, _) = x;
							(who.clone(), token_id, initial_amount)
						})
						.collect();
				created_tokens_for_staking_token_1.append(&mut created_tokens_for_staking_token_2);
				created_tokens_for_staking_token_1
			},
		},
		treasury: Default::default(),
		parachain_info: mangata_kusama_runtime::ParachainInfoConfig {
			parachain_id: id,
			..Default::default()
		},
		parachain_staking: mangata_kusama_runtime::ParachainStakingConfig {
			candidates: staking_accounts
				.iter()
				.map(|x| {
					let (account_id, _, _, _, _, liquidity_token_id, liquidity_token_amount) = x;
					(account_id.clone(), *liquidity_token_amount, *liquidity_token_id)
				})
				.collect(),
			delegations: vec![],
		},
		session: mangata_kusama_runtime::SessionConfig {
			keys: initial_authorities
				.into_iter()
				.map(|(acc, aura)| {
					(
						acc.clone(),                // account id
						acc,                        // validator id
						mangata_session_keys(aura), // session keys
					)
				})
				.collect(),
		},
		// no need to pass anything to aura, in fact it will panic if we do. Session will take care
		// of this.
		aura: Default::default(),
		aura_ext: Default::default(),
		parachain_system: Default::default(),
		polkadot_xcm: mangata_kusama_runtime::PolkadotXcmConfig {
			safe_xcm_version: Some(SAFE_XCM_VERSION),
			..Default::default()
		},
		xyk: mangata_kusama_runtime::XykConfig {
			created_pools_for_staking: staking_accounts
				.iter()
				.map(|x| {
					let (
						account_id,
						native_token_id,
						native_token_amount,
						pooled_token_id,
						pooled_token_amount,
						liquidity_token_id,
						_,
					) = x;
					(
						account_id.clone(),
						*native_token_id,
						*native_token_amount,
						*pooled_token_id,
						*pooled_token_amount,
						*liquidity_token_id,
					)
				})
				.collect(),
		},
		fee_lock: mangata_kusama_runtime::FeeLockConfig {
			period_length: Some(10),
			fee_lock_amount: Some(50__000_000_000_000_000_000u128),
			swap_value_threshold: Some(1000__000_000_000_000_000_000u128),
			whitelisted_tokens: Default::default(),
		},
		council: Default::default(),
		transaction_payment: Default::default(),
		sudo: mangata_kusama_runtime::SudoConfig {
			// Assign network admin rights.
			key: Some(root_key),
		},
		asset_registry: mangata_kusama_runtime::AssetRegistryConfig {
			assets: register_assets
				.iter()
				.cloned()
				.map(|(id, meta)| {
					let encoded = AssetMetadataOf::encode(&meta);
					(id, encoded)
				})
				.collect(),
		},
		vesting: Default::default(),
	}
}