1
// Copyright 2019-2025 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Moonbeam is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
use super::*;
18
use crate as pallet_asset_manager;
19
use parity_scale_codec::{Decode, Encode};
20

            
21
use frame_support::{construct_runtime, parameter_types, traits::Everything, weights::Weight};
22
use frame_system::EnsureRoot;
23
use scale_info::TypeInfo;
24
use sp_core::{RuntimeDebug, H256};
25
use sp_runtime::traits::Hash as THash;
26
use sp_runtime::traits::{BlakeTwo256, IdentityLookup};
27
use sp_runtime::{BuildStorage, DispatchError};
28
use xcm::v3::prelude::*;
29

            
30
type Block = frame_system::mocking::MockBlock<Test>;
31

            
32
pub type AccountId = u64;
33
pub type Balance = u64;
34

            
35
192
construct_runtime!(
36
89
	pub enum Test
37
89
	{
38
89
		System: frame_system,
39
89
		Balances: pallet_balances,
40
89
		AssetManager: pallet_asset_manager,
41
89
	}
42
209
);
43

            
44
parameter_types! {
45
	pub const BlockHashCount: u32 = 250;
46
}
47
impl frame_system::Config for Test {
48
	type BaseCallFilter = Everything;
49
	type BlockWeights = ();
50
	type BlockLength = ();
51
	type RuntimeOrigin = RuntimeOrigin;
52
	type RuntimeCall = RuntimeCall;
53
	type RuntimeTask = RuntimeTask;
54
	type Nonce = u64;
55
	type Block = Block;
56
	type Hash = H256;
57
	type Hashing = BlakeTwo256;
58
	type AccountId = AccountId;
59
	type Lookup = IdentityLookup<Self::AccountId>;
60
	type RuntimeEvent = RuntimeEvent;
61
	type BlockHashCount = BlockHashCount;
62
	type DbWeight = ();
63
	type Version = ();
64
	type PalletInfo = PalletInfo;
65
	type AccountData = pallet_balances::AccountData<u64>;
66
	type OnNewAccount = ();
67
	type OnKilledAccount = ();
68
	type SystemWeightInfo = ();
69
	type SS58Prefix = ();
70
	type OnSetCode = ();
71
	type MaxConsumers = frame_support::traits::ConstU32<16>;
72
	type SingleBlockMigrations = ();
73
	type MultiBlockMigrator = ();
74
	type PreInherents = ();
75
	type PostInherents = ();
76
	type PostTransactions = ();
77
	type ExtensionsWeightInfo = ();
78
}
79

            
80
parameter_types! {
81
	pub const ExistentialDeposit: u64 = 0;
82
}
83

            
84
impl pallet_balances::Config for Test {
85
	type Balance = Balance;
86
	type DustRemoval = ();
87
	type RuntimeEvent = RuntimeEvent;
88
	type ExistentialDeposit = ExistentialDeposit;
89
	type AccountStore = System;
90
	type WeightInfo = ();
91
	type MaxLocks = ();
92
	type MaxReserves = ();
93
	type ReserveIdentifier = [u8; 8];
94
	type RuntimeHoldReason = ();
95
	type FreezeIdentifier = ();
96
	type MaxFreezes = ();
97
	type RuntimeFreezeReason = ();
98
	type DoneSlashHandler = ();
99
}
100

            
101
parameter_types! {
102
	pub const AssetDeposit: u64 = 1;
103
	pub const ApprovalDeposit: u64 = 1;
104
	pub const StringLimit: u32 = 50;
105
	pub const MetadataDepositBase: u64 = 1;
106
	pub const MetadataDepositPerByte: u64 = 1;
107
}
108

            
109
parameter_types! {
110
	pub const StatemineParaIdInfo: u32 = 1000u32;
111
	pub const StatemineAssetsInstanceInfo: u8 = 50u8;
112
}
113

            
114
pub type AssetId = u128;
115
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug, TypeInfo)]
116
pub enum MockAssetType {
117
	Xcm(Location),
118
17
	MockAsset(AssetId),
119
}
120

            
121
impl Default for MockAssetType {
122
	fn default() -> Self {
123
		Self::MockAsset(0)
124
	}
125
}
126

            
127
impl From<MockAssetType> for AssetId {
128
7
	fn from(asset: MockAssetType) -> AssetId {
129
7
		match asset {
130
7
			MockAssetType::MockAsset(id) => id,
131
			MockAssetType::Xcm(id) => {
132
				let mut result: [u8; 16] = [0u8; 16];
133
				let hash: H256 = id.using_encoded(<Test as frame_system::Config>::Hashing::hash);
134
				result.copy_from_slice(&hash.as_fixed_bytes()[0..16]);
135
				u128::from_le_bytes(result)
136
			}
137
		}
138
7
	}
139
}
140

            
141
impl From<Location> for MockAssetType {
142
	fn from(location: Location) -> Self {
143
		Self::Xcm(location)
144
	}
145
}
146

            
147
impl Into<Option<Location>> for MockAssetType {
148
	fn into(self) -> Option<Location> {
149
		match self {
150
			Self::Xcm(location) => Some(location),
151
			_ => None,
152
		}
153
	}
154
}
155

            
156
pub struct MockAssetPalletRegistrar;
157

            
158
impl AssetRegistrar<Test> for MockAssetPalletRegistrar {
159
6
	fn create_foreign_asset(
160
6
		_asset: u128,
161
6
		_min_balance: u64,
162
6
		_metadata: u32,
163
6
		_is_sufficient: bool,
164
6
	) -> Result<(), DispatchError> {
165
6
		Ok(())
166
6
	}
167

            
168
1
	fn destroy_foreign_asset(_asset: u128) -> Result<(), DispatchError> {
169
1
		Ok(())
170
1
	}
171

            
172
	fn destroy_asset_dispatch_info_weight(_asset: u128) -> Weight {
173
		Weight::from_parts(0, 0)
174
	}
175
}
176

            
177
impl Config for Test {
178
	type RuntimeEvent = RuntimeEvent;
179
	type Balance = u64;
180
	type AssetId = u128;
181
	type AssetRegistrarMetadata = u32;
182
	type ForeignAssetType = MockAssetType;
183
	type AssetRegistrar = MockAssetPalletRegistrar;
184
	type ForeignAssetModifierOrigin = EnsureRoot<u64>;
185
	type WeightInfo = ();
186
}
187

            
188
pub(crate) struct ExtBuilder {
189
	// endowed accounts with balances
190
	balances: Vec<(AccountId, Balance)>,
191
}
192

            
193
impl Default for ExtBuilder {
194
8
	fn default() -> ExtBuilder {
195
8
		ExtBuilder { balances: vec![] }
196
8
	}
197
}
198

            
199
impl ExtBuilder {
200
8
	pub(crate) fn build(self) -> sp_io::TestExternalities {
201
8
		let mut t = frame_system::GenesisConfig::<Test>::default()
202
8
			.build_storage()
203
8
			.expect("Frame system builds valid default genesis config");
204
8

            
205
8
		pallet_balances::GenesisConfig::<Test> {
206
8
			balances: self.balances,
207
8
		}
208
8
		.assimilate_storage(&mut t)
209
8
		.expect("Pallet balances storage can be assimilated");
210
8
		let mut ext = sp_io::TestExternalities::new(t);
211
8
		ext.execute_with(|| System::set_block_number(1));
212
8
		ext
213
8
	}
214
}
215

            
216
5
pub(crate) fn events() -> Vec<super::Event<Test>> {
217
5
	System::events()
218
5
		.into_iter()
219
9
		.map(|r| r.event)
220
9
		.filter_map(|e| {
221
9
			if let RuntimeEvent::AssetManager(inner) = e {
222
9
				Some(inner)
223
			} else {
224
				None
225
			}
226
9
		})
227
5
		.collect::<Vec<_>>()
228
5
}
229

            
230
5
pub fn expect_events(e: Vec<super::Event<Test>>) {
231
5
	assert_eq!(events(), e);
232
5
}